diff options
Diffstat (limited to 'src/services')
-rw-r--r-- | src/services/MomentService.ts | 32 | ||||
-rw-r--r-- | src/services/NotificationService.ts | 63 |
2 files changed, 94 insertions, 1 deletions
diff --git a/src/services/MomentService.ts b/src/services/MomentService.ts index e4db95b5..b837585a 100644 --- a/src/services/MomentService.ts +++ b/src/services/MomentService.ts @@ -54,6 +54,38 @@ export const postMoment = async ( return undefined; }; +export const patchMoment = async ( + momentId: string, + caption: string, + tags: { + x: number; + y: number; + z: number; + user_id: string; + }[], +) => { + try { + const request = new FormData(); + request.append('moment_id', momentId); + request.append('captions', JSON.stringify({[momentId]: caption})); + request.append('tags', JSON.stringify(tags)); + const token = await AsyncStorage.getItem('token'); + let response = await fetch(MOMENTS_ENDPOINT, { + method: 'PATCH', + headers: { + 'Content-Type': 'multipart/form-data', + Authorization: 'Token ' + token, + }, + body: request, + }); + let statusCode = response.status; + return statusCode === 200 || statusCode === 201; + } catch (err) { + console.log(err); + } + return false; +}; + export const loadMoments = async (userId: string, token: string) => { try { const response = await fetch(MOMENTS_ENDPOINT + '?user_id=' + userId, { diff --git a/src/services/NotificationService.ts b/src/services/NotificationService.ts index c5c843f5..ccaa9135 100644 --- a/src/services/NotificationService.ts +++ b/src/services/NotificationService.ts @@ -1,5 +1,9 @@ import AsyncStorage from '@react-native-community/async-storage'; -import {NOTIFICATIONS_ENDPOINT} from '../constants'; +import { + NOTIFICATIONS_ENDPOINT, + NOTIFICATIONS_COUNT_ENDPOINT, + NOTIFICATIONS_DATE, +} from '../constants'; import {NotificationType} from '../types'; export const getNotificationsData: () => Promise<NotificationType[]> = @@ -29,3 +33,60 @@ export const getNotificationsData: () => Promise<NotificationType[]> = return []; } }; + +export const getNotificationsUnreadCount = async () => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(NOTIFICATIONS_COUNT_ENDPOINT, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + if (response.status === 200) { + const data: any = await response.json(); + const typedData: { + CMT?: number; + FRD_REQ?: number; + P_VIEW?: number; + MOM_TAG?: number; + } = {}; + if (data.CMT) { + typedData.CMT = data.CMT; + } + if (data.FRD_REQ && data.FRD_REQ > 0) { + typedData.FRD_REQ = data.FRD_REQ; + } + if (data.P_VIEW && data.P_VIEW > 0) { + typedData.P_VIEW = data.P_VIEW; + } + if (data.MOM_TAG && data.MOM_TAG > 0) { + typedData.MOM_TAG = data.MOM_TAG; + } + return typedData; + } + } catch (error) { + console.log('Unable to fetch notifications'); + } + return undefined; +}; + +export const setNotificationsReadDate: () => Promise<boolean> = async () => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(NOTIFICATIONS_DATE, { + method: 'POST', + headers: { + Authorization: 'Token ' + token, + }, + }); + if (response.status === 204) { + return true; + } else { + return false; + } + } catch (error) { + console.log('Unable to fetch notifications'); + return false; + } +}; |