From fc1aeeebb153a33a9e1f940b4564cff76326affc Mon Sep 17 00:00:00 2001 From: Anand Suthar Date: Tue, 4 Jun 2024 16:00:50 +0530 Subject: [PATCH] Disabled login button untill inputs are filled --- app/(pages)/(auth)/login/page.tsx | 38 ++++++++++++------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/app/(pages)/(auth)/login/page.tsx b/app/(pages)/(auth)/login/page.tsx index 6a37676b..8dce779b 100644 --- a/app/(pages)/(auth)/login/page.tsx +++ b/app/(pages)/(auth)/login/page.tsx @@ -1,5 +1,5 @@ "use client"; -import { useState, type ChangeEvent, useRef } from "react"; +import { useState, type ChangeEvent, useRef, useEffect } from "react"; import { AiTwotoneEye, AiOutlineEyeInvisible } from "react-icons/ai"; import { carouselData, roles } from "@constants/index"; import { Carousel, OtpSection } from "@components/index"; @@ -27,9 +27,12 @@ export default function Login() { const [Error, setError] = useState(null || String); const [showOtp, setShowOtp] = useState(false); const [userData, setUserData] = useState({ email: "", role: "", action: "" }); + const emailRef = useRef(null); const passwordRef = useRef(null); + const [loginDisabled, setLoginDisabled] = useState(true); + function handleEmailChange(e: ChangeEvent) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; const isValidEmail = emailRegex.test(e.target.value); @@ -76,6 +79,14 @@ export default function Login() { const toggleVisibility = () => setIsVisible(!isVisible); + useEffect(() => { + setLoginDisabled(isLoginDisabled()); + }, [emailError, email, passwordError, password]); + + function isLoginDisabled(): boolean { + return !!emailError || !email || !!passwordError || !password; + } + async function handleFormSubmit( e: React.FormEvent ): Promise { @@ -214,33 +225,12 @@ export default function Login() { {/* passing the user data to otp section */} {showOtp && } - setError("")} - placement="top-center" - > - - {(onClose) => ( - <> - -

- {Error} -

- for-wrong-password-use -
- - )} -
-
+