2025-03-07 05:53:52 +07:00
|
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
2025-03-12 20:24:54 +08:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
|
import toast from 'react-hot-toast'
|
2025-03-07 19:31:37 +07:00
|
|
|
import { useFetcher, useNavigate } from 'react-router'
|
2025-03-07 05:53:52 +07:00
|
|
|
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
|
|
|
|
|
import { z } from 'zod'
|
|
|
|
|
|
2025-03-08 01:32:25 +08:00
|
|
|
import type { TCategoryResponse } from '~/apis/common/get-categories'
|
2025-03-07 05:53:52 +07:00
|
|
|
import { Button } from '~/components/ui/button'
|
|
|
|
|
import { Input } from '~/components/ui/input'
|
|
|
|
|
import { TitleDashboard } from '~/components/ui/title-dashboard'
|
2025-03-07 19:31:37 +07:00
|
|
|
import { urlFriendlyCode } from '~/utils/formatter'
|
2025-03-07 05:53:52 +07:00
|
|
|
|
2025-03-12 09:47:21 +08:00
|
|
|
export const categorySchema = z.object({
|
2025-03-08 01:58:59 +08:00
|
|
|
id: z.string().optional(),
|
2025-03-07 05:53:52 +07:00
|
|
|
name: z.string().min(3, 'Nama minimal 3 karakter'),
|
2025-03-08 01:58:59 +08:00
|
|
|
code: z.string(),
|
|
|
|
|
sequence: z.preprocess(Number, z.number().optional()),
|
|
|
|
|
description: z.string(),
|
2025-03-07 05:53:52 +07:00
|
|
|
})
|
2025-03-12 09:47:21 +08:00
|
|
|
export type TCategorySchema = z.infer<typeof categorySchema>
|
2025-03-08 01:32:25 +08:00
|
|
|
type TProperties = {
|
|
|
|
|
categoryData?: TCategoryResponse
|
|
|
|
|
}
|
2025-03-07 05:53:52 +07:00
|
|
|
|
2025-03-08 01:32:25 +08:00
|
|
|
export const FormCategoryPage = (properties: TProperties) => {
|
|
|
|
|
const { categoryData } = properties || {}
|
2025-03-07 05:53:52 +07:00
|
|
|
const fetcher = useFetcher()
|
2025-03-07 19:31:37 +07:00
|
|
|
const navigate = useNavigate()
|
2025-03-07 05:53:52 +07:00
|
|
|
const formMethods = useRemixForm<TCategorySchema>({
|
|
|
|
|
mode: 'onSubmit',
|
|
|
|
|
fetcher,
|
2025-03-12 09:47:21 +08:00
|
|
|
resolver: zodResolver(categorySchema),
|
2025-03-08 01:32:25 +08:00
|
|
|
values: {
|
2025-03-08 01:58:59 +08:00
|
|
|
id: categoryData?.id || undefined,
|
2025-03-08 01:32:25 +08:00
|
|
|
code: categoryData?.code || '',
|
|
|
|
|
name: categoryData?.name || '',
|
2025-03-14 13:28:15 +08:00
|
|
|
sequence: categoryData?.sequence ?? undefined,
|
2025-03-08 01:58:59 +08:00
|
|
|
description: categoryData?.description || '',
|
2025-03-08 01:32:25 +08:00
|
|
|
},
|
2025-03-07 05:53:52 +07:00
|
|
|
})
|
|
|
|
|
|
2025-03-08 01:32:25 +08:00
|
|
|
const { handleSubmit, watch, setValue } = formMethods
|
|
|
|
|
const watchName = watch('name')
|
2025-03-07 05:53:52 +07:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-03-15 09:16:57 +08:00
|
|
|
if (!fetcher.data?.success && fetcher.data?.message) {
|
|
|
|
|
toast.error(fetcher.data.message)
|
2025-03-12 20:24:54 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-15 09:16:57 +08:00
|
|
|
if (fetcher.data?.success) {
|
2025-03-12 20:24:54 +08:00
|
|
|
toast.success(
|
|
|
|
|
`Kategori berhasil ${categoryData ? 'diupdate' : 'dibuat'}!`,
|
|
|
|
|
)
|
|
|
|
|
navigate('/lg-admin/categories')
|
2025-03-07 05:53:52 +07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2025-03-12 20:24:54 +08:00
|
|
|
}, [fetcher.data])
|
2025-03-07 05:53:52 +07:00
|
|
|
|
2025-03-08 01:32:25 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
setValue('code', urlFriendlyCode(watchName))
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [watchName])
|
2025-03-07 05:53:52 +07:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="relative">
|
2025-03-08 01:32:25 +08:00
|
|
|
<TitleDashboard title={`${categoryData ? 'Update' : 'Buat'} Kategori`} />
|
2025-03-07 05:53:52 +07:00
|
|
|
<div>
|
|
|
|
|
<RemixFormProvider {...formMethods}>
|
|
|
|
|
<fetcher.Form
|
|
|
|
|
method="post"
|
|
|
|
|
onSubmit={handleSubmit}
|
2025-03-08 01:58:59 +08:00
|
|
|
action={`/actions/admin/categories/${categoryData ? 'update' : 'create'}`}
|
2025-03-07 05:53:52 +07:00
|
|
|
className="space-y-4"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex items-end justify-between gap-4">
|
|
|
|
|
<Input
|
|
|
|
|
id="name"
|
2025-03-08 01:32:25 +08:00
|
|
|
label="Kategori"
|
|
|
|
|
placeholder="Masukkan Nama Kategori"
|
2025-03-07 05:53:52 +07:00
|
|
|
name="name"
|
2025-03-08 01:32:25 +08:00
|
|
|
className="border-0 bg-white shadow read-only:bg-gray-100 focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none disabled:bg-gray-100"
|
2025-03-07 05:53:52 +07:00
|
|
|
labelClassName="text-sm font-medium text-[#363636]"
|
|
|
|
|
containerClassName="flex-1"
|
2025-03-14 13:28:15 +08:00
|
|
|
readOnly={categoryData?.code === 'spotlight'}
|
2025-03-07 05:53:52 +07:00
|
|
|
/>
|
|
|
|
|
<Input
|
|
|
|
|
id="code"
|
2025-03-08 01:32:25 +08:00
|
|
|
label="Kode"
|
|
|
|
|
placeholder="Masukkan Kode Kategori"
|
2025-03-07 05:53:52 +07:00
|
|
|
readOnly
|
|
|
|
|
name="code"
|
2025-03-08 01:32:25 +08:00
|
|
|
className="border-0 bg-white shadow read-only:bg-gray-100 focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none disabled:bg-gray-100"
|
2025-03-07 05:53:52 +07:00
|
|
|
labelClassName="text-sm font-medium text-[#363636]"
|
|
|
|
|
containerClassName="flex-1"
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
2025-03-11 06:13:03 +08:00
|
|
|
isLoading={fetcher.state !== 'idle'}
|
|
|
|
|
disabled={fetcher.state !== 'idle'}
|
2025-03-07 05:53:52 +07:00
|
|
|
type="submit"
|
|
|
|
|
size="lg"
|
|
|
|
|
className="text-md h-[42px] rounded-md"
|
|
|
|
|
>
|
|
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2025-03-08 01:58:59 +08:00
|
|
|
<div className="flex items-end justify-between gap-4">
|
|
|
|
|
<Input
|
|
|
|
|
id="sequence"
|
|
|
|
|
label="Urutan"
|
|
|
|
|
placeholder="Urutan Kategori"
|
|
|
|
|
name="sequence"
|
|
|
|
|
type="number"
|
|
|
|
|
className="border-0 bg-white shadow read-only:bg-gray-100 focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none disabled:bg-gray-100"
|
|
|
|
|
labelClassName="text-sm font-medium text-[#363636]"
|
|
|
|
|
containerClassName="w-44"
|
2025-03-14 13:28:15 +08:00
|
|
|
readOnly={categoryData?.code === 'spotlight'}
|
2025-03-08 01:58:59 +08:00
|
|
|
/>
|
|
|
|
|
<Input
|
|
|
|
|
id="description"
|
|
|
|
|
label="Deskripsi"
|
|
|
|
|
placeholder="Masukkan Deskripsi Kategori"
|
|
|
|
|
name="description"
|
|
|
|
|
className="border-0 bg-white shadow read-only:bg-gray-100 focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none disabled:bg-gray-100"
|
|
|
|
|
labelClassName="text-sm font-medium text-[#363636]"
|
|
|
|
|
containerClassName="flex-1"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-03-07 05:53:52 +07:00
|
|
|
</fetcher.Form>
|
|
|
|
|
</RemixFormProvider>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|