From cf6b61e0e903ef4c3ad3dbbdb958e2a3a0efa12c Mon Sep 17 00:00:00 2001 From: Arthur Jamet Date: Tue, 25 Apr 2023 15:52:38 +0100 Subject: [PATCH] Front: Global Error Handling on User Profile Fetch Error --- front/Navigation.tsx | 34 +++++++++++++++++++++++++++------- front/i18n/Translations.ts | 9 +++++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/front/Navigation.tsx b/front/Navigation.tsx index 2a990ec..0a0f167 100644 --- a/front/Navigation.tsx +++ b/front/Navigation.tsx @@ -1,9 +1,9 @@ import { NativeStackScreenProps, createNativeStackNavigator } from '@react-navigation/native-stack'; import { NavigationProp, ParamListBase, useNavigation as navigationHook } from "@react-navigation/native"; -import React from 'react'; +import React, { useEffect } from 'react'; import { DarkTheme, DefaultTheme, NavigationContainer } from '@react-navigation/native'; import { RootState, useSelector } from './state/Store'; -import { translate } from './i18n/i18n'; +import { Translate, translate } from './i18n/i18n'; import SongLobbyView from './views/SongLobbyView'; import AuthenticationView from './views/AuthenticationView'; import StartPageView from './views/StartPageView'; @@ -17,6 +17,7 @@ import ScoreView from './views/ScoreView'; import { LoadingView } from './components/Loading'; import ProfileView from './views/ProfileView'; import useColorScheme from './hooks/colorScheme'; +import { Button, Center, VStack } from 'native-base'; const protectedRoutes = () => ({ @@ -32,6 +33,7 @@ const protectedRoutes = () => ({ const publicRoutes = () => ({ Start: { component: StartPageView, options: { title: "Chromacase", headerShown: false } }, Login: { component: AuthenticationView, options: { title: translate('signInBtn') } }, + Oops: { component: ProfileErrorView, options: { title: 'Oops', headerShown: false } }, }) as const; type Route = { @@ -65,6 +67,16 @@ const routesToScreens = (routes: Partial>) = /> )) +const ProfileErrorView = (props: { onTryAgain: () => any }) => { + return
+ + + + + +
+} + export const Router = () => { const accessToken = useSelector((state: RootState) => state.user.accessToken); const userProfile = useQuery(['user', 'me', accessToken], () => API.getUserInfo(), { @@ -73,17 +85,25 @@ export const Router = () => { }); const colorScheme = useColorScheme(); + useEffect(() => { + if (accessToken) { + userProfile.refetch(); + } + }, [accessToken]); + return ( - { userProfile.isLoading && !userProfile.data ? - - : routesToScreens(userProfile.isSuccess && accessToken - ? protectedRoutes() - : publicRoutes()) + { userProfile.isError && accessToken && !userProfile.isLoading + ? userProfile.refetch()}/>)}/> + : userProfile.isLoading && !userProfile.data ? + + : routesToScreens(userProfile.isSuccess && accessToken + ? protectedRoutes() + : publicRoutes()) } diff --git a/front/i18n/Translations.ts b/front/i18n/Translations.ts index ab529e9..c5fb5a6 100644 --- a/front/i18n/Translations.ts +++ b/front/i18n/Translations.ts @@ -96,7 +96,8 @@ export const en = { unknownError: 'Unknown error', errAlrdExst: 'Already exist', errIncrrct: 'Incorrect Credentials', - + userProfileFetchError: 'An error occured while fetching your profile', + tryAgain: 'Try Again', changePassword: 'Change password', oldPassword: 'Old password', @@ -255,6 +256,8 @@ export const fr: typeof en = { errAlrdExst: "Utilisateur existe déjà", unknownError: 'Erreur inconnue', errIncrrct: 'Identifiant incorrect', + userProfileFetchError: 'Une erreur est survenue lors de la récupération du profil', + tryAgain: 'Réessayer', passwordUpdated: 'Mot de passe mis à jour', emailUpdated: 'Email mis à jour', @@ -401,7 +404,9 @@ export const sp: typeof en = { unknownError: 'Error desconocido', errAlrdExst: "Ya existe", errIncrrct: "credenciales incorrectas", - + userProfileFetchError: 'Ocurrió un error al obtener su perfil', + tryAgain: 'intentar otra vez', + changePassword: 'Cambio de contraseña', oldPassword: 'Contraseña anterior', newPassword: 'Nueva contraseña',