diff options
| author | Ivan Chen <ivan@tagg.id> | 2021-01-26 18:09:32 -0500 |
|---|---|---|
| committer | Ivan Chen <ivan@tagg.id> | 2021-01-26 18:09:32 -0500 |
| commit | 0570f1fa41bc9d41303ade2cf54592a4670cb87b (patch) | |
| tree | 517894da8b93a8b9c7f06087380aa7b484c8f488 /src/services/MomentService.ts | |
| parent | 6cd49ed14f99fe953026e54969abc6232f3aec57 (diff) | |
renamed services to have consistent naming and added common service
Diffstat (limited to 'src/services/MomentService.ts')
| -rw-r--r-- | src/services/MomentService.ts | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/src/services/MomentService.ts b/src/services/MomentService.ts new file mode 100644 index 00000000..7bad6d4c --- /dev/null +++ b/src/services/MomentService.ts @@ -0,0 +1,209 @@ +import AsyncStorage from '@react-native-community/async-storage'; +import {Alert} from 'react-native'; +import RNFetchBlob from 'rn-fetch-blob'; +import { + COMMENTS_ENDPOINT, + MOMENTS_ENDPOINT, + MOMENT_THUMBNAIL_ENDPOINT, +} from '../constants'; +import {ERROR_FAILED_TO_COMMENT} from '../constants/strings'; +import {MomentType} from '../types'; +import {checkImageUploadStatus} from '../utils'; + +//Get all comments for a moment +export const getMomentComments = async ( + momentId: string, + callback: Function, +) => { + try { + const token = await AsyncStorage.getItem('token'); + 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); + } +}; + +export const postMomentComment = async ( + commenter: string, + comment: string, + momentId: string, +) => { + try { + const token = await AsyncStorage.getItem('token'); + 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, + }); + 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 getMomentCommentsCount = async ( + momentId: string, + callback: Function, +) => { + try { + const token = await AsyncStorage.getItem('token'); + 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, + ); + } +}; + +export const postMoment: ( + fileName: string, + uri: string, + caption: string, + category: string, + userId: string, +) => Promise<number | undefined> = async ( + fileName, + uri, + caption, + category, + userId, +) => { + try { + const request = new FormData(); + //Manipulating filename to end with .jpg instead of .heic + if (fileName.endsWith('.heic') || fileName.endsWith('.HEIC')) { + fileName = fileName.split('.')[0] + '.jpg'; + } + request.append('image', { + uri: uri, + name: fileName, + type: 'image/jpg', + }); + request.append('moment', category); + request.append('user_id', userId); + request.append('captions', JSON.stringify({image: caption})); + const token = await AsyncStorage.getItem('token'); + let response = await fetch(MOMENTS_ENDPOINT, { + method: 'POST', + headers: { + 'Content-Type': 'multipart/form-data', + Authorization: 'Token ' + token, + }, + body: request, + }); + let statusCode = response.status; + let data = await response.json(); + if (statusCode === 200 && checkImageUploadStatus(data.moments)) { + return data.profile_completion_stage; + } + } catch (err) { + console.log(err); + } + return undefined; +}; + +export const loadMoments: ( + userId: string, + token: string, +) => Promise<MomentType[]> = async (userId, token) => { + let moments: MomentType[] = []; + try { + const response = await fetch(MOMENTS_ENDPOINT + '?user_id=' + userId, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (status === 200) { + const data = await response.json(); + moments = data; + } else { + console.log('Could not load moments!'); + return []; + } + } catch (err) { + console.log(err); + return []; + } + return moments; +}; + +export const deleteMoment = async (momentId: string) => { + try { + const token = await AsyncStorage.getItem('token'); + + const response = await fetch(MOMENTS_ENDPOINT + `${momentId}/`, { + method: 'DELETE', + headers: { + Authorization: 'Token ' + token, + }, + }); + return response.status === 200; + } catch (error) { + console.log(error); + console.log('Unable to delete moment', momentId); + 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; + } +}; |
