aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Chen <ivan@tagg.id>2021-05-10 15:43:33 -0400
committerIvan Chen <ivan@tagg.id>2021-05-10 16:50:42 -0400
commitc67025c0137074842b2cfeacefe1ea48d5748630 (patch)
treec98cc2010e223226f28a936b9c4e52c0755f16e4
parent4f37038df45ee1aea6d42933ffa79f70a0a426d2 (diff)
added getUsersReactedToAComment implementation
-rw-r--r--src/constants/api.ts1
-rw-r--r--src/services/CommentService.ts49
2 files changed, 47 insertions, 3 deletions
diff --git a/src/constants/api.ts b/src/constants/api.ts
index d45616a1..1a31e815 100644
--- a/src/constants/api.ts
+++ b/src/constants/api.ts
@@ -32,6 +32,7 @@ export const MOMENTS_ENDPOINT: string = API_URL + 'moments/';
export const MOMENT_THUMBNAIL_ENDPOINT: string = API_URL + 'moment-thumbnail/';
export const VERIFY_INVITATION_CODE_ENDPOUNT: string = API_URL + 'verify-code/';
export const COMMENTS_ENDPOINT: string = API_URL + 'comments/';
+export const COMMENT_REACTIONS_ENDPOINT: string = API_URL + 'react-comment/';
export const FRIENDS_ENDPOINT: string = API_URL + 'friends/';
export const ALL_USERS_ENDPOINT: string = API_URL + 'users/';
export const REPORT_ISSUE_ENDPOINT: string = API_URL + 'report/';
diff --git a/src/services/CommentService.ts b/src/services/CommentService.ts
index 6f054c72..69c5f3bc 100644
--- a/src/services/CommentService.ts
+++ b/src/services/CommentService.ts
@@ -1,8 +1,17 @@
import AsyncStorage from '@react-native-community/async-storage';
import {Alert} from 'react-native';
-import {COMMENTS_ENDPOINT, COMMENT_THREAD_ENDPOINT} from '../constants';
+import {
+ COMMENTS_ENDPOINT,
+ COMMENT_REACTIONS_ENDPOINT,
+ COMMENT_THREAD_ENDPOINT,
+} from '../constants';
import {ERROR_FAILED_TO_COMMENT} from '../constants/strings';
-import {CommentBaseType, CommentType} from '../types';
+import {
+ CommentBaseType,
+ CommentType,
+ ProfilePreviewType,
+ ReactionOptionsType,
+} from '../types';
export const getComments = async (
objectId: string,
@@ -121,14 +130,48 @@ export const deleteComment = async (id: string, isThread: boolean) => {
* If `user_reaction` is undefined, we like the comment, if `user_reaction`
* is defined, we unlike the comment.
*
- * @param comment the comment object
+ * @param comment the comment object that contains `user_reaction` (or not)
* @returns
*/
export const handleLikeUnlikeComment = async (comment: CommentBaseType) => {
try {
+ const token = await AsyncStorage.getItem('token');
+ if (comment.user_reaction !== undefined) {
+ // unlike a comment
+ } else {
+ // like a comment
+ }
return undefined;
} catch (error) {
console.log('Unable to like/unlike a comment');
console.error(error);
}
};
+
+export const getUsersReactedToAComment = async (comment: CommentBaseType) => {
+ try {
+ const token = await AsyncStorage.getItem('token');
+ const url =
+ COMMENT_REACTIONS_ENDPOINT + `?comment_id=${comment.comment_id}`;
+ const response = await fetch(url, {
+ method: 'GET',
+ headers: {
+ Authorization: 'Token ' + token,
+ },
+ });
+ const typedResponse: {
+ reaction: ReactionOptionsType;
+ users: ProfilePreviewType[];
+ }[] = await response.json();
+ for (const obj of typedResponse) {
+ if (obj.reaction === ReactionOptionsType.Like) {
+ return obj.users;
+ }
+ }
+ return [];
+ } catch (error) {
+ console.log('Unable to fetch list of users whom reacted to a comment');
+ console.error(error);
+ }
+ return [];
+};