aboutsummaryrefslogtreecommitdiff
path: root/src/services
diff options
context:
space:
mode:
authorAshm Walia <40498934+ashmgarv@users.noreply.github.com>2020-10-22 15:34:21 -0700
committerGitHub <noreply@github.com>2020-10-22 18:34:21 -0400
commitd0237cbb61e5c4d77c7b0cefc50891639646ee91 (patch)
tree5b0c1e33c1043887ad45c06a30173dc469d28228 /src/services
parent5db451725d6165de16ee11cda608a05e96e481f9 (diff)
[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
Diffstat (limited to 'src/services')
-rw-r--r--src/services/MomentServices.ts98
-rw-r--r--src/services/index.ts1
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';