feat: enhance login form with error handling and disable state

This commit is contained in:
Ardeman 2025-02-28 09:10:45 +08:00
parent e546ebd3dd
commit 0b6b43a951
3 changed files with 54 additions and 40 deletions

View File

@ -1,5 +1,5 @@
import { zodResolver } from '@hookform/resolvers/zod' import { zodResolver } from '@hookform/resolvers/zod'
import { useEffect } from 'react' import { useEffect, useState } from 'react'
import { useFetcher } from 'react-router' import { useFetcher } from 'react-router'
import { RemixFormProvider, useRemixForm } from 'remix-hook-form' import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
import { z } from 'zod' import { z } from 'zod'
@ -30,6 +30,8 @@ export const FormLogin = (properties: TProperties) => {
setIsInitSubscribeOpen, setIsInitSubscribeOpen,
} = properties } = properties
const fetcher = useFetcher() const fetcher = useFetcher()
const [error, setError] = useState<string>()
const [disabled, setDisabled] = useState(false)
const formMethods = useRemixForm<TLoginSchema>({ const formMethods = useRemixForm<TLoginSchema>({
mode: 'onSubmit', mode: 'onSubmit',
@ -39,11 +41,21 @@ export const FormLogin = (properties: TProperties) => {
const { handleSubmit } = formMethods const { handleSubmit } = formMethods
useEffect(() => {
if (fetcher.state !== 'idle' || fetcher.data?.success) {
setDisabled(true)
return
}
setDisabled(false)
}, [fetcher])
useEffect(() => { useEffect(() => {
if (fetcher.data?.success) { if (fetcher.data?.success) {
setIsInitSubscribeOpen(true) setIsInitSubscribeOpen(true)
setIsLoginOpen(false) setIsLoginOpen(false)
return
} }
setError(fetcher.data?.message)
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetcher]) }, [fetcher])
@ -72,6 +84,10 @@ export const FormLogin = (properties: TProperties) => {
type="password" type="password"
/> />
{error && (
<div className="text-sm text-red-500 capitalize">{error}</div>
)}
{/* Lupa Kata Sandi */} {/* Lupa Kata Sandi */}
<div className="flex items-center justify-between text-sm"> <div className="flex items-center justify-between text-sm">
<span className="text-gray-600">Lupa Kata Sandi?</span> <span className="text-gray-600">Lupa Kata Sandi?</span>
@ -89,6 +105,7 @@ export const FormLogin = (properties: TProperties) => {
{/* Tombol Masuk */} {/* Tombol Masuk */}
<Button <Button
disabled={disabled}
type="submit" type="submit"
className="w-full rounded-md bg-[#2E2F7C] py-2 text-white transition hover:bg-blue-800" className="w-full rounded-md bg-[#2E2F7C] py-2 text-white transition hover:bg-blue-800"
> >

View File

@ -11,7 +11,6 @@ export const HeaderTop = () => {
const fetcher = useFetcher() const fetcher = useFetcher()
return ( return (
<>
<div className="flex h-[60px] items-center justify-between bg-white px-5 align-middle sm:h-[100px] sm:gap-[15px] sm:px-[50px] sm:py-[20px]"> <div className="flex h-[60px] items-center justify-between bg-white px-5 align-middle sm:h-[100px] sm:gap-[15px] sm:px-[50px] sm:py-[20px]">
<Link <Link
to="/news" to="/news"
@ -54,6 +53,5 @@ export const HeaderTop = () => {
)} )}
</div> </div>
</div> </div>
</>
) )
} }

View File

@ -37,7 +37,6 @@ export const action = async ({ request }: Route.ActionArgs) => {
return data( return data(
{ {
success: true, success: true,
accessToken: token,
}, },
{ {
headers, headers,