1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
import AsyncStorage from '@react-native-community/async-storage';
import {Alert} from 'react-native';
import {
COMMENTS_ENDPOINT,
COMMENT_REACTIONS_ENDPOINT,
COMMENT_REACTIONS_REPLY_ENDPOINT,
COMMENT_THREAD_ENDPOINT,
} from '../constants';
import {ERROR_FAILED_TO_COMMENT} from '../constants/strings';
import {
CommentThreadType,
CommentType,
ProfilePreviewType,
ReactionOptionsType,
} from '../types';
export const getComments = async (
objectId: string,
fetchThreads: boolean,
): Promise<CommentType[]> => {
let comments: CommentType[] = [];
try {
const token = await AsyncStorage.getItem('token');
const endpoint = fetchThreads
? COMMENT_THREAD_ENDPOINT + '?comment_id='
: COMMENTS_ENDPOINT + '?moment_id=';
const response = await fetch(endpoint + objectId, {
method: 'GET',
headers: {
Authorization: 'Token ' + token,
},
});
const status = response.status;
if (status === 200) {
comments = await response.json();
} else {
console.log('Could not load comments');
}
} catch (error) {
console.log('Could not load comments', error);
}
return comments;
};
export const postComment = async (
comment: string,
objectId: string,
postThread: boolean,
) => {
try {
const token = await AsyncStorage.getItem('token');
const request = new FormData();
request.append('comment', comment);
if (postThread) {
request.append('comment_id', objectId);
} else {
request.append('moment_id', objectId);
}
const endpoint = postThread ? COMMENT_THREAD_ENDPOINT : COMMENTS_ENDPOINT;
const response = await fetch(endpoint, {
method: 'POST',
headers: {
Authorization: 'Token ' + token,
},
body: request,
});
if (response.status !== 200) {
throw 'server error';
}
return await response.json();
} catch (error) {
Alert.alert(ERROR_FAILED_TO_COMMENT);
return undefined;
}
};
//Get count of comments for a moment
export const getCommentsCount = async (
objectId: string,
fetchThread: boolean,
): Promise<string> => {
let comments_count: string = '';
try {
const token = await AsyncStorage.getItem('token');
const endpoint = fetchThread ? COMMENT_THREAD_ENDPOINT : COMMENTS_ENDPOINT;
const response = await fetch(endpoint + `${objectId}/`, {
method: 'GET',
headers: {
Authorization: 'Token ' + token,
},
});
const status = response.status;
if (status === 200) {
const response_data = await response.json();
comments_count = response_data.count;
} else {
console.log(
'Something went wrong! 😭',
'Not able to retrieve comments count',
);
}
} catch (error) {
console.log(
'Something went wrong! 😭',
'Not able to retrieve comments count',
error,
);
}
return comments_count;
};
export const deleteComment = async (id: string, isThread: boolean) => {
try {
const token = await AsyncStorage.getItem('token');
const url = isThread ? COMMENT_THREAD_ENDPOINT : COMMENTS_ENDPOINT;
const response = await fetch(url + `${id}/`, {
method: 'DELETE',
headers: {
Authorization: 'Token ' + token,
},
});
return response.status === 200;
} catch (error) {
console.log('Failed to delete a comment');
console.log(error);
return false;
}
};
/**
* If `user_reaction` is undefined, we like the comment, if `user_reaction`
* is defined, we unlike the comment.
*
* @param comment the comment object that contains `user_reaction` (or not)
* @returns
*/
export const handleLikeUnlikeComment = async (
comment: CommentType | CommentThreadType,
liked: boolean,
) => {
try {
const isReply = 'parent_comment' in comment;
const token = await AsyncStorage.getItem('token');
let url = isReply
? COMMENT_REACTIONS_REPLY_ENDPOINT
: COMMENT_REACTIONS_ENDPOINT;
if (liked) {
// unlike a comment
url += `${comment.comment_id}/?reaction_type=LIKE`;
const response = await fetch(url, {
method: 'DELETE',
headers: {
Authorization: 'Token ' + token,
},
});
return response.status === 200;
} else {
// like a comment
const form = new FormData();
form.append('comment_id', comment.comment_id);
form.append('reaction_type', ReactionOptionsType.Like);
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
Authorization: 'Token ' + token,
},
body: form,
});
return response.status === 200;
}
} catch (error) {
console.log('Unable to like/unlike a comment');
console.error(error);
}
};
export const getUsersReactedToAComment = async (
comment: CommentType | CommentThreadType,
) => {
try {
const isReply = 'parent_comment' in comment;
const token = await AsyncStorage.getItem('token');
let url = isReply
? COMMENT_REACTIONS_REPLY_ENDPOINT
: COMMENT_REACTIONS_ENDPOINT;
url += `?comment_id=${comment.comment_id}`;
const response = await fetch(url, {
method: 'GET',
headers: {
Authorization: 'Token ' + token,
},
});
const typedResponse: {
reaction: ReactionOptionsType;
user_list: ProfilePreviewType[];
}[] = await response.json();
for (const obj of typedResponse) {
if (obj.reaction === ReactionOptionsType.Like) {
return obj.user_list;
}
}
return [];
} catch (error) {
console.log('Unable to fetch list of users whom reacted to a comment');
console.error(error);
}
return [];
};
|