Skip to content

Commit

Permalink
chore: refactor register 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 0d768cb commit 6c4f37b
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/components/organisms/register.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { FormProvider } from 'react-hook-form';
import { Link, useNavigate } from 'react-router-dom';
// import clsx from 'clsx';
import { useAuthDispatch } from '@src/context';
import type { IErrorResponse } from '@src/@types';
import { RHFSubmitHandler, RegisterFormSchema, useZodForm } from '@src/helpers';
import { useRegisterMutation } 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 { FormField, FormFieldPassword } from '../molecules';
import { Loader } from './loader';

type Props = {};

Expand All @@ -18,16 +18,16 @@ const RegisterForm = (props: Props) => {
});

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

const { mutate: register } = useRegisterMutation(client, {
const { mutate: register, isLoading } = useRegisterMutation(client, {
onSuccess(data) {
toast.success('Register User Success');
dispatch('auth/logout');
toast.success(data.register?.message);
navigate('/login');
},
onError(e) {
console.log(e);
onError(e: IErrorResponse) {
e.response.errors.forEach(async (error) => {
toast.error(error.message);
});
},
});

Expand All @@ -50,6 +50,8 @@ const RegisterForm = (props: Props) => {
const isSubmittable =
Boolean(methods.formState.isDirty) && Boolean(methods.formState.isValid);

if (isLoading) return <Loader />;

return (
<FormProvider {...methods}>
<form
Expand Down

0 comments on commit 6c4f37b

Please sign in to comment.