diff options
Diffstat (limited to 'src/services')
-rw-r--r-- | src/services/MomentServices.ts | 98 | ||||
-rw-r--r-- | src/services/index.ts | 1 |
2 files changed, 99 insertions, 0 deletions
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'; |