diff options
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/CommentService.ts | 118 | ||||
| -rw-r--r-- | src/services/CommonService.ts | 22 | ||||
| -rw-r--r-- | src/services/ExploreService.ts (renamed from src/services/ExploreServices.ts) | 1 | ||||
| -rw-r--r-- | src/services/MomentService.ts (renamed from src/services/MomentServices.ts) | 96 | ||||
| -rw-r--r-- | src/services/UserFriendsService.ts (renamed from src/services/UserFriendsServices.ts) | 0 | ||||
| -rw-r--r-- | src/services/UserProfileService.ts | 12 | ||||
| -rw-r--r-- | src/services/index.ts | 8 |
7 files changed, 147 insertions, 110 deletions
diff --git a/src/services/CommentService.ts b/src/services/CommentService.ts new file mode 100644 index 00000000..2faaa8db --- /dev/null +++ b/src/services/CommentService.ts @@ -0,0 +1,118 @@ +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 {CommentType} from '../types'; + +export const getComments = async ( + objectId: string, + fetchThreads: boolean, +): Promise<CommentType[]> => { + 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<string> => { + 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; + } +}; diff --git a/src/services/CommonService.ts b/src/services/CommonService.ts new file mode 100644 index 00000000..4f9fb47a --- /dev/null +++ b/src/services/CommonService.ts @@ -0,0 +1,22 @@ +import RNFetchBlob from 'rn-fetch-blob'; + +export const loadImageFromURL = async (url: string) => { + try { + if (!url) { + return undefined; + } + const response = await RNFetchBlob.config({ + fileCache: true, + appendExt: 'jpg', + }).fetch('GET', url); + const status = response.info().status; + if (status === 200) { + return response.path(); + } else { + return undefined; + } + } catch (error) { + console.log(error); + return undefined; + } +}; diff --git a/src/services/ExploreServices.ts b/src/services/ExploreService.ts index ca4f1b69..980258be 100644 --- a/src/services/ExploreServices.ts +++ b/src/services/ExploreService.ts @@ -1,5 +1,4 @@ import AsyncStorage from '@react-native-community/async-storage'; -import {getDeviceToken} from 'react-native-device-info'; import {ALL_USERS_ENDPOINT, DISCOVER_ENDPOINT} from '../constants'; import {EMPTY_EXPLORE_SECTIONS} from '../store/initialStates'; import {ExploreSectionType, ProfilePreviewType} from '../types'; diff --git a/src/services/MomentServices.ts b/src/services/MomentService.ts index 735f2ed2..2354d18e 100644 --- a/src/services/MomentServices.ts +++ b/src/services/MomentService.ts @@ -1,101 +1,9 @@ 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 {MOMENTS_ENDPOINT, MOMENT_THUMBNAIL_ENDPOINT} from '../constants'; 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, @@ -192,7 +100,7 @@ export const loadMomentThumbnail = async (momentId: string) => { try { const token = await AsyncStorage.getItem('token'); const response = await RNFetchBlob.config({ - fileCache: true, + fileCache: false, appendExt: 'jpg', }).fetch('GET', MOMENT_THUMBNAIL_ENDPOINT + `${momentId}/`, { Authorization: 'Token ' + token, diff --git a/src/services/UserFriendsServices.ts b/src/services/UserFriendsService.ts index f2e15824..f2e15824 100644 --- a/src/services/UserFriendsServices.ts +++ b/src/services/UserFriendsService.ts diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts index 75d7d367..b400843d 100644 --- a/src/services/UserProfileService.ts +++ b/src/services/UserProfileService.ts @@ -164,12 +164,8 @@ export const loadRecentlySearchedUsers = async () => { export const handlePasswordResetRequest = async (value: string) => { try { - const token = await AsyncStorage.getItem('token'); const response = await fetch(PASSWORD_RESET_ENDPOINT + 'request/', { method: 'POST', - headers: { - Authorization: 'Token ' + token, - }, body: JSON.stringify({ value, }), @@ -204,12 +200,8 @@ export const handlePasswordCodeVerification = async ( otp: string, ) => { try { - const token = await AsyncStorage.getItem('token'); const response = await fetch(PASSWORD_RESET_ENDPOINT + 'verify/', { method: 'POST', - headers: { - Authorization: 'Token ' + token, - }, body: JSON.stringify({ value, otp, @@ -239,12 +231,8 @@ export const handlePasswordCodeVerification = async ( export const handlePasswordReset = async (value: string, password: string) => { try { - const token = await AsyncStorage.getItem('token'); const response = await fetch(PASSWORD_RESET_ENDPOINT + 'reset/', { method: 'POST', - headers: { - Authorization: 'Token ' + token, - }, body: JSON.stringify({ value, password, diff --git a/src/services/index.ts b/src/services/index.ts index 56cefddd..9c168d4f 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -1,11 +1,13 @@ export * from './UserProfileService'; export * from './SocialLinkingService'; -export * from './MomentServices'; -export * from './ExploreServices'; -export * from './UserFriendsServices'; +export * from './MomentService'; +export * from './ExploreService'; +export * from './UserFriendsService'; export * from './ReportingService'; export * from './BlockUserService'; export * from './MomentCategoryService'; export * from './NotificationService'; export * from './FCMService'; export * from './WaitlistUserService'; +export * from './CommonService'; +export * from './CommentService'; |
