diff options
author | Ivan Chen <ivan@tagg.id> | 2021-05-10 15:43:33 -0400 |
---|---|---|
committer | Ivan Chen <ivan@tagg.id> | 2021-05-10 16:50:42 -0400 |
commit | c67025c0137074842b2cfeacefe1ea48d5748630 (patch) | |
tree | c98cc2010e223226f28a936b9c4e52c0755f16e4 /src/services/CommentService.ts | |
parent | 4f37038df45ee1aea6d42933ffa79f70a0a426d2 (diff) |
added getUsersReactedToAComment implementation
Diffstat (limited to 'src/services/CommentService.ts')
-rw-r--r-- | src/services/CommentService.ts | 49 |
1 files changed, 46 insertions, 3 deletions
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 []; +}; |