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, 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, }); if (response.status !== 200) { throw 'server error'; } return await response.json(); } catch (error) { Alert.alert(ERROR_FAILED_TO_COMMENT); return {}; } }; //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 = 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 = 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; } };