feat: better error handling and better toast messages

This commit is contained in:
Ephraim Atta-Duncan
2023-09-18 15:09:41 +00:00
committed by Mythie
parent 1e0cde850a
commit dec7a9cb38
6 changed files with 76 additions and 45 deletions

View File

@ -7,6 +7,7 @@ import { Loader } from 'lucide-react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { TRPCClientError } from '@documenso/trpc/client';
import { trpc } from '@documenso/trpc/react';
import { cn } from '@documenso/ui/lib/utils';
import { Button } from '@documenso/ui/primitives/button';
@ -43,23 +44,33 @@ export const ForgotPasswordForm = ({ className }: ForgotPasswordFormProps) => {
const { mutateAsync: forgotPassword } = trpc.profile.forgotPassword.useMutation();
const onFormSubmit = async ({ email }: TForgotPasswordFormSchema) => {
// TODO: Handle error with try/catch
await forgotPassword({
email,
});
try {
await forgotPassword({ email });
toast({
title: 'Password updated',
description: 'Your password has been updated successfully.',
duration: 5000,
});
toast({
title: 'Reset email sent',
description: 'Your password reset mail has been sent successfully.',
duration: 5000,
});
await new Promise((resolve) => {
setTimeout(resolve, 2000);
});
reset();
router.push('/check-email');
reset();
router.push('/check-email');
} catch (err) {
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
toast({
title: 'An error occurred',
description: err.message,
variant: 'destructive',
});
} else {
toast({
title: 'An unknown error occurred',
variant: 'destructive',
description:
'We encountered an unknown error while attempting to send your email. Please try again later.',
});
}
}
};
return (

View File

@ -7,6 +7,7 @@ import { Loader } from 'lucide-react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { TRPCClientError } from '@documenso/trpc/client';
import { trpc } from '@documenso/trpc/react';
import { cn } from '@documenso/ui/lib/utils';
import { Button } from '@documenso/ui/primitives/button';
@ -51,24 +52,38 @@ export const ResetPasswordForm = ({ className, token }: ResetPasswordFormProps)
const { mutateAsync: resetPassword } = trpc.profile.resetPassword.useMutation();
const onFormSubmit = async ({ password, repeatedPassword }: TResetPasswordFormSchema) => {
// TODO: Handle error with try/catch
console.log(password, repeatedPassword, token);
const onFormSubmit = async ({ password }: Omit<TResetPasswordFormSchema, 'repeatedPassword'>) => {
try {
await resetPassword({
password,
token,
});
await resetPassword({
password,
token,
});
reset();
reset();
toast({
title: 'Password updated',
description: 'Your password has been updated successfully.',
duration: 5000,
});
toast({
title: 'Password updated',
description: 'Your password has been updated successfully.',
duration: 5000,
});
router.push('/signin');
router.push('/signin');
} catch (err) {
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
toast({
title: 'An error occurred',
description: err.message,
variant: 'destructive',
});
} else {
toast({
title: 'An unknown error occurred',
variant: 'destructive',
description:
'We encountered an unknown error while attempting to reset your password. Please try again later.',
});
}
}
};
return (