Skip to content

Commit

Permalink
chore: cleanup login form
Browse files Browse the repository at this point in the history
Reference: #48, #51, #49

Signed-off-by: Prince Muel <[email protected]>
  • Loading branch information
princemuel committed May 6, 2023
1 parent 6c4f37b commit 9c65dea
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/components/organisms/login.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { IErrorResponse } from '@src/@types';
import { icons } from '@src/common';
import { useAuthDispatch } from '@src/context';
import { LoginFormSchema, RHFSubmitHandler, useZodForm } from '@src/helpers';
import { useLoginMutation, usePersist } from '@src/hooks';
import { client } from '@src/lib';
import { FormProvider } from 'react-hook-form';
import { Link, useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
import { Text } from '../atoms';
import { Loader } from '../layout/loader';
import { FormField, FormFieldPassword } from '../molecules';

type Props = {};
Expand All @@ -18,20 +19,18 @@ const LoginForm = (props: Props) => {
mode: 'onChange',
});

const dispatch = useAuthDispatch();
const navigate = useNavigate();

const { mutate: login } = useLoginMutation(client, {
const { mutate: login, isLoading } = useLoginMutation(client, {
onSuccess(data) {
toast('Login Success', {
type: 'success',
});

dispatch('auth/addToken');
dispatch('auth/addUser');

toast.success('Login Successful');
navigate('/');
},
onError(e: IErrorResponse) {
e.response.errors.forEach(async (error) => {
toast.error(error.message);
});
},
});

const onSubmit: RHFSubmitHandler<typeof LoginFormSchema> = async (data) => {
Expand All @@ -53,6 +52,8 @@ const LoginForm = (props: Props) => {
const isSubmittable =
Boolean(methods.formState.isDirty) && Boolean(methods.formState.isValid);

if (isLoading) return <Loader />;

return (
<FormProvider {...methods}>
<form
Expand Down Expand Up @@ -89,12 +90,11 @@ const LoginForm = (props: Props) => {
<img
src={icons.actions.check}
className='hidden group-aria-pressed:block'
alt=''
/>
</span>

<span className='body-100'>
Recognize this device in the future
</span>
<span className='body-100'>Keep me logged in</span>
</button>

<div className='col-span-6 mt-6'>
Expand Down

0 comments on commit 9c65dea

Please sign in to comment.