ソースを参照

quickEnter fixes

Viktoriia 3 週間 前
コミット
caf15fd694

+ 81 - 14
src/contexts/RegionContext.tsx

@@ -1,6 +1,11 @@
 import { fetchCountryUserData, usePostSetSlowMutation } from '@api/countries';
 import { usePostSetDareRegionMutation } from '@api/myDARE';
-import { usePostSetNmRegionMutation } from '@api/myRegions';
+import {
+  usePostAddVisitMutation,
+  usePostGetSingleRegionMutation,
+  usePostSetNmRegionMutation,
+  usePostSetNotVisitedMutation
+} from '@api/myRegions';
 import React, { createContext, useContext, useState, useCallback } from 'react';
 import { DareRegion, NmRegion, SlowData } from 'src/screens/InAppScreens/TravelsScreen/utils/types';
 import { StoreType, storage } from 'src/storage';
@@ -19,6 +24,9 @@ export const RegionProvider = ({ children }: { children: React.ReactNode }) => {
   const { mutate: updateDARE } = usePostSetDareRegionMutation();
   const { mutate: updateSlow } = usePostSetSlowMutation();
   const { mutateAsync: mutateCountriesData } = fetchCountryUserData();
+  const { mutateAsync: unvisitRegion } = usePostSetNotVisitedMutation();
+  const { mutateAsync: addVisit } = usePostAddVisitMutation();
+  const { mutateAsync: getRegion } = usePostGetSingleRegionMutation();
 
   const handleUpdateNM = useCallback(
     (region: number, first: number, last: number, visits: number, quality: number) => {
@@ -31,16 +39,47 @@ export const RegionProvider = ({ children }: { children: React.ReactNode }) => {
         visited: visits > 0 ? true : false
       };
 
+      if (visits === 0) {
+        unvisitRegion(
+          { token, region },
+          {
+            onSuccess: (res) => {
+              updatedNM && setUserData(updatedNM);
+            },
+            onError: (err) => {
+              console.log('err', err);
+            }
+          }
+        );
+
+        return;
+      }
+
+      const currentYear = new Date().getFullYear();
+      const currentMonth = new Date().getMonth() + 1;
+      updatedNM.first_visit_year = currentYear;
+      updatedNM.last_visit_year = currentYear;
+      updatedNM.best_visit_quality = 3;
+
       const updatedNMData = {
         token,
         region,
-        first,
-        last,
-        visits,
-        quality
+        quality: 3,
+        year_from: currentYear,
+        month_from: currentMonth,
+        day_from: undefined,
+        year_to: currentYear,
+        month_to: currentMonth,
+        day_to: undefined,
+        hidden: 0
       };
 
-      updateNM(updatedNMData);
+      addVisit(updatedNMData as never, {
+        onError: (error) => {
+          console.log('addVisit error', error);
+        }
+      });
+
       updatedNM && setUserData(updatedNM);
     },
     [userData, token]
@@ -122,29 +161,57 @@ export const RegionProvider = ({ children }: { children: React.ReactNode }) => {
 
   const handleUpdateNMList = useCallback(
     (id: number, first: number, last: number, visits: number, quality: number) => {
+      const currentYear = new Date().getFullYear();
+      const currentMonth = new Date().getMonth() + 1;
+
       const updatedNM = nmRegions.map((item) => {
         if (item.id === id) {
           return {
             ...item,
-            year: first,
-            last,
-            quality,
+            year: visits === 0 ? first : currentYear,
+            last: visits === 0 ? last : currentYear,
+            quality: visits === 0 ? quality : 3,
             visits
           };
         }
         return item;
       });
 
+      if (visits === 0) {
+        unvisitRegion(
+          { token, region: id },
+          {
+            onSuccess: (res) => {
+              updatedNM && setNmRegions(updatedNM);
+            },
+            onError: (err) => {
+              console.log('err', err);
+            }
+          }
+        );
+
+        return;
+      }
+
       const updatedNMData = {
         token,
         region: id,
-        first,
-        last,
-        visits,
-        quality
+        quality: 3,
+        year_from: currentYear,
+        month_from: currentMonth,
+        day_from: undefined,
+        year_to: currentYear,
+        month_to: currentMonth,
+        day_to: undefined,
+        hidden: 0
       };
 
-      updateNM(updatedNMData);
+      addVisit(updatedNMData as never, {
+        onError: (error) => {
+          console.log('addVisit error', error);
+        }
+      });
+
       updatedNM && setNmRegions(updatedNM);
     },
     [nmRegions]

+ 6 - 3
src/modules/api/maps/queries/use-post-get-visited-regions-ids.tsx

@@ -10,14 +10,17 @@ export const usePostGetVisitedRegionsIdsQuery = (
   type: 'in' | 'by',
   year: number,
   uid: number,
-  enabled: boolean
+  enabled: boolean,
+  forceRefetch: number = 0
 ) => {
   return useQuery<PostGetVisitedIds, BaseAxiosError>({
-    queryKey: mapsQueryKeys.getVisitedRegionsIds(token, type, year, uid),
+    queryKey: [...mapsQueryKeys.getVisitedRegionsIds(token, type, year, uid), forceRefetch],
     queryFn: async () => {
       const response = await mapsApi.getVisitedRegionsIds(token, type, year, uid);
       return response.data;
     },
-    enabled
+    enabled,
+    staleTime: 0,
+    gcTime: 0,
   });
 };

+ 1 - 0
src/modules/api/myRegions/queries/index.ts

@@ -8,3 +8,4 @@ export * from './use-post-add-visit';
 export * from './use-post-update-visit';
 export * from './use-post-delete-visit';
 export * from './use-post-get-single-region';
+export * from './use-post-set-not-visited';

+ 17 - 0
src/modules/api/myRegions/queries/use-post-set-not-visited.tsx

@@ -0,0 +1,17 @@
+import { useMutation } from '@tanstack/react-query';
+
+import { regionsQueryKeys } from '../regions-query-keys';
+import { type PostUnvisit, regionsApi } from '../regions-api';
+
+import type { BaseAxiosError } from '../../../../types';
+import { ResponseType } from '@api/response-type';
+
+export const usePostSetNotVisitedMutation = () => {
+  return useMutation<ResponseType, BaseAxiosError, PostUnvisit, ResponseType>({
+    mutationKey: regionsQueryKeys.setNotVisited(),
+    mutationFn: async (data) => {
+      const response = await regionsApi.setNotVisited(data);
+      return response.data;
+    }
+  });
+};

+ 7 - 1
src/modules/api/myRegions/regions-api.tsx

@@ -140,6 +140,11 @@ export interface PostDeleteVisit {
   id: number;
 }
 
+export interface PostUnvisit {
+  token: string;
+  region: number;
+}
+
 export interface PostGetSingleRegionReturn {
   not_visited?: 0 | 1;
   region?: {
@@ -170,5 +175,6 @@ export const regionsApi = {
   updateVisit: (data: PostUpdateVisit) => request.postForm<ResponseType>(API.UPDATE_VISIT, data),
   deleteVisit: (data: PostDeleteVisit) => request.postForm<ResponseType>(API.DELETE_VISIT, data),
   getSingleRegion: (data: PostDeleteVisit) =>
-    request.postForm<PostGetSingleRegionReturn>(API.GET_SINGLE_REGION, data)
+    request.postForm<PostGetSingleRegionReturn>(API.GET_SINGLE_REGION, data),
+  setNotVisited: (data: PostUnvisit) => request.postForm<ResponseType>(API.SET_NOT_VISITED, data)
 };

+ 1 - 0
src/modules/api/myRegions/regions-query-keys.tsx

@@ -14,4 +14,5 @@ export const regionsQueryKeys = {
   updateVisit: () => ['updateVisit'] as const,
   deleteVisit: () => ['deleteVisit'] as const,
   getSingleRegion: () => ['getSingleRegion'] as const,
+  setNotVisited: () => ['setNotVisited'] as const
 };

+ 4 - 3
src/screens/InAppScreens/MapScreen/index.tsx

@@ -336,13 +336,15 @@ const MapScreen: any = ({ navigation, route }: { navigation: any; route: any })
     !!token && isConnected
   );
   const { mutateAsync: updateLocation } = usePostUpdateLocationMutation();
+  const [forceRefetch, setForceRefetch] = useState(0);
   const { data: visitedRegionIds, refetch: refetchVisitedRegions } =
     usePostGetVisitedRegionsIdsQuery(
       token,
       regionsFilter.visitedLabel,
       regionsFilter.year,
       +userId,
-      type === 'regions' && !!userId && isConnected
+      type === 'regions' && !!userId && isConnected,
+      forceRefetch
     );
   const { data: visitedCountryIds, refetch: refetchVisitedCountries } =
     usePostGetVisitedCountriesIdsQuery(
@@ -606,14 +608,13 @@ const MapScreen: any = ({ navigation, route }: { navigation: any; route: any })
   useFocusEffect(
     useCallback(() => {
       if (token) {
-        refetchVisitedRegions();
+        setForceRefetch((prev) => prev + 1);
         refetchVisitedCountries();
         refetchVisitedDare();
       }
     }, [navigation, token])
   );
 
-  // to do refactor
   useEffect(() => {
     if (visitedRegionIds) {
       setRegionsVisited(visitedRegionIds.ids);

+ 2 - 2
src/screens/InAppScreens/TravelsScreen/EditNmDataScreen/index.tsx

@@ -198,7 +198,7 @@ const EditNmDataScreen = ({ navigation, route }: { navigation: any; route: any }
             year_to: visit.endDate?.year || null,
             month_to: visit.endDate?.month || null,
             day_to: visit.endDate?.day || null,
-            completed: 1,
+            // completed: 1,
             hidden: 0
           },
           {
@@ -460,7 +460,7 @@ const EditNmDataScreen = ({ navigation, route }: { navigation: any; route: any }
             year_to: v.endDate?.year || null,
             month_to: v.endDate?.month || null,
             day_to: v.endDate?.day || null,
-            completed: 1,
+            // completed: 1,
             hidden: 0
           },
           {

+ 4 - 2
src/types/api.ts

@@ -220,7 +220,8 @@ export enum API_ENDPOINT {
   ADD_VISIT = 'add-visit',
   UPDATE_VISIT = 'update-visit',
   DELETE_VISIT = 'delete-visit',
-  GET_SINGLE_REGION = 'get-single-region'
+  GET_SINGLE_REGION = 'get-single-region',
+  SET_NOT_VISITED = 'set-not-visited'
 }
 
 export enum API {
@@ -414,7 +415,8 @@ export enum API {
   ADD_VISIT = `${API_ROUTE.QUICK_ENTER}/${API_ENDPOINT.ADD_VISIT}`,
   UPDATE_VISIT = `${API_ROUTE.QUICK_ENTER}/${API_ENDPOINT.UPDATE_VISIT}`,
   DELETE_VISIT = `${API_ROUTE.QUICK_ENTER}/${API_ENDPOINT.DELETE_VISIT}`,
-  GET_SINGLE_REGION = `${API_ROUTE.QUICK_ENTER}/${API_ENDPOINT.GET_SINGLE_REGION}`
+  GET_SINGLE_REGION = `${API_ROUTE.QUICK_ENTER}/${API_ENDPOINT.GET_SINGLE_REGION}`,
+  SET_NOT_VISITED = `${API_ROUTE.QUICK_ENTER}/${API_ENDPOINT.SET_NOT_VISITED}`
 }
 
 export type BaseAxiosError = AxiosError;