fix: use toast

This commit is contained in:
nafees nazik
2023-09-02 11:46:12 +05:30
parent d9da09c1e7
commit 5540fcf0d2

View File

@ -1,10 +1,11 @@
'use client'; 'use client';
/* eslint-disable @typescript-eslint/consistent-type-assertions */ import { useEffect, useRef } from 'react';
import { useSearchParams } from 'next/navigation'; import { useSearchParams } from 'next/navigation';
import { zodResolver } from '@hookform/resolvers/zod'; import { zodResolver } from '@hookform/resolvers/zod';
import { AlertTriangle, Loader } from 'lucide-react'; import { Loader } from 'lucide-react';
import { signIn } from 'next-auth/react'; import { signIn } from 'next-auth/react';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { FcGoogle } from 'react-icons/fc'; import { FcGoogle } from 'react-icons/fc';
@ -12,7 +13,6 @@ import { z } from 'zod';
import { ErrorCodes } from '@documenso/lib/next-auth/error-codes'; import { ErrorCodes } from '@documenso/lib/next-auth/error-codes';
import { cn } from '@documenso/ui/lib/utils'; import { cn } from '@documenso/ui/lib/utils';
import { Alert, AlertDescription } from '@documenso/ui/primitives/alert';
import { Button } from '@documenso/ui/primitives/button'; import { Button } from '@documenso/ui/primitives/button';
import { Input } from '@documenso/ui/primitives/input'; import { Input } from '@documenso/ui/primitives/input';
import { Label } from '@documenso/ui/primitives/label'; import { Label } from '@documenso/ui/primitives/label';
@ -51,6 +51,30 @@ export const SignInForm = ({ className }: SignInFormProps) => {
resolver: zodResolver(ZSignInFormSchema), resolver: zodResolver(ZSignInFormSchema),
}); });
const timer = useRef<NodeJS.Timeout | null>(null);
useEffect(() => {
const error = searchParams?.get('error');
if (error) {
timer.current = setTimeout(() => {
toast({
variant: 'destructive',
description:
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
ErrorMessages[searchParams?.get('error') as keyof typeof ErrorMessages] ??
'an unknown error occurred',
});
}, 100);
}
return () => {
if (timer.current) {
clearInterval(timer.current);
}
timer.current = null;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const onFormSubmit = async ({ email, password }: TSignInFormSchema) => { const onFormSubmit = async ({ email, password }: TSignInFormSchema) => {
try { try {
await signIn('credentials', { await signIn('credentials', {
@ -86,20 +110,6 @@ export const SignInForm = ({ className }: SignInFormProps) => {
}; };
return ( return (
<>
{searchParams?.get('error') && (
<div className="pt-4">
<Alert variant="destructive">
<AlertTriangle className="h-4 w-4" />
<AlertDescription>
{ErrorMessages[searchParams?.get('error') as keyof typeof ErrorMessages] ??
'an unknown error occurred'}
</AlertDescription>
</Alert>
</div>
)}
<form <form
className={cn('flex w-full flex-col gap-y-4', className)} className={cn('flex w-full flex-col gap-y-4', className)}
onSubmit={handleSubmit(onFormSubmit)} onSubmit={handleSubmit(onFormSubmit)}
@ -111,9 +121,7 @@ export const SignInForm = ({ className }: SignInFormProps) => {
<Input id="email" type="email" className="bg-background mt-2" {...register('email')} /> <Input id="email" type="email" className="bg-background mt-2" {...register('email')} />
{errors.email && ( {errors.email && <span className="mt-1 text-xs text-red-500">{errors.email.message}</span>}
<span className="mt-1 text-xs text-red-500">{errors.email.message}</span>
)}
</div> </div>
<div> <div>
@ -136,11 +144,7 @@ export const SignInForm = ({ className }: SignInFormProps) => {
)} )}
</div> </div>
<Button <Button size="lg" disabled={isSubmitting} className="dark:bg-documenso dark:hover:opacity-90">
size="lg"
disabled={isSubmitting}
className="dark:bg-documenso dark:hover:opacity-90"
>
{isSubmitting && <Loader className="mr-2 h-5 w-5 animate-spin" />} {isSubmitting && <Loader className="mr-2 h-5 w-5 animate-spin" />}
Sign In Sign In
</Button> </Button>
@ -163,6 +167,5 @@ export const SignInForm = ({ className }: SignInFormProps) => {
Google Google
</Button> </Button>
</form> </form>
</>
); );
}; };