From cec07b7e99ebada40e93d23797f70b3d45158a81 Mon Sep 17 00:00:00 2001 From: Arthur Jamet Date: Thu, 4 Jan 2024 11:46:13 +0100 Subject: [PATCH] Front: Handle Error when guest username is already taken --- front/components/UI/ScaffoldAuth.tsx | 26 +++++++++++--------------- front/components/forms/guestForm.tsx | 10 +++++++--- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/front/components/UI/ScaffoldAuth.tsx b/front/components/UI/ScaffoldAuth.tsx index 49e44ad..503f608 100644 --- a/front/components/UI/ScaffoldAuth.tsx +++ b/front/components/UI/ScaffoldAuth.tsx @@ -1,5 +1,5 @@ import { LinearGradient } from 'expo-linear-gradient'; -import { Stack, View, Text, Wrap, Image, Row, Column, ScrollView, useToast } from 'native-base'; +import { Stack, View, Text, Wrap, Image, Row, Column, ScrollView } from 'native-base'; import { FunctionComponent, useState } from 'react'; import { Linking, Platform, useWindowDimensions } from 'react-native'; import ButtonBase from './ButtonBase'; @@ -41,7 +41,6 @@ const ScaffoldAuth: FunctionComponent = ({ }) => { const layout = useWindowDimensions(); const dispatch = useDispatch(); - const toast = useToast(); const colorScheme = useColorScheme(); const [guestModalIsOpen, openGuestModal] = useState(false); const [logo] = useAssets( @@ -100,24 +99,21 @@ const ScaffoldAuth: FunctionComponent = ({ setIsVisible={openGuestModal} > { - return handleGuestLogin(username, (accessToken: string) => { + onSubmit={(username) => + handleGuestLogin(username, (accessToken: string) => { dispatch(setAccessToken(accessToken)); }) - .catch((error) => { - if (error instanceof APIError) { - toast.show({ - description: translate(error.userMessage), - }); - return; - } - toast.show({ description: error as string }); - }) .then(() => { openGuestModal(false); return translate('loggedIn'); - }); - }} + }) + .catch((error) => { + if (error instanceof APIError) { + return translate('usernameTaken'); + } + return error as string; + }) + } /> { title={translate('submitBtn')} style={{ marginVertical: 10 }} onPress={() => { - onSubmit(formData.username.value).catch((e) => { - toast.show({ description: e as string }); - }); + onSubmit(formData.username.value) + .then((e) => { + toast.show({ description: e as string }); + }) + .catch((e) => { + toast.show({ description: e as string }); + }); }} />