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/index.tsx | 170 ++++++++++++++++++--------------- 1 file changed, 94 insertions(+), 76 deletions(-) (limited to 'src-migrate/modules/register/index.tsx') diff --git a/src-migrate/modules/register/index.tsx b/src-migrate/modules/register/index.tsx index 31e09088..d91af9e3 100644 --- a/src-migrate/modules/register/index.tsx +++ b/src-migrate/modules/register/index.tsx @@ -1,118 +1,130 @@ -import PageContent from "~/modules/page-content"; -import Form from "./components/Form"; -import RegistrasiIndividu from "./components/RegistrasiIndividu"; -import RegistrasiBisnis from "./components/RegistrasiBisnis"; -import FormBisnis from "./components/FormBisnis"; -import Link from "next/link"; -import Image from "next/image"; -import IndoteknikLogo from "~/images/logo.png"; -import AccountActivation from "../account-activation"; -import { useMemo, useState } from "react"; -import { useRegisterStore } from "./stores/useRegisterStore"; -import FormCaptcha from "./components/FormCaptcha"; -import TermCondition from "./components/TermCondition"; -import { Button } from '@chakra-ui/react' -import { useMutation } from "react-query"; -import { UseToastOptions, useToast } from "@chakra-ui/react"; -import { RegisterProps } from "~/types/auth"; -import { registerUser } from "~/services/auth"; -import { useRouter } from "next/router"; +import PageContent from '~/modules/page-content'; +import RegistrasiIndividu from './components/RegistrasiIndividu'; +import RegistrasiBisnis from './components/RegistrasiBisnis'; +import Link from 'next/link'; +import Image from 'next/image'; +import IndoteknikLogo from '~/images/logo.png'; +import AccountActivation from '../account-activation'; +import { useMemo, useState } from 'react'; +import { useRegisterStore } from './stores/useRegisterStore'; +import FormCaptcha from './components/FormCaptcha'; +import TermCondition from './components/TermCondition'; +import { Button } from '@chakra-ui/react'; +import { useMutation } from 'react-query'; +import { UseToastOptions, useToast } from '@chakra-ui/react'; +import { RegisterProps } from '~/types/auth'; +import { registerUser } from '~/services/auth'; +import { useRouter } from 'next/router'; const LOGO_WIDTH = 150; const LOGO_HEIGHT = LOGO_WIDTH / 3; const Register = () => { const [isIndividuClicked, setIsIndividuClicked] = useState(true); + const [notValid, setNotValid] = useState(false); const [isBisnisClicked, setIsBisnisClicked] = useState(false); - const {form, isCheckedTNC, isValidCaptcha, resetForm,errors, - updateForm - } = useRegisterStore() + const { form, isCheckedTNC, isValidCaptcha, resetForm, errors, updateForm } = + useRegisterStore(); - const isFormValid = useMemo(() => Object.keys(errors).length === 0, [errors]) - const toast = useToast() - const router = useRouter() + const isFormValid = useMemo(() => Object.keys(errors).length === 0, [errors]); + const toast = useToast(); + const router = useRouter(); const mutation = useMutation({ - mutationFn: (data: RegisterProps) => registerUser(data) - }) + mutationFn: (data: RegisterProps) => registerUser(data), + }); const handleIndividuClick = () => { resetForm(); setIsIndividuClicked(true); setIsBisnisClicked(false); }; - + const handleBisnisClick = () => { resetForm(); - updateForm("is_terdaftar", 'false') - updateForm("type_acc", 'business') + updateForm('is_terdaftar', 'false'); + updateForm('type_acc', 'business'); setIsIndividuClicked(false); setIsBisnisClicked(true); }; const handleSubmit = async () => { - const response = await mutation.mutateAsync(form) + if (!isFormValid) { + setNotValid(true); + return; + } else { + setNotValid(false); + } + 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 ( -
-
-
-
- +
+
+
+
+ Logo Indoteknik -

+

Daftar Akun Indoteknik

-

+

Buat akun sekarang lebih mudah dan terverifikasi

-