Viktoriia преди 1 година
родител
ревизия
d55cf549ca

+ 11 - 0
Route.tsx

@@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react';
 import { useFonts } from 'expo-font';
 import * as SplashScreen from 'expo-splash-screen';
 import { Platform } from 'react-native';
+import * as Notifications from 'expo-notifications';
 
 import { createStackNavigator } from '@react-navigation/stack';
 import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
@@ -70,6 +71,7 @@ const Route = () => {
 
   useEffect(() => {
     const prepareApp = async () => {
+      checkTokenAndUpdate();
       await openDatabases();
       setDbLoaded(true);
       await setupDatabaseAndSync();
@@ -88,6 +90,15 @@ const Route = () => {
     hideSplashScreen();
   }, [fontsLoaded, dbLoaded]);
 
+  const checkTokenAndUpdate = async () => {
+    const storedToken = storage.get('deviceToken', StoreType.STRING);
+    const currentToken = (await Notifications.getDevicePushTokenAsync());
+
+    if (storedToken && currentToken?.data !== storedToken) {
+      storage.set('deviceToken', currentToken.data);
+    }
+  };
+
   if (!fontsLoaded) {
     return null;
   }

+ 15 - 0
src/modules/api/app/app-api.ts

@@ -0,0 +1,15 @@
+import { request } from '../../../utils';
+import { API } from '../../../types';
+import { ResponseType } from '../response-type';
+
+export interface PostGetLastUpdate extends ResponseType {
+  date: string;
+}
+
+export const appApi = {
+  deleteUser: (token: string) => request.postForm(API.DELETE_USER, { token }),
+  getLastRegionsUpdate: (date: string) =>
+    request.postForm<PostGetLastUpdate>(API.GET_LAST_REGIONS_DB_UPDATE, { date }),
+  getLastDareUpdate: (date: string) =>
+    request.postForm<PostGetLastUpdate>(API.GET_LAST_DARE_DB_UPDATE, { date })
+};

+ 5 - 0
src/modules/api/app/app-query-keys.tsx

@@ -0,0 +1,5 @@
+export const appQueryKeys = {
+  deleteUser: () => ['deleteUser'] as const,
+  getLastRegionsUpdate: () => ['getLastRegionsUpdate'] as const,
+  getLastDareUpdate: () => ['getLastDareUpdate'] as const,
+};

+ 3 - 0
src/modules/api/app/index.ts

@@ -0,0 +1,3 @@
+export * from './queries';
+export * from './app-api';
+export * from './app-query-keys';

+ 3 - 0
src/modules/api/app/queries/index.ts

@@ -0,0 +1,3 @@
+export * from './use-post-delete-user';
+export * from './use-post-last-regions-db-update';
+export * from './use-post-last-dare-db-update';

+ 17 - 0
src/modules/api/app/queries/use-post-delete-user.tsx

@@ -0,0 +1,17 @@
+import { useMutation } from '@tanstack/react-query';
+
+import { appQueryKeys } from '../app-query-keys';
+import { appApi } from '../app-api';
+import { ResponseType } from '../../response-type';
+
+import type { BaseAxiosError } from '../../../../types';
+
+export const useDeleteUserMutation = () => {
+  return useMutation<ResponseType, BaseAxiosError, { token: string }, ResponseType>({
+    mutationKey: appQueryKeys.deleteUser(),
+    mutationFn: async (variables) => {
+      const response = await appApi.deleteUser(variables.token);
+      return response.data;
+    }
+  });
+};

+ 18 - 0
src/modules/api/app/queries/use-post-last-dare-db-update.tsx

@@ -0,0 +1,18 @@
+import { appQueryKeys } from '../app-query-keys';
+import { type PostGetLastUpdate, appApi } from '../app-api';
+import { queryClient } from 'src/utils/queryClient';
+
+export const fetchLastDareDbUpdate = async (date: string) => {
+  try {
+    const data: PostGetLastUpdate = await queryClient.fetchQuery({
+      queryKey: appQueryKeys.getLastDareUpdate(),
+      queryFn: () => appApi.getLastDareUpdate(date).then((res) => res.data),
+      gcTime: 0,
+      staleTime: 0
+    });
+
+    return data;
+  } catch (error) {
+    console.error('Failed to fetch last dare update:', error);
+  }
+};

+ 18 - 0
src/modules/api/app/queries/use-post-last-regions-db-update.tsx

@@ -0,0 +1,18 @@
+import { appQueryKeys } from '../app-query-keys';
+import { type PostGetLastUpdate, appApi } from '../app-api';
+import { queryClient } from 'src/utils/queryClient';
+
+export const fetchLastRegionsDbUpdate = async (date: string) => {
+  try {
+    const data: PostGetLastUpdate = await queryClient.fetchQuery({
+      queryKey: appQueryKeys.getLastRegionsUpdate(),
+      queryFn: () => appApi.getLastRegionsUpdate(date).then((res) => res.data),
+      gcTime: 0,
+      staleTime: 0
+    });
+
+    return data;
+  } catch (error) {
+    console.error('Failed to fetch last regions update:', error);
+  }
+};

+ 7 - 2
src/screens/InAppScreens/ProfileScreen/Settings/index.tsx

@@ -19,8 +19,11 @@ import UserXMark from '../../../../../assets/icons/user-xmark.svg';
 import type { MenuButtonType } from '../../../../types/components';
 import { StoreType, storage } from 'src/storage';
 import { CommonActions, useNavigation } from '@react-navigation/native';
+import { useDeleteUserMutation } from '@api/app';
 
 const Settings = () => {
+  const { mutate: deleteUser } = useDeleteUserMutation();
+  const token = storage.get('token', StoreType.STRING) as string;
   const [isSubscribed, setIsSubscribed] = useState(false);
   const [shouldOpenWarningModal, setShouldOpenWarningModal] = useState(false);
   const navigation = useNavigation();
@@ -108,7 +111,7 @@ const Settings = () => {
   };
 
   const handleDeleteAccount = () => {
-    console.log('Account deleted');
+    deleteUser({ token }, { onSuccess: handleLogout });
   };
 
   const handleSubscribe = async () => {
@@ -132,6 +135,7 @@ const Settings = () => {
   const toggleSwitch = () => {
     if (isSubscribed) {
       storage.set('subscribed', false);
+      storage.remove('deviceToken');
       setIsSubscribed(false);
     } else {
       setModalInfo({
@@ -163,7 +167,7 @@ const Settings = () => {
       });
       return null;
     }
-    token = (await Notifications.getExpoPushTokenAsync()).data;
+    token = (await Notifications.getDevicePushTokenAsync()).data;
 
     if (Platform.OS === 'android') {
       Notifications.setNotificationChannelAsync('default', {
@@ -173,6 +177,7 @@ const Settings = () => {
         lightColor: '#FF231F7C'
       });
     }
+    storage.set('deviceToken', token);
 
     return token;
   }

+ 10 - 3
src/types/api.ts

@@ -14,7 +14,8 @@ export enum API_ROUTE {
   SLOW = 'slow',
   QUICK_ENTER = 'quickEnter',
   TRIUMPHS = 'triumphs',
-  SERIES_RANKING = 'series-ranking'
+  SERIES_RANKING = 'series-ranking',
+  APP = 'app'
 }
 
 export enum API_ENDPOINT {
@@ -76,7 +77,10 @@ export enum API_ENDPOINT {
   GET_SERIES_GROUPS_RANKING = 'get-series-groups-ranking',
   GET_SERIES_RANKING = 'get-series-ranking',
   GET_USER_DATA = 'get-user-data-app',
-  GET_USER_DATA_DARE = 'get-user-data-dare-app'
+  GET_USER_DATA_DARE = 'get-user-data-dare-app',
+  DELETE_USER = 'delete-user',
+  GET_LAST_REGIONS_UPDATE = 'last-regions-db-update',
+  GET_LAST_DARE_UPDATE = 'last-dare-db-update'
 }
 
 export enum API {
@@ -137,7 +141,10 @@ export enum API {
   GET_SERIES_GROUPS_RANKING = `${API_ROUTE.SERIES_RANKING}/${API_ENDPOINT.GET_SERIES_GROUPS_RANKING}`,
   GET_SERIES_RANKING = `${API_ROUTE.SERIES_RANKING}/${API_ENDPOINT.GET_SERIES_RANKING}`,
   GET_USER_DATA = `${API_ROUTE.REGIONS}/${API_ENDPOINT.GET_USER_DATA}`,
-  GET_USER_DATA_DARE = `${API_ROUTE.REGIONS}/${API_ENDPOINT.GET_USER_DATA_DARE}`
+  GET_USER_DATA_DARE = `${API_ROUTE.REGIONS}/${API_ENDPOINT.GET_USER_DATA_DARE}`,
+  DELETE_USER = `${API_ROUTE.APP}/${API_ENDPOINT.DELETE_USER}`,
+  GET_LAST_REGIONS_DB_UPDATE = `${API_ROUTE.APP}/${API_ENDPOINT.GET_LAST_REGIONS_UPDATE}`,
+  GET_LAST_DARE_DB_UPDATE = `${API_ROUTE.APP}/${API_ENDPOINT.GET_LAST_DARE_UPDATE}`
 }
 
 export type BaseAxiosError = AxiosError;