aboutsummaryrefslogtreecommitdiff
path: root/src/services/MomentServices.ts
diff options
context:
space:
mode:
authorAshm Walia <40498934+ashmgarv@users.noreply.github.com>2021-01-16 09:51:36 -0800
committerGitHub <noreply@github.com>2021-01-16 09:51:36 -0800
commit9711093bfa5810868e3cd17008bb7b3ddc7b9034 (patch)
tree6a8bb4341b1623cfc2da86af4b3b28c1fd3f8a44 /src/services/MomentServices.ts
parentd85eaeb878cbbeedda860ee5809b81100c910af2 (diff)
parent30391867438bb28cbcba9fc9ee2ff6d00027fd86 (diff)
Merge branch 'master' into tma538-friend-requests
Diffstat (limited to 'src/services/MomentServices.ts')
-rw-r--r--src/services/MomentServices.ts59
1 files changed, 41 insertions, 18 deletions
diff --git a/src/services/MomentServices.ts b/src/services/MomentServices.ts
index 91ecf712..514b674c 100644
--- a/src/services/MomentServices.ts
+++ b/src/services/MomentServices.ts
@@ -1,6 +1,16 @@
import AsyncStorage from '@react-native-community/async-storage';
import {Alert} from 'react-native';
-import {COMMENTS_ENDPOINT, MOMENTS_ENDPOINT} from '../constants';
+import RNFetchBlob from 'rn-fetch-blob';
+import {
+ COMMENTS_ENDPOINT,
+ MOMENTS_ENDPOINT,
+ MOMENT_THUMBNAIL_ENDPOINT,
+} from '../constants';
+import {
+ ERROR_FAILED_TO_COMMENT,
+ ERROR_UPLOAD,
+ SUCCESS_PIC_UPLOAD,
+} from '../constants/strings';
import {MomentType} from '../types';
import {checkImageUploadStatus} from '../utils';
@@ -48,20 +58,12 @@ export const postMomentComment = async (
},
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 {};
+ if (response.status !== 200) {
+ throw 'server error';
}
+ return await response.json();
} catch (error) {
- Alert.alert(
- 'Something went wrong! 😭',
- 'Not able to post a comment',
- error,
- );
+ Alert.alert(ERROR_FAILED_TO_COMMENT);
return {};
}
};
@@ -136,15 +138,15 @@ export const postMoment: (
});
let statusCode = response.status;
let data = await response.json();
- if (statusCode === 200 && checkImageUploadStatus(data['moments'])) {
- Alert.alert('The picture was uploaded successfully!');
- return data['profile_completion_stage'];
+ if (statusCode === 200 && checkImageUploadStatus(data.moments)) {
+ Alert.alert(SUCCESS_PIC_UPLOAD);
+ return data.profile_completion_stage;
} else {
- Alert.alert('An error occured while uploading. Please try again!');
+ Alert.alert(ERROR_UPLOAD);
}
} catch (err) {
console.log(err);
- Alert.alert('An error occured during authenticaion. Please login again!');
+ Alert.alert(ERROR_UPLOAD);
}
return undefined;
};
@@ -193,3 +195,24 @@ export const deleteMoment = async (momentId: string) => {
return false;
}
};
+
+export const loadMomentThumbnail = async (momentId: string) => {
+ try {
+ const token = await AsyncStorage.getItem('token');
+ const response = await RNFetchBlob.config({
+ fileCache: true,
+ appendExt: 'jpg',
+ }).fetch('GET', MOMENT_THUMBNAIL_ENDPOINT + `${momentId}/`, {
+ Authorization: 'Token ' + token,
+ });
+ const status = response.info().status;
+ if (status === 200) {
+ return response.path();
+ } else {
+ return undefined;
+ }
+ } catch (error) {
+ console.log(error);
+ return undefined;
+ }
+};