diff options
| author | Ivan Chen <ivan@tagg.id> | 2021-05-11 18:56:28 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-11 18:56:28 -0400 | 
| commit | 68d10064cf1dcd2a774a4b2299f3a64f8fb75c60 (patch) | |
| tree | e8367c2a8cdb19e52c2a70b73ab2a89865efd54a /src/services/CommentService.ts | |
| parent | 610b6c9ddd2414b3b0d5c4cc24c35ef6e9e68513 (diff) | |
| parent | 51f397132d227edf5e07d48d673ee167d2aa5937 (diff) | |
Merge pull request #409 from IvanIFChen/tma799-comment-likes
[TMA-799] Comments likes
Diffstat (limited to 'src/services/CommentService.ts')
| -rw-r--r-- | src/services/CommentService.ts | 95 | 
1 files changed, 93 insertions, 2 deletions
| diff --git a/src/services/CommentService.ts b/src/services/CommentService.ts index 2faaa8db..6d71ce9c 100644 --- a/src/services/CommentService.ts +++ b/src/services/CommentService.ts @@ -1,8 +1,18 @@  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_REACTIONS_REPLY_ENDPOINT, +  COMMENT_THREAD_ENDPOINT, +} from '../constants';  import {ERROR_FAILED_TO_COMMENT} from '../constants/strings'; -import {CommentType} from '../types'; +import { +  CommentThreadType, +  CommentType, +  ProfilePreviewType, +  ReactionOptionsType, +} from '../types';  export const getComments = async (    objectId: string, @@ -116,3 +126,84 @@ export const deleteComment = async (id: string, isThread: boolean) => {      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 []; +}; | 
