diff options
author | Ivan Chen <ivan@tagg.id> | 2021-01-27 14:18:00 -0500 |
---|---|---|
committer | Ivan Chen <ivan@tagg.id> | 2021-01-27 14:18:00 -0500 |
commit | 3cbfdc6026ef886b7919181b69a2f252723a6bd8 (patch) | |
tree | 706f55a8d2d624ee2ad2175d6521286f5419b2c3 /src/services/CommentService.ts | |
parent | 3f133f27964439f6ef5caab41d7801bbef498294 (diff) |
Squashed commit of the following:
commit 9298f8a31ca25f53d7fb6a0a90af783b9b01f46c
Author: Ashm Walia <ashmwalia@outlook.com>
Date: Wed Jan 27 09:00:16 2021 -0800
Removed redundancy
commit 2a93a92521d989664ebb8dfebe011d8df67ad40f
Author: Ashm Walia <ashmwalia@outlook.com>
Date: Wed Jan 27 08:58:24 2021 -0800
Sc
commit 2f5f9df6dec1e905f3abf37d124ecd92d0e3a3d9
Author: Ashm Walia <ashmwalia@outlook.com>
Date: Wed Jan 27 08:29:56 2021 -0800
Pre-final
commit 85c0f668665696ba8127ee1ea436d10ec0af955f
Author: Ashm Walia <ashmwalia@outlook.com>
Date: Wed Jan 27 08:24:43 2021 -0800
Pre-final
commit 755f4f540d3e759ff9ad3bc35c64b6f7fc83998b
Author: Ashm Walia <ashmwalia@outlook.com>
Date: Mon Jan 25 18:00:44 2021 -0800
Add provision to show and hide replies
Diffstat (limited to 'src/services/CommentService.ts')
-rw-r--r-- | src/services/CommentService.ts | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/services/CommentService.ts b/src/services/CommentService.ts new file mode 100644 index 00000000..3baf0305 --- /dev/null +++ b/src/services/CommentService.ts @@ -0,0 +1,101 @@ +import AsyncStorage from '@react-native-community/async-storage'; +import {Alert} from 'react-native'; +import {COMMENTS_ENDPOINT, COMMENT_THREAD_ENDPOINT} from '../constants'; +import {ERROR_FAILED_TO_COMMENT} from '../constants/strings'; +import {CommentType} from '../types'; + +export const getComments = async ( + objectId: string, + fetchThreads: boolean, +): Promise<CommentType[]> => { + let comments: CommentType[] = []; + try { + const token = await AsyncStorage.getItem('token'); + const endpoint = fetchThreads + ? COMMENT_THREAD_ENDPOINT + '?comment_id=' + : COMMENTS_ENDPOINT + '?moment_id='; + const response = await fetch(endpoint + objectId, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (status === 200) { + comments = await response.json(); + console.log(comments[0]); + } else { + console.log('Could not load comments'); + } + } catch (error) { + console.log('Could not load comments', error); + } + return comments; +}; + +export const postComment = async ( + comment: string, + objectId: string, + postThread: boolean, +) => { + try { + const token = await AsyncStorage.getItem('token'); + const request = new FormData(); + request.append('comment', comment); + if (postThread) { + request.append('comment_id', objectId); + } else { + request.append('moment_id', objectId); + } + const endpoint = postThread ? COMMENT_THREAD_ENDPOINT : COMMENTS_ENDPOINT; + const response = await fetch(endpoint, { + method: 'POST', + headers: { + Authorization: 'Token ' + token, + }, + body: request, + }); + if (response.status !== 200) { + throw 'server error'; + } + return await response.json(); + } catch (error) { + Alert.alert(ERROR_FAILED_TO_COMMENT); + return undefined; + } +}; + +//Get count of comments for a moment +export const getCommentsCount = async ( + objectId: string, + fetchThread: boolean, +): Promise<string> => { + let comments_count: string = ''; + try { + const token = await AsyncStorage.getItem('token'); + const endpoint = fetchThread ? COMMENT_THREAD_ENDPOINT : COMMENTS_ENDPOINT; + const response = await fetch(endpoint + `${objectId}/`, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (status === 200) { + const response_data = await response.json(); + comments_count = 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, + ); + } + return comments_count; +}; |