summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorit-fixcomart <it@fixcomart.co.id>2024-09-17 09:01:04 +0700
committerit-fixcomart <it@fixcomart.co.id>2024-09-17 09:01:04 +0700
commitaaf907c834343970e1d30b3ef49c13ed5f9d34ed (patch)
treee22a80c3b9bfc2835ad78c13fc51fcafbc6598a0
parent499954a8be814850103eece2dbd0306d6246989d (diff)
<iman> add focus error when button submit click
-rw-r--r--src-migrate/modules/register/components/Form.tsx43
-rw-r--r--src-migrate/modules/register/components/FormBisnis.tsx11
-rw-r--r--src-migrate/modules/register/components/RegistrasiBisnis.tsx8
-rw-r--r--src-migrate/modules/register/components/RegistrasiIndividu.tsx7
-rw-r--r--src-migrate/modules/register/index.tsx13
-rw-r--r--src/lib/auth/components/CompanyProfile.jsx1
6 files changed, 74 insertions, 9 deletions
diff --git a/src-migrate/modules/register/components/Form.tsx b/src-migrate/modules/register/components/Form.tsx
index 118d9d69..b04e763c 100644
--- a/src-migrate/modules/register/components/Form.tsx
+++ b/src-migrate/modules/register/components/Form.tsx
@@ -1,4 +1,4 @@
-import { ChangeEvent, useMemo } from 'react';
+import { ChangeEvent, useMemo, useEffect, useRef, useState } from 'react';
import { useMutation } from 'react-query';
import { useRegisterStore } from '../stores/useRegisterStore';
import { RegisterProps } from '~/types/auth';
@@ -14,13 +14,15 @@ interface FormProps {
required: boolean;
isBisnisRegist: boolean;
chekValid: boolean;
+ buttonSubmitClick: boolean;
}
const Form: React.FC<FormProps> = ({
type,
required,
isBisnisRegist = false,
- chekValid = false,
+ chekValid,
+ buttonSubmitClick,
}) => {
const { form, isCheckedTNC, isValidCaptcha, errors, updateForm, validate } =
useRegisterStore();
@@ -30,6 +32,11 @@ const Form: React.FC<FormProps> = ({
const router = useRouter();
const toast = useToast();
+ const emailRef = useRef<HTMLInputElement>(null);
+ const nameRef = useRef<HTMLInputElement>(null);
+ const passwordRef = useRef<HTMLInputElement>(null);
+ const teleponRef = useRef<HTMLDivElement>(null);
+
const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
const { name, value } = event.target;
updateForm(name, value);
@@ -40,6 +47,34 @@ const Form: React.FC<FormProps> = ({
mutationFn: (data: RegisterProps) => registerUser(data),
});
+ useEffect(() => {
+ const loadIndustries = async () => {
+ const response = await mutation.mutateAsync(form);
+ if (!response?.register) {
+ // Logic to focus on first invalid input
+ if (errors.email_partner && emailRef.current) {
+ emailRef.current.focus();
+ return;
+ }
+ if (errors.name && nameRef.current) {
+ nameRef.current.focus();
+ return;
+ }
+
+ if (errors.password && passwordRef.current) {
+ passwordRef.current.focus();
+ return;
+ }
+
+ if (errors.phone && teleponRef.current) {
+ teleponRef.current.scrollIntoView();
+ return;
+ }
+ }
+ };
+ loadIndustries();
+ }, [buttonSubmitClick]);
+
const handleSubmit = async (e: ChangeEvent<HTMLFormElement>) => {
e.preventDefault();
@@ -101,6 +136,7 @@ const Form: React.FC<FormProps> = ({
className='form-input mt-3'
placeholder='Masukan nama lengkap anda'
value={form.name}
+ ref={nameRef}
onChange={handleInputChange}
aria-invalid={chekValid && !!errors.name}
/>
@@ -122,6 +158,7 @@ const Form: React.FC<FormProps> = ({
className='form-input mt-3'
placeholder='Masukan alamat email anda'
value={form.email}
+ ref={emailRef}
onChange={handleInputChange}
autoComplete='username'
aria-invalid={chekValid && !!errors.email}
@@ -143,6 +180,7 @@ const Form: React.FC<FormProps> = ({
className='form-input mt-3'
placeholder='••••••••••••'
value={form.password}
+ ref={passwordRef}
onChange={handleInputChange}
autoComplete='current-password'
aria-invalid={chekValid && !!errors.password}
@@ -163,6 +201,7 @@ const Form: React.FC<FormProps> = ({
id='phone'
name='phone'
className='form-input mt-3'
+ ref={passwordRef}
placeholder='08xxxxxxxx'
value={form.phone}
onChange={handleInputChange}
diff --git a/src-migrate/modules/register/components/FormBisnis.tsx b/src-migrate/modules/register/components/FormBisnis.tsx
index dd9cd72f..2ac998c9 100644
--- a/src-migrate/modules/register/components/FormBisnis.tsx
+++ b/src-migrate/modules/register/components/FormBisnis.tsx
@@ -26,6 +26,7 @@ interface FormProps {
required: boolean;
isPKP: boolean;
chekValid: boolean;
+ buttonSubmitClick: boolean;
}
interface industry_id {
@@ -39,7 +40,13 @@ interface companyType {
label: string;
}
-const form: React.FC<FormProps> = ({ type, required, isPKP, chekValid }) => {
+const form: React.FC<FormProps> = ({
+ type,
+ required,
+ isPKP,
+ chekValid,
+ buttonSubmitClick,
+}) => {
const { form, errors, updateForm, validate } = useRegisterStore();
const { control, watch, setValue } = useForm();
const [selectedCategory, setSelectedCategory] = useState<string>('');
@@ -278,7 +285,7 @@ const form: React.FC<FormProps> = ({ type, required, isPKP, chekValid }) => {
}
};
loadIndustries();
- }, [chekValid, form, errors]);
+ }, [buttonSubmitClick]);
const handleSubmit = async (e: ChangeEvent<HTMLFormElement>) => {
e.preventDefault();
diff --git a/src-migrate/modules/register/components/RegistrasiBisnis.tsx b/src-migrate/modules/register/components/RegistrasiBisnis.tsx
index 36476ab9..ce4d3972 100644
--- a/src-migrate/modules/register/components/RegistrasiBisnis.tsx
+++ b/src-migrate/modules/register/components/RegistrasiBisnis.tsx
@@ -16,8 +16,12 @@ import { UseToastOptions, useToast } from '@chakra-ui/react';
import Link from 'next/link';
interface FormProps {
chekValid: boolean;
+ buttonSubmitClick: boolean;
}
-const RegistrasiBisnis: React.FC<FormProps> = ({ chekValid }) => {
+const RegistrasiBisnis: React.FC<FormProps> = ({
+ chekValid,
+ buttonSubmitClick,
+}) => {
const [isPKP, setIsPKP] = useState(true);
const [isTerdaftar, setIsTerdaftar] = useState(false);
const [isDropIndividu, setIsDropIndividu] = useState(true);
@@ -113,6 +117,7 @@ const RegistrasiBisnis: React.FC<FormProps> = ({ chekValid }) => {
required={true}
isBisnisRegist={true}
chekValid={chekValid}
+ buttonSubmitClick={buttonSubmitClick}
/>
</div>
)}
@@ -177,6 +182,7 @@ const RegistrasiBisnis: React.FC<FormProps> = ({ chekValid }) => {
required={isTerdaftar}
isPKP={isPKP}
chekValid={chekValid}
+ buttonSubmitClick={buttonSubmitClick}
/>
</div>
)}
diff --git a/src-migrate/modules/register/components/RegistrasiIndividu.tsx b/src-migrate/modules/register/components/RegistrasiIndividu.tsx
index 3997e767..84049065 100644
--- a/src-migrate/modules/register/components/RegistrasiIndividu.tsx
+++ b/src-migrate/modules/register/components/RegistrasiIndividu.tsx
@@ -3,8 +3,12 @@ import { useRegisterStore } from '../stores/useRegisterStore';
import { useEffect } from 'react';
interface FormProps {
chekValid: boolean;
+ buttonSubmitClick: boolean;
}
-const RegistrasiIndividu: React.FC<FormProps> = ({ chekValid }) => {
+const RegistrasiIndividu: React.FC<FormProps> = ({
+ chekValid,
+ buttonSubmitClick,
+}) => {
const { form, errors, updateForm, validate } = useRegisterStore();
useEffect(() => {
@@ -21,6 +25,7 @@ const RegistrasiIndividu: React.FC<FormProps> = ({ chekValid }) => {
required={false}
isBisnisRegist={false}
chekValid={chekValid}
+ buttonSubmitClick={buttonSubmitClick}
/>
</>
);
diff --git a/src-migrate/modules/register/index.tsx b/src-migrate/modules/register/index.tsx
index d91af9e3..08d7f893 100644
--- a/src-migrate/modules/register/index.tsx
+++ b/src-migrate/modules/register/index.tsx
@@ -22,6 +22,7 @@ const LOGO_HEIGHT = LOGO_WIDTH / 3;
const Register = () => {
const [isIndividuClicked, setIsIndividuClicked] = useState(true);
const [notValid, setNotValid] = useState(false);
+ const [buttonSubmitClick, setButtonSubmitClick] = useState(false);
const [isBisnisClicked, setIsBisnisClicked] = useState(false);
const { form, isCheckedTNC, isValidCaptcha, resetForm, errors, updateForm } =
useRegisterStore();
@@ -49,8 +50,10 @@ const Register = () => {
const handleSubmit = async () => {
if (!isFormValid) {
setNotValid(true);
+ setButtonSubmitClick(!buttonSubmitClick);
return;
} else {
+ setButtonSubmitClick(!buttonSubmitClick);
setNotValid(false);
}
const response = await mutation.mutateAsync(form);
@@ -142,12 +145,18 @@ const Register = () => {
<div className='transition-opacity duration-300 ease-in-out'>
{isIndividuClicked && (
<div className='opacity-100'>
- <RegistrasiIndividu chekValid={notValid} />
+ <RegistrasiIndividu
+ chekValid={notValid}
+ buttonSubmitClick={buttonSubmitClick}
+ />
</div>
)}
{isBisnisClicked && (
<div className='opacity-100'>
- <RegistrasiBisnis chekValid={notValid} />
+ <RegistrasiBisnis
+ chekValid={notValid}
+ buttonSubmitClick={buttonSubmitClick}
+ />
</div>
)}
</div>
diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx
index fc3d149b..70713bdf 100644
--- a/src/lib/auth/components/CompanyProfile.jsx
+++ b/src/lib/auth/components/CompanyProfile.jsx
@@ -52,7 +52,6 @@ const CompanyProfile = () => {
useEffect(() => {
const loadProfile = async () => {
const dataProfile = await addressApi({ id: auth.parentId });
- console.log('dataProfile', dataProfile);
setValue('name', dataProfile.name);
setValue('industry', dataProfile.industryId);
setValue('companyType', dataProfile.companyTypeId);