From d0237cbb61e5c4d77c7b0cefc50891639646ee91 Mon Sep 17 00:00:00 2001 From: Ashm Walia <40498934+ashmgarv@users.noreply.github.com> Date: Thu, 22 Oct 2020 15:34:21 -0700 Subject: [TMA 236] Comments PR (#64) * Added comments count and retrieve comments * Working draft * The one before cleanup * Finally * Added time icon and major refactoring * Small fix for social media taggs * Addressed review comments --- src/services/MomentServices.ts | 98 ++++++++++++++++++++++++++++++++++++++++++ src/services/index.ts | 1 + 2 files changed, 99 insertions(+) create mode 100644 src/services/MomentServices.ts (limited to 'src/services') diff --git a/src/services/MomentServices.ts b/src/services/MomentServices.ts new file mode 100644 index 00000000..60f516ce --- /dev/null +++ b/src/services/MomentServices.ts @@ -0,0 +1,98 @@ +//Common moments api abstracted out here + +import {COMMENTS_ENDPOINT} from '../constants'; +import {Alert} from 'react-native'; + +//Get all comments for a moment +export const getMomentComments = async ( + momentId: string, + callback: Function, + token: string, +) => { + try { + const response = await fetch(COMMENTS_ENDPOINT + '?moment_id=' + momentId, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (status === 200) { + const comments = await response.json(); + callback(comments); + } else { + console.log('Could not load comments'); + } + } catch (error) { + console.log('Could not load comments', error); + } +}; + +//Post a comment on a moment +export const postMomentComment = async ( + commenter: string, + comment: string, + momentId: string, + token: string, +) => { + try { + const request = new FormData(); + request.append('moment_id', momentId); + request.append('commenter', commenter); + request.append('comment', comment); + const response = await fetch(COMMENTS_ENDPOINT, { + method: 'POST', + headers: { + Authorization: 'Token ' + token, + }, + body: request, + }); + const status = response.status; + if (status === 200) { + const response_data = await response.json(); + return response_data; + } else { + Alert.alert('Something went wrong! 😭', 'Not able to post a comment'); + return {}; + } + } catch (error) { + Alert.alert( + 'Something went wrong! 😭', + 'Not able to post a comment', + error, + ); + return {}; + } +}; + +//Get count of comments for a moment +export const getMomentCommentsCount = async ( + momentId: string, + callback: Function, + token: string, +) => { + try { + const response = await fetch(COMMENTS_ENDPOINT + `${momentId}/`, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (status === 200) { + const response_data = await response.json(); + callback(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, + ); + } +}; diff --git a/src/services/index.ts b/src/services/index.ts index 5cd06cfe..2abcef95 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -1 +1,2 @@ export * from './UserProfileService'; +export * from './MomentServices'; -- cgit v1.2.3-70-g09d2