Commit da42e08b authored by tom's avatar tom

show better OTP code errors

parent ee71acd8
...@@ -7,6 +7,7 @@ import type { OtpCodeFormFields, ScreenSuccess } from '../types'; ...@@ -7,6 +7,7 @@ import type { OtpCodeFormFields, ScreenSuccess } from '../types';
import useApiFetch from 'lib/api/useApiFetch'; import useApiFetch from 'lib/api/useApiFetch';
import getErrorMessage from 'lib/errors/getErrorMessage'; import getErrorMessage from 'lib/errors/getErrorMessage';
import getErrorObjPayload from 'lib/errors/getErrorObjPayload';
import useToast from 'lib/hooks/useToast'; import useToast from 'lib/hooks/useToast';
import IconSvg from 'ui/shared/IconSvg'; import IconSvg from 'ui/shared/IconSvg';
...@@ -44,39 +45,47 @@ const AuthModalScreenOtpCode = ({ email, onSuccess, isAuth }: Props) => { ...@@ -44,39 +45,47 @@ const AuthModalScreenOtpCode = ({ email, onSuccess, isAuth }: Props) => {
onSuccess({ type: 'success_email', email, isAuth }); onSuccess({ type: 'success_email', email, isAuth });
}) })
.catch((error) => { .catch((error) => {
// TODO @tom2drum handle incorrect code error const apiError = getErrorObjPayload<{ message: string }>(error);
if (apiError?.message) {
formApi.setError('code', { message: apiError.message });
return;
}
toast({ toast({
status: 'error', status: 'error',
title: 'Error', title: 'Error',
description: getErrorMessage(error) || 'Something went wrong', description: getErrorMessage(error) || 'Something went wrong',
}); });
}); });
}, [ apiFetch, email, onSuccess, toast, isAuth ]); }, [ apiFetch, email, onSuccess, isAuth, toast, formApi ]);
const handleResendCodeClick = React.useCallback(() => { const handleResendCodeClick = React.useCallback(async() => {
return apiFetch('auth_send_otp', { try {
formApi.clearErrors('code');
await apiFetch('auth_send_otp', {
fetchParams: { fetchParams: {
method: 'POST', method: 'POST',
body: { body: { email },
email,
}, },
}, });
})
.then(() => {
toast({ toast({
status: 'success', status: 'success',
title: 'Code sent', title: 'Success',
description: 'Code has been sent to your email', description: 'Code has been sent to your email',
}); });
}) } catch (error) {
.catch((error) => { // TODO @tom2drum check cool down error
const apiError = getErrorObjPayload<{ message: string }>(error);
toast({ toast({
status: 'error', status: 'error',
title: 'Error', title: 'Error',
description: getErrorMessage(error) || 'Something went wrong', description: apiError?.message || getErrorMessage(error) || 'Something went wrong',
});
}); });
}, [ apiFetch, email, toast ]); }
}, [ apiFetch, email, formApi, toast ]);
return ( return (
<FormProvider { ...formApi }> <FormProvider { ...formApi }>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment