import AsyncStorage from '@react-native-community/async-storage'; import {Alert} from 'react-native'; import {COMMENTS_ENDPOINT, COMMENT_THREAD_ENDPOINT} from '../constants'; import {ERROR_FAILED_TO_COMMENT} from '../constants/strings'; import {CommentBaseType, CommentType} from '../types'; export const getComments = async ( objectId: string, fetchThreads: boolean, ): Promise => { let comments: CommentType[] = []; try { const token = await AsyncStorage.getItem('token'); const endpoint = fetchThreads ? COMMENT_THREAD_ENDPOINT + '?comment_id=' : COMMENTS_ENDPOINT + '?moment_id='; const response = await fetch(endpoint + objectId, { method: 'GET', headers: { Authorization: 'Token ' + token, }, }); const status = response.status; if (status === 200) { comments = await response.json(); } else { console.log('Could not load comments'); } } catch (error) { console.log('Could not load comments', error); } return comments; }; export const postComment = async ( comment: string, objectId: string, postThread: boolean, ) => { try { const token = await AsyncStorage.getItem('token'); const request = new FormData(); request.append('comment', comment); if (postThread) { request.append('comment_id', objectId); } else { request.append('moment_id', objectId); } const endpoint = postThread ? COMMENT_THREAD_ENDPOINT : COMMENTS_ENDPOINT; const response = await fetch(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 getCommentsCount = async ( objectId: string, fetchThread: boolean, ): Promise => { let comments_count: string = ''; try { const token = await AsyncStorage.getItem('token'); const endpoint = fetchThread ? COMMENT_THREAD_ENDPOINT : COMMENTS_ENDPOINT; const response = await fetch(endpoint + `${objectId}/`, { method: 'GET', headers: { Authorization: 'Token ' + token, }, }); const status = response.status; if (status === 200) { const response_data = await response.json(); comments_count = 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, ); } return comments_count; }; export const deleteComment = async (id: string, isThread: boolean) => { try { const token = await AsyncStorage.getItem('token'); const url = isThread ? COMMENT_THREAD_ENDPOINT : COMMENTS_ENDPOINT; const response = await fetch(url + `${id}/`, { method: 'DELETE', headers: { Authorization: 'Token ' + token, }, }); return response.status === 200; } catch (error) { console.log('Failed to delete a comment'); console.log(error); return false; } }; /** * If `user_reaction` is undefined, we like the comment, if `user_reaction` * is defined, we unlike the comment. * * @param comment the comment object * @returns */ export const handleLikeUnlikeComment = async (comment: CommentBaseType) => { try { return undefined; } catch (error) { console.log('Unable to like/unlike a comment'); console.error(error); } };