Browse Source

loginNeeded errors

Viktoriia 10 months ago
parent
commit
36fad0cca9

+ 22 - 1
src/components/ErrorModal/index.tsx

@@ -9,11 +9,32 @@ import { Colors } from 'src/theme';
 import CloseIcon from 'assets/icons/close.svg';
 import { ButtonVariants } from 'src/types/components';
 import { Button } from '../Button';
+import { CommonActions, useNavigation } from '@react-navigation/native';
+import { NAVIGATION_PAGES } from 'src/types';
+import { storage } from 'src/storage';
+import { useNotification } from 'src/contexts/NotificationContext';
 
 export const ErrorModal = () => {
-  const { error, hideError } = useError();
+  const { error, hideError, navigateToLogin } = useError();
+  const navigation = useNavigation();
+  const { updateNotificationStatus } = useNotification();
 
   const handleClose = () => {
+    if (navigateToLogin) {
+      storage.remove('token');
+      storage.remove('uid');
+      storage.remove('currentUserData');
+      storage.remove('visitedTilesUrl');
+      storage.remove('filterSettings');
+      updateNotificationStatus();
+
+      navigation.dispatch(
+        CommonActions.reset({
+          index: 1,
+          routes: [{ name: NAVIGATION_PAGES.WELCOME }]
+        })
+      );
+    }
     hideError();
   };
 

+ 9 - 5
src/contexts/ErrorContext.tsx

@@ -2,19 +2,23 @@ import React, { createContext, useState, useContext } from 'react';
 
 const ErrorContext = createContext<{
   error: string | null;
-  showError: (message: string) => void;
+  showError: (message: string, loginNeeded: boolean) => void;
   hideError: () => void;
+  navigateToLogin: boolean;
 }>({
   error: null,
-  showError: (message: string) => {},
-  hideError: () => {}
+  showError: (message: string, loginNeeded: boolean) => {},
+  hideError: () => {},
+  navigateToLogin: false
 });
 
 export const ErrorProvider = ({ children }: { children: React.ReactNode }) => {
   const [error, setError] = useState<string | null>(null);
+  const [navigateToLogin, setNavigateToLogin] = useState<boolean>(false);
 
-  const showError = (message: string) => {
+  const showError = (message: string, loginNeeded: boolean) => {
     setError(message);
+    setNavigateToLogin(loginNeeded);
   };
 
   const hideError = () => {
@@ -22,7 +26,7 @@ export const ErrorProvider = ({ children }: { children: React.ReactNode }) => {
   };
 
   return (
-    <ErrorContext.Provider value={{ error, showError, hideError }}>
+    <ErrorContext.Provider value={{ error, showError, hideError, navigateToLogin }}>
       {children}
     </ErrorContext.Provider>
   );

+ 1 - 0
src/modules/api/response-type.ts

@@ -12,4 +12,5 @@ export interface ResponseType {
   result: ResultTypes;
   status?: StatusTypes;
   result_description?: string;
+  login_needed?: number;
 }

+ 18 - 6
src/utils/request.ts

@@ -8,7 +8,11 @@ export const request = axios.create({
   timeout: 10000
 });
 
-export const setupInterceptors = ({ showError }: { showError: (message: string) => void }) => {
+export const setupInterceptors = ({
+  showError
+}: {
+  showError: (message: string, loginNeeded: boolean) => void;
+}) => {
   request.interceptors.request.use(
     (config) => {
       config.headers['App-Version'] = APP_VERSION;
@@ -23,9 +27,17 @@ export const setupInterceptors = ({ showError }: { showError: (message: string)
   request.interceptors.response.use(
     (response) => {
       if (response.data.result === 'ERROR' && response.data.result_description) {
-        setTimeout(() => {
-          showError(response.data.result_description);
-        }, 1000);
+        const showErrorWithDelay = (message: string, requiresLogin: boolean) => {
+          setTimeout(() => {
+            showError(message, requiresLogin);
+          }, 1000);
+        };
+
+        if (response.data?.login_needed && response.data.login_needed === 1) {
+          showErrorWithDelay(response.data.result_description, true);
+          return response;
+        }
+        showErrorWithDelay(response.data.result_description, false);
       }
       return response;
     },
@@ -33,13 +45,13 @@ export const setupInterceptors = ({ showError }: { showError: (message: string)
       if (error.code === 'ECONNABORTED') {
         error.isTimeout = true;
         showBanner('Slow internet connection!');
-
+        
         return;
       } else if (error.message === 'Network Error') {
         return;
       }
 
-      showError(error.message);
+      showError(error.message, false);
 
       return Promise.reject(error);
     }