From 6ff5efdb4b7eec25f991f5bfcc02b4a3f883981b Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Wed, 11 Sep 2024 10:04:31 +0700 Subject: update error handling hanya muncul saat button daftar di click --- src-migrate/modules/register/components/Form.tsx | 152 +++++++++++++---------- 1 file changed, 85 insertions(+), 67 deletions(-) (limited to 'src-migrate/modules/register/components/Form.tsx') diff --git a/src-migrate/modules/register/components/Form.tsx b/src-migrate/modules/register/components/Form.tsx index 29c21f62..118d9d69 100644 --- a/src-migrate/modules/register/components/Form.tsx +++ b/src-migrate/modules/register/components/Form.tsx @@ -1,90 +1,98 @@ -import { ChangeEvent, useMemo } from "react"; -import { useMutation } from "react-query"; -import { useRegisterStore } from "../stores/useRegisterStore"; -import { RegisterProps } from "~/types/auth"; -import { registerUser } from "~/services/auth"; -import TermCondition from "./TermCondition"; -import FormCaptcha from "./FormCaptcha"; -import { useRouter } from "next/router"; -import { UseToastOptions, useToast } from "@chakra-ui/react"; -import Link from "next/link"; +import { ChangeEvent, useMemo } from 'react'; +import { useMutation } from 'react-query'; +import { useRegisterStore } from '../stores/useRegisterStore'; +import { RegisterProps } from '~/types/auth'; +import { registerUser } from '~/services/auth'; +import TermCondition from './TermCondition'; +import FormCaptcha from './FormCaptcha'; +import { useRouter } from 'next/router'; +import { UseToastOptions, useToast } from '@chakra-ui/react'; +import Link from 'next/link'; interface FormProps { type: string; required: boolean; isBisnisRegist: boolean; + chekValid: boolean; } -const Form: React.FC = ({ type, required, isBisnisRegist=false }) => { - const { - form, - isCheckedTNC, - isValidCaptcha, - errors, - updateForm, - validate, - } = useRegisterStore() +const Form: React.FC = ({ + type, + required, + isBisnisRegist = false, + chekValid = false, +}) => { + const { form, isCheckedTNC, isValidCaptcha, errors, updateForm, validate } = + useRegisterStore(); - const isFormValid = useMemo(() => Object.keys(errors).length === 0, [errors]) + const isFormValid = useMemo(() => Object.keys(errors).length === 0, [errors]); - const router = useRouter() - const toast = useToast() + const router = useRouter(); + const toast = useToast(); const handleInputChange = (event: ChangeEvent) => { const { name, value } = event.target; - updateForm(name, value) - validate() - } + updateForm(name, value); + validate(); + }; const mutation = useMutation({ - mutationFn: (data: RegisterProps) => registerUser(data) - }) + mutationFn: (data: RegisterProps) => registerUser(data), + }); const handleSubmit = async (e: ChangeEvent) => { - e.preventDefault() + e.preventDefault(); - const response = await mutation.mutateAsync(form) + const response = await mutation.mutateAsync(form); if (response?.register === true) { const urlParams = new URLSearchParams({ activation: 'otp', email: form.email, - redirect: (router.query?.next || '/') as string - }) - router.push(`${router.route}?${urlParams}`) + redirect: (router.query?.next || '/') as string, + }); + router.push(`${router.route}?${urlParams}`); } const toastProps: UseToastOptions = { duration: 5000, - isClosable: true - } + isClosable: true, + position: 'top', + }; switch (response?.reason) { case 'EMAIL_USED': toast({ ...toastProps, title: 'Email sudah digunakan', - status: 'warning' - }) + status: 'warning', + }); break; case 'NOT_ACTIVE': - const activationUrl = `${router.route}?activation=email` + const activationUrl = `${router.route}?activation=email`; toast({ ...toastProps, title: 'Akun belum aktif', - description: <>Akun sudah terdaftar namun belum aktif. Klik untuk aktivasi akun, - status: 'warning' - }) - break + description: ( + <> + Akun sudah terdaftar namun belum aktif.{' '} + + Klik untuk aktivasi akun + + + ), + status: 'warning', + }); + break; } - } - + }; return ( -
- +
- + = ({ type, required, isBisnisRegist=false }) => placeholder='Masukan nama lengkap anda' value={form.name} onChange={handleInputChange} - aria-invalid={!!errors.name} + aria-invalid={chekValid && !!errors.name} /> - {!!errors.name && {errors.name}} + {chekValid && !!errors.name && ( + {errors.name} + )}
- + = ({ type, required, isBisnisRegist=false }) => placeholder='Masukan alamat email anda' value={form.email} onChange={handleInputChange} - autoComplete="username" - aria-invalid={!!errors.email} + autoComplete='username' + aria-invalid={chekValid && !!errors.email} /> - {!!errors.email && {errors.email}} + {chekValid && !!errors.email && ( + {errors.email} + )}
- +
- + = ({ type, required, isBisnisRegist=false }) => placeholder='••••••••••••' value={form.password} onChange={handleInputChange} - autoComplete="current-password" - aria-invalid={!!errors.password} + autoComplete='current-password' + aria-invalid={chekValid && !!errors.password} /> - {!!errors.password && {errors.password}} + {chekValid && !!errors.password && ( + {errors.password} + )}
- + = ({ type, required, isBisnisRegist=false }) => placeholder='08xxxxxxxx' value={form.phone} onChange={handleInputChange} - aria-invalid={!!errors.phone} + aria-invalid={chekValid && !!errors.phone} /> - {!!errors.phone && {errors.phone}} + {chekValid && !!errors.phone && ( + {errors.phone} + )}
- - - -
- ) -} + ); +}; -export default Form \ No newline at end of file +export default Form; -- cgit v1.2.3