fix: replace disabled state with loading state for buttons in upload and category forms

This commit is contained in:
Ardeman 2025-03-11 06:13:03 +08:00
parent 66bc731b07
commit 6ec8e5f2a2
5 changed files with 13 additions and 22 deletions

View File

@ -1,10 +1,11 @@
import { Button, Input } from '@headlessui/react' import { Input } from '@headlessui/react'
import { zodResolver } from '@hookform/resolvers/zod' import { zodResolver } from '@hookform/resolvers/zod'
import { useEffect, useState, type ChangeEvent } from 'react' import { useEffect, useState, type ChangeEvent } 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'
import { Button } from '~/components/ui/button'
import { uploadCategorySchema, useAdminContext } from '~/contexts/admin' import { uploadCategorySchema, useAdminContext } from '~/contexts/admin'
export const uploadSchema = z.object({ export const uploadSchema = z.object({
@ -17,7 +18,6 @@ export type TUploadSchema = z.infer<typeof uploadSchema>
export const FormUpload = () => { export const FormUpload = () => {
const { isUploadOpen, setUploadedFile } = useAdminContext() const { isUploadOpen, setUploadedFile } = useAdminContext()
const fetcher = useFetcher() const fetcher = useFetcher()
const [disabled, setDisabled] = useState(false)
const [error, setError] = useState<string>() const [error, setError] = useState<string>()
const maxFileSize = 10 * 1024 // 10MB const maxFileSize = 10 * 1024 // 10MB
@ -32,13 +32,11 @@ export const FormUpload = () => {
useEffect(() => { useEffect(() => {
if (!fetcher.data?.success) { if (!fetcher.data?.success) {
setError(fetcher.data?.message) setError(fetcher.data?.message)
setDisabled(false)
return return
} }
setUploadedFile(fetcher.data.uploadData.data.file_url) setUploadedFile(fetcher.data.uploadData.data.file_url)
setDisabled(true)
setError(undefined) setError(undefined)
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetcher]) }, [fetcher])
@ -110,9 +108,10 @@ export const FormUpload = () => {
{...register('category')} {...register('category')}
/> />
<Button <Button
disabled={disabled} disabled={fetcher.state !== 'idle'}
isLoading={fetcher.state !== 'idle'}
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 py-2"
> >
Upload Upload
</Button> </Button>

View File

@ -39,7 +39,6 @@ export const FormCategoryPage = (properties: TProperties) => {
}, },
}) })
const [error, setError] = useState<string>() const [error, setError] = useState<string>()
const [disabled, setDisabled] = useState(false)
const { handleSubmit, watch, setValue } = formMethods const { handleSubmit, watch, setValue } = formMethods
const watchName = watch('name') const watchName = watch('name')
@ -47,11 +46,9 @@ export const FormCategoryPage = (properties: TProperties) => {
useEffect(() => { useEffect(() => {
if (!fetcher.data?.success) { if (!fetcher.data?.success) {
setError(fetcher.data?.message) setError(fetcher.data?.message)
setDisabled(false)
return return
} }
navigate('/lg-admin/categories') navigate('/lg-admin/categories')
setDisabled(true)
setError(undefined) setError(undefined)
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetcher]) }, [fetcher])
@ -96,7 +93,8 @@ export const FormCategoryPage = (properties: TProperties) => {
containerClassName="flex-1" containerClassName="flex-1"
/> />
<Button <Button
disabled={disabled} isLoading={fetcher.state !== 'idle'}
disabled={fetcher.state !== 'idle'}
type="submit" type="submit"
size="lg" size="lg"
className="text-md h-[42px] rounded-md" className="text-md h-[42px] rounded-md"

View File

@ -73,7 +73,6 @@ export const FormContentsPage = (properties: TProperties) => {
const { categoriesData: categories } = loaderData || {} const { categoriesData: categories } = loaderData || {}
const { tagsData: tags } = loaderData || {} const { tagsData: tags } = loaderData || {}
const [error, setError] = useState<string>() const [error, setError] = useState<string>()
const [disabled, setDisabled] = useState(false)
const formMethods = useRemixForm<TContentSchema>({ const formMethods = useRemixForm<TContentSchema>({
mode: 'onSubmit', mode: 'onSubmit',
@ -100,12 +99,10 @@ export const FormContentsPage = (properties: TProperties) => {
useEffect(() => { useEffect(() => {
if (!fetcher.data?.success) { if (!fetcher.data?.success) {
setError(fetcher.data?.message) setError(fetcher.data?.message)
setDisabled(false)
return return
} }
navigate('/lg-admin/contents') navigate('/lg-admin/contents')
setDisabled(true)
setError(undefined) setError(undefined)
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetcher]) }, [fetcher])
@ -144,7 +141,8 @@ export const FormContentsPage = (properties: TProperties) => {
containerClassName="flex-1" containerClassName="flex-1"
/> />
<Button <Button
disabled={disabled} isLoading={fetcher.state !== 'idle'}
disabled={fetcher.state !== 'idle'}
type="submit" type="submit"
size="lg" size="lg"
className="text-md h-[42px] rounded-md" className="text-md h-[42px] rounded-md"

View File

@ -34,7 +34,6 @@ export const FormSubscribePlanPage = (properties: TProperties) => {
}, },
}) })
const [error, setError] = useState<string>() const [error, setError] = useState<string>()
const [disabled, setDisabled] = useState(false)
const { handleSubmit, watch, setValue } = formMethods const { handleSubmit, watch, setValue } = formMethods
const watchName = watch('name') const watchName = watch('name')
@ -42,11 +41,9 @@ export const FormSubscribePlanPage = (properties: TProperties) => {
useEffect(() => { useEffect(() => {
if (!fetcher.data?.success) { if (!fetcher.data?.success) {
setError(fetcher.data?.message) setError(fetcher.data?.message)
setDisabled(false)
return return
} }
navigate('/lg-admin/subscribe-plan') navigate('/lg-admin/subscribe-plan')
setDisabled(true)
setError(undefined) setError(undefined)
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetcher]) }, [fetcher])
@ -93,7 +90,8 @@ export const FormSubscribePlanPage = (properties: TProperties) => {
containerClassName="flex-1" containerClassName="flex-1"
/> />
<Button <Button
disabled={disabled} disabled={fetcher.state !== 'idle'}
isLoading={fetcher.state !== 'idle'}
type="submit" type="submit"
size="lg" size="lg"
className="text-md h-[42px] rounded-md" className="text-md h-[42px] rounded-md"

View File

@ -35,7 +35,6 @@ export const FormTagPage = (properties: TProperties) => {
}, },
}) })
const [error, setError] = useState<string>() const [error, setError] = useState<string>()
const [disabled, setDisabled] = useState(false)
const { handleSubmit, watch, setValue } = formMethods const { handleSubmit, watch, setValue } = formMethods
const watchName = watch('name') const watchName = watch('name')
@ -43,11 +42,9 @@ export const FormTagPage = (properties: TProperties) => {
useEffect(() => { useEffect(() => {
if (!fetcher.data?.success) { if (!fetcher.data?.success) {
setError(fetcher.data?.message) setError(fetcher.data?.message)
setDisabled(false)
return return
} }
navigate('/lg-admin/tags') navigate('/lg-admin/tags')
setDisabled(true)
setError(undefined) setError(undefined)
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetcher]) }, [fetcher])
@ -92,7 +89,8 @@ export const FormTagPage = (properties: TProperties) => {
containerClassName="flex-1" containerClassName="flex-1"
/> />
<Button <Button
disabled={disabled} isLoading={fetcher.state !== 'idle'}
disabled={fetcher.state !== 'idle'}
type="submit" type="submit"
size="lg" size="lg"
className="text-md h-[42px] rounded-md" className="text-md h-[42px] rounded-md"