From b3aae1d173a3c580735aad4570bd7cd8fa616469 Mon Sep 17 00:00:00 2001 From: sina_sajjadi Date: Tue, 17 Sep 2024 10:19:03 +0330 Subject: [PATCH] Type Definitions completed --- src/app/(account-pages)/account/page.tsx | 8 ++-- src/app/signup/otp-code/page.tsx | 27 ++++-------- src/app/signup/page.tsx | 53 +++++++++++------------- src/hooks/FormValidation.ts | 20 ++++++--- src/shared/Button.tsx | 2 +- 5 files changed, 52 insertions(+), 58 deletions(-) diff --git a/src/app/(account-pages)/account/page.tsx b/src/app/(account-pages)/account/page.tsx index 1a22ace..32a856f 100644 --- a/src/app/(account-pages)/account/page.tsx +++ b/src/app/(account-pages)/account/page.tsx @@ -27,12 +27,12 @@ interface LoadingState { } interface APIResponse { - data: { + avatar: string; email: string; fullname: string; phone_number: string; - }; + } const AccountPage: FC = () => { @@ -133,12 +133,12 @@ const AccountPage: FC = () => { }; const handleFileChange = async (e: ChangeEvent): Promise => { - const file = e.target.files?.[0]; + const file : File | undefined = e.target.files?.[0]; if (file) { const uploadedFile = await getImageURL(file); setImageURL(uploadedFile.url); + setImage(file); } - setImage(file); }; return ( diff --git a/src/app/signup/otp-code/page.tsx b/src/app/signup/otp-code/page.tsx index c5343a3..f8a0f1e 100644 --- a/src/app/signup/otp-code/page.tsx +++ b/src/app/signup/otp-code/page.tsx @@ -4,15 +4,14 @@ import React, { FC, useContext, useState, useEffect, useRef } from "react"; import ButtonPrimary from "@/shared/ButtonPrimary"; import axiosInstance from "@/components/api/axios"; import { useUserContext } from "@/components/contexts/userContext"; -import useFormValidation from "@/hooks/FormValidation"; import { useRouter } from "next/navigation"; export interface PageSignUpProps {} const PageSignUp: FC = () => { const router = useRouter(); - const { form } = useUserContext(); - const { user, setUser } = useUserContext(); + const { form, user, setUser } = useUserContext(); + useEffect(() => { if (Object.keys(user).length) { router.replace("/"); @@ -21,7 +20,7 @@ const PageSignUp: FC = () => { const [otp, setOtp] = useState(["", "", "", "", ""]); const [time, setTime] = useState(30); - const [error, setError] = useState(""); + const [error, setError] = useState(""); const otpRefs = useRef<(HTMLInputElement | null)[]>([]); const handleOtpChange = (value: string, index: number) => { @@ -46,10 +45,7 @@ const PageSignUp: FC = () => { useEffect(() => { if (time > 0) { - const timer = setInterval( - () => setTime((prevTime) => prevTime - 1), - 1000 - ); + const timer = setInterval(() => setTime((prevTime) => prevTime - 1), 1000); return () => clearInterval(timer); } }, [time]); @@ -57,7 +53,7 @@ const PageSignUp: FC = () => { const handleResend = () => { if (time === 0) { setTime(30); - // Add logic to resend OTP here if needed + // Logic to resend OTP can be added here } }; @@ -75,7 +71,7 @@ const PageSignUp: FC = () => { } else { setError("Something went wrong. Please try again."); } - } catch (error) { + } catch (error: any) { // Cast to 'any' or a specific error type setError(error.response?.data?.message || "An error occurred."); } }; @@ -87,8 +83,7 @@ const PageSignUp: FC = () => { Verification Code

- Enter the 5-digit code that we sent to complete your account - registration + Enter the 5-digit code that we sent to complete your account registration

@@ -108,17 +103,13 @@ const PageSignUp: FC = () => {

Haven't got the confirmation code yet?{" "} - {time > 0 && ( - ({time} Seconds) - )} + {time > 0 && ({time} Seconds)}

{error &&

{error}

} = () => { const router = useRouter(); - const {user} = useUserContext() + const { user, setForm, setMethod } = useUserContext(); - if (Object.keys(user).length) { - router.replace("/"); - return null; - } + // Redirect if user is already logged in + useEffect(() => { + if (Object.keys(user).length) { + router.replace("/"); + } + }, [user, router]); - const { setForm, setMethod } = useUserContext(); - const [name, setName] = useState(""); - const [countryCode, setCountryCode] = useState(""); - const [phoneNumber, setPhoneNumber] = useState(""); - const [password, setPassword] = useState(""); - const [confirmPassword, setConfirmPassword] = useState(""); - const [loading, setLoading] = useState(false); - const [failed , setFailed ] =useState("") + const [name, setName] = useState(""); + const [countryCode, setCountryCode] = useState(""); + const [phoneNumber, setPhoneNumber] = useState(""); + const [password, setPassword] = useState(""); + const [confirmPassword, setConfirmPassword] = useState(""); + const [loading, setLoading] = useState(false); + const [failed, setFailed] = useState(""); const { errors, validateForm } = useFormValidation(); - const countryCodeHandler = (e) => { + const countryCodeHandler = (e: React.ChangeEvent) => { if (e.target.value.length <= 3) { setCountryCode(e.target.value); } @@ -46,7 +47,7 @@ const PageSignUp: FC = () => { phoneNumber, password, confirmPassword, - verification_methodes : "" , + verification_methodes: "", method: "register", }; @@ -57,18 +58,16 @@ const PageSignUp: FC = () => { const response = await axiosInstance.get( `/api/account/verfication/?range_phone=${countryCode}&phone_number=${phoneNumber}` ); - form.verification_methodes = response.data.verification_method ; + form.verification_methodes = response.data.verification_method; router.push("/signup/methodes"); - } catch (error) { + } catch (error: any) { console.error("Error fetching data:", error); - setFailed(error.message) - + setFailed(error.message || "An error occurred."); } finally { setLoading(false); } } else { console.log("Form has errors:", errors); - } }; @@ -92,7 +91,7 @@ const PageSignUp: FC = () => { {errors.name &&

{errors.name}

} @@ -135,16 +132,14 @@ const PageSignUp: FC = () => { setConfirmPassword(e.target.value)} - placeholder="Password" - type="password" // Changed to password type for security + type="password" className="secure-input mt-1" - onCopy={(e) => e.preventDefault()} /> {errors.confirmPassword && (

{errors.confirmPassword}

)} - {failed && (

{failed}

)} + {failed &&

{failed}

} { - const [errors, setErrors] = useState({}); + const [errors, setErrors] = useState>({}); - const validateForm = (form) => { - let newErrors = {}; + const validateForm = (form: SignUpForm) => { + let newErrors: Record = {}; if (!form.name) { newErrors.name = 'Full Name is required'; @@ -36,7 +44,7 @@ const useFormValidation = () => { return Object.keys(newErrors).length === 0; }; - return { errors, validateForm }; + return { errors, validateForm, setErrors }; // Optionally return setErrors }; export default useFormValidation; diff --git a/src/shared/Button.tsx b/src/shared/Button.tsx index ed534e9..3f5aa71 100644 --- a/src/shared/Button.tsx +++ b/src/shared/Button.tsx @@ -15,7 +15,7 @@ export interface ButtonProps { type?: ButtonHTMLAttributes["type"]; href?: Route; targetBlank?: boolean; - onClick?: () => void; + onClick?: (e: React.MouseEvent) => void; children?: React.ReactNode; }