From 6674e928c3621c742b56842db9e01d665a244ad3 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Mon, 10 May 2021 15:51:34 -0400 Subject: added implementation for handling like or unlike a comment --- src/services/CommentService.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src') diff --git a/src/services/CommentService.ts b/src/services/CommentService.ts index 69c5f3bc..7ede11cc 100644 --- a/src/services/CommentService.ts +++ b/src/services/CommentService.ts @@ -138,8 +138,29 @@ export const handleLikeUnlikeComment = async (comment: CommentBaseType) => { const token = await AsyncStorage.getItem('token'); if (comment.user_reaction !== undefined) { // unlike a comment + const url = COMMENT_REACTIONS_ENDPOINT + `${comment.user_reaction.id}/`; + const response = await fetch(url, { + method: 'DELETE', + headers: { + Authorization: 'Token ' + token, + }, + }); + return response.status === 200; } else { // like a comment + const url = COMMENT_REACTIONS_ENDPOINT; + 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; } return undefined; } catch (error) { -- cgit v1.2.3-70-g09d2