//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, ); } };