From c38b6b78935c0ff06ee2a75ccab43324de2aaaa7 Mon Sep 17 00:00:00 2001 From: Prince Muel Date: Wed, 7 Jun 2023 23:18:15 +0100 Subject: [PATCH] feat: refactor the form input component Reference-to: #51, #48 Signed-off-by: Prince Muel --- src/components/molecules/form-field-pwd.tsx | 67 --------------------- src/components/molecules/form-field.tsx | 30 ++++++++- src/components/organisms/login.tsx | 15 ++--- src/components/organisms/register.tsx | 17 +++--- 4 files changed, 41 insertions(+), 88 deletions(-) delete mode 100644 src/components/molecules/form-field-pwd.tsx diff --git a/src/components/molecules/form-field-pwd.tsx b/src/components/molecules/form-field-pwd.tsx deleted file mode 100644 index f3ab91c..0000000 --- a/src/components/molecules/form-field-pwd.tsx +++ /dev/null @@ -1,67 +0,0 @@ -import { EyeIcon, EyeSlashIcon } from '@heroicons/react/24/solid'; -import clsx from 'clsx'; -import { useReducer } from 'react'; -import { FormControl, FormErrorText, FormInput, FormLabel } from '../atoms'; - -interface Props - extends React.DetailedHTMLProps< - React.InputHTMLAttributes, - HTMLInputElement - > { - name: string; - label: string; - labelClassName?: string; - inputClassName?: string; - errorClassName?: string; -} - -const FormFieldPassword = ({ - name, - placeholder, - label, - defaultValue, - autoComplete, - className, - labelClassName, - inputClassName, - errorClassName, -}: Props) => { - const [isPasswordShown, setIsPasswordShown] = useReducer( - (prev) => !prev, - false - ); - - return ( - - - -
- - -
- - -
- ); -}; - -export { FormFieldPassword }; diff --git a/src/components/molecules/form-field.tsx b/src/components/molecules/form-field.tsx index 700d968..c4db1eb 100644 --- a/src/components/molecules/form-field.tsx +++ b/src/components/molecules/form-field.tsx @@ -1,5 +1,7 @@ +import { EyeIcon, EyeSlashIcon } from '@heroicons/react/24/solid'; +import clsx from 'clsx'; +import { useReducer } from 'react'; import { FormControl, FormErrorText, FormInput, FormLabel } from '../atoms'; - interface Props extends React.DetailedHTMLProps< React.InputHTMLAttributes, @@ -10,10 +12,12 @@ interface Props labelClassName?: string; inputClassName?: string; errorClassName?: string; + isPassword?: boolean; } const FormField = ({ name, + isPassword, type, placeholder, label, @@ -24,10 +28,15 @@ const FormField = ({ inputClassName, errorClassName, }: Props) => { + const [isPasswordShown, setIsPasswordShown] = useReducer( + (prev) => !prev, + false + ); + return ( - + + + {isPassword && ( + + )} ); }; diff --git a/src/components/organisms/login.tsx b/src/components/organisms/login.tsx index d18faf6..a16be16 100644 --- a/src/components/organisms/login.tsx +++ b/src/components/organisms/login.tsx @@ -8,7 +8,7 @@ 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 { FormField } from '../molecules'; import { Loader } from './loader'; type Props = {}; @@ -24,14 +24,9 @@ const LoginForm = (props: Props) => { const { mutate: login, isLoading } = useLoginMutation(client, { onSuccess(data) { - dispatch('auth/addToken'); + dispatch({ type: 'auth/addToken', payload: data.login?.token }); toast.success('Login Successful'); - navigate('/'); - }, - onError(e: IErrorResponse) { - e.response.errors.forEach(async (error) => { - toast.error(error.message); - }); + navigate('/', { replace: true }); }, }); @@ -75,11 +70,13 @@ const LoginForm = (props: Props) => { autoComplete='username' /> -
diff --git a/src/components/organisms/register.tsx b/src/components/organisms/register.tsx index 0d324c8..b085101 100644 --- a/src/components/organisms/register.tsx +++ b/src/components/organisms/register.tsx @@ -9,7 +9,7 @@ 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 { FormField } from '../molecules'; import { Loader } from './loader'; type Props = {}; @@ -25,12 +25,7 @@ const RegisterForm = (props: Props) => { const { mutate: register, isLoading } = useRegisterMutation(client, { onSuccess(data) { toast.success(data.register?.message); - navigate('/login'); - }, - onError(e: IErrorResponse) { - e.response.errors.forEach(async (error) => { - toast.error(error.message); - }); + navigate('/login', { replace: true }); }, }); @@ -74,18 +69,22 @@ const RegisterForm = (props: Props) => { autoComplete='username' /> - -