diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 8c515c9..73098ea 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -486,6 +486,7 @@ "text-inactive-shops": "Inactive/New shops", "text-product-management": "Product management", "text-all-products": "All Product", + "text-create-products": "Create Product", "text-new-products": "Add new product", "text-my-draft": "My Draft", "text-my-draft-products": "My Draft products", diff --git a/src/components/auth/registration-form.tsx b/src/components/auth/registration-form.tsx index 83a2fe2..482fc00 100644 --- a/src/components/auth/registration-form.tsx +++ b/src/components/auth/registration-form.tsx @@ -3,7 +3,7 @@ import Button from '@/components/ui/button'; import Input from '@/components/ui/input'; import PasswordInput from '@/components/ui/password-input'; import { useRouter } from 'next/router'; -import { useState } from 'react'; +import { useState, useRef, useEffect } from 'react'; import { useForm } from 'react-hook-form'; import { Routes } from '@/config/routes'; import { useTranslation } from 'next-i18next'; @@ -17,8 +17,6 @@ import { } from '@/utils/auth-utils'; import { Permission } from '@/types'; import { useOTPMutation, useRegisterMutation } from '@/data/user'; -import { useRef } from 'react'; -import { useEffect } from 'react'; type FormValues = { fullname: string; @@ -28,15 +26,16 @@ type FormValues = { password_confirmation: string; user_type: 'merchant'; }; + const registrationFormSchema = yup.object().shape({ - fullname: yup.string().required('Full name is required'), - phone_number: yup.string().required('Phone number is required'), - range_phone: yup.string().required('Country code is required'), - password: yup.string().required('Password is required'), + fullname: yup.string().required('form:error-fullname-required'), + phone_number: yup.string().required('form:error-phone-required'), + range_phone: yup.string().required('form:error-country-code-required'), + password: yup.string().required('form:error-password-required'), password_confirmation: yup .string() - .oneOf([yup.ref('password')], 'Passwords must match') - .required('Confirm password is required'), + .oneOf([yup.ref('password')], 'form:error-passwords-must-match') + .required('form:error-confirm-password-required'), permission: yup.string().default('store_owner').oneOf(['store_owner']), }); @@ -46,7 +45,7 @@ const RegistrationForm = () => { const { mutate: confirmUser, isLoading: OTPloading } = useOTPMutation(); const [stage, setStage] = useState('signUp'); // Stage management const [otp, setOtp] = useState(['', '', '', '', '']); // OTP state - const [time, setTime] = useState(3); // Timer for OTP resend + const [time, setTime] = useState(30); // Timer for OTP resend const otpRefs = useRef<(HTMLInputElement | null)[]>([]); // Refs for OTP input focus const firstOtpRef = useRef(null); @@ -77,14 +76,13 @@ const RegistrationForm = () => { } }, [stage]); - async function onSubmit({ + const onSubmit = ({ fullname, phone_number, range_phone, password, password_confirmation, - }: FormValues) { - + }: FormValues) => { if (stage === 'signUp') { registerUser( { @@ -98,26 +96,33 @@ const RegistrationForm = () => { }, { onSuccess: (data) => { - - // if (data?.token) { setStage('OTP'); // Transition to OTP stage on successful registration - // } else { - // setErrorMessage('form:error-credential-wrong'); - // } }, onError: (error: any) => { - Object.keys(error?.response?.data).forEach((field: any) => { - setError(field, { - type: 'manual', - message: error?.response?.data[field], + // Handle field-specific errors + if (error?.response?.data) { + Object.keys(error.response.data).forEach((field: any) => { + setError(field, { + type: 'manual', + message: error.response.data[field], + }); }); - }); + + // Set general error message if available + if (error.response.data.message) { + setErrorMessage(error.response.data.message); + } else { + setErrorMessage(t('form:error-general')); + } + } else { + setErrorMessage(t('form:error-general')); + } }, }, ); } + if (stage === 'OTP') { - confirmUser( { method: 'register', @@ -127,27 +132,33 @@ const RegistrationForm = () => { }, { onSuccess: (data) => { - // if (data?.token) { - setAuthCredentials(data.token) + setAuthCredentials(data.token); router.replace(Routes.dashboard); - // } else { - // setErrorMessage('form:error-credential-wrong'); - // } }, onError: (error: any) => { - console.log(error); - - Object.keys(error?.response?.data).forEach((field: any) => { - setError(field, { - type: 'manual', - message: error?.response?.data[field], + // Handle field-specific errors + if (error?.response?.data) { + Object.keys(error.response.data).forEach((field: any) => { + setError(field, { + type: 'manual', + message: error.response.data[field], + }); }); - }); + + // Set general error message if available + if (error.response.data.message) { + setErrorMessage(error.response.data.message); + } else { + setErrorMessage(t('form:error-general')); + } + } else { + setErrorMessage(t('form:error-general')); + } }, }, ); } - } + }; const handleOtpChange = (value: string, index: number) => { if (/^[0-9]?$/.test(value)) { @@ -171,7 +182,7 @@ const RegistrationForm = () => { const handleOTPSubmit = () => { if (!otp.join('').trim()) { - setErrorMessage('Please enter the OTP'); + setErrorMessage(t('form:error-otp-required')); return; } @@ -190,9 +201,9 @@ const RegistrationForm = () => { className="mb-4" error={t(errors?.fullname?.message!)} /> -