2025-03-05 17:57:52 +08:00
|
|
|
import { DevTool } from '@hookform/devtools'
|
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
2025-03-12 19:23:46 +08:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
|
import toast from 'react-hot-toast'
|
2025-03-06 05:46:25 +08:00
|
|
|
import { useFetcher, useNavigate, useRouteLoaderData } from 'react-router'
|
2025-03-05 17:57:52 +08:00
|
|
|
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
|
|
|
|
|
import { z } from 'zod'
|
2025-03-02 21:51:50 +07:00
|
|
|
|
2025-03-09 10:23:11 +08:00
|
|
|
import type { newsResponseSchema } from '~/apis/common/get-news'
|
2025-03-05 23:39:09 +08:00
|
|
|
import { TextEditor } from '~/components/text-editor'
|
2025-03-05 17:57:52 +08:00
|
|
|
import { Button } from '~/components/ui/button'
|
|
|
|
|
import { Combobox } from '~/components/ui/combobox'
|
|
|
|
|
import { Input } from '~/components/ui/input'
|
2025-03-09 15:14:57 +08:00
|
|
|
import { InputFile } from '~/components/ui/input-file'
|
2025-03-06 08:18:36 +08:00
|
|
|
import { Switch } from '~/components/ui/switch'
|
2025-03-02 21:51:50 +07:00
|
|
|
import { TitleDashboard } from '~/components/ui/title-dashboard'
|
2025-03-09 09:57:26 +08:00
|
|
|
import type { loader } from '~/routes/_admin.lg-admin._dashboard'
|
2025-03-14 13:08:48 +08:00
|
|
|
import { dateInput } from '~/utils/formatter'
|
2025-03-05 17:57:52 +08:00
|
|
|
|
|
|
|
|
export const contentSchema = z.object({
|
2025-03-07 14:40:04 +08:00
|
|
|
id: z.string().optional(),
|
2025-03-05 17:57:52 +08:00
|
|
|
categories: z
|
|
|
|
|
.array(
|
|
|
|
|
z
|
|
|
|
|
.object({
|
|
|
|
|
id: z.string(),
|
|
|
|
|
code: z.string(),
|
|
|
|
|
name: z.string(),
|
|
|
|
|
})
|
|
|
|
|
.optional()
|
|
|
|
|
.nullable(),
|
|
|
|
|
)
|
2025-03-06 08:47:21 +08:00
|
|
|
.refine((data) => data.length, {
|
2025-03-05 17:57:52 +08:00
|
|
|
message: 'Please select a category',
|
|
|
|
|
}),
|
2025-03-06 05:46:25 +08:00
|
|
|
tags: z
|
|
|
|
|
.array(
|
|
|
|
|
z
|
|
|
|
|
.object({
|
|
|
|
|
id: z.string(),
|
|
|
|
|
code: z.string(),
|
|
|
|
|
name: z.string(),
|
|
|
|
|
})
|
|
|
|
|
.optional()
|
|
|
|
|
.nullable(),
|
|
|
|
|
)
|
|
|
|
|
.optional(),
|
2025-03-05 17:57:52 +08:00
|
|
|
title: z.string().min(1, {
|
2025-03-05 23:39:09 +08:00
|
|
|
message: 'Judul is required',
|
2025-03-05 17:57:52 +08:00
|
|
|
}),
|
|
|
|
|
content: z.string().min(1, {
|
2025-03-05 23:39:09 +08:00
|
|
|
message: 'Konten is required',
|
2025-03-05 17:57:52 +08:00
|
|
|
}),
|
2025-03-09 11:36:47 +08:00
|
|
|
featured_image: z.string().url({
|
|
|
|
|
message: 'Gambar Unggulan must be a valid URL',
|
|
|
|
|
}),
|
2025-03-05 17:57:52 +08:00
|
|
|
is_premium: z.boolean().optional(),
|
|
|
|
|
live_at: z.string().min(1, {
|
|
|
|
|
message: 'Tanggal live is required',
|
|
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export type TContentSchema = z.infer<typeof contentSchema>
|
2025-03-07 13:50:16 +08:00
|
|
|
type TProperties = {
|
|
|
|
|
newsData?: z.infer<typeof newsResponseSchema>
|
|
|
|
|
}
|
2025-03-02 21:51:50 +07:00
|
|
|
|
2025-03-08 00:47:33 +08:00
|
|
|
export const FormContentsPage = (properties: TProperties) => {
|
2025-03-07 13:50:16 +08:00
|
|
|
const { newsData } = properties || {}
|
2025-03-05 17:57:52 +08:00
|
|
|
const fetcher = useFetcher()
|
2025-03-06 05:46:25 +08:00
|
|
|
const navigate = useNavigate()
|
2025-03-09 09:57:26 +08:00
|
|
|
const loaderData = useRouteLoaderData<typeof loader>(
|
|
|
|
|
'routes/_admin.lg-admin._dashboard',
|
|
|
|
|
)
|
2025-03-09 09:32:51 +08:00
|
|
|
const { categoriesData: categories } = loaderData || {}
|
|
|
|
|
const { tagsData: tags } = loaderData || {}
|
2025-03-05 17:57:52 +08:00
|
|
|
|
|
|
|
|
const formMethods = useRemixForm<TContentSchema>({
|
|
|
|
|
mode: 'onSubmit',
|
|
|
|
|
fetcher,
|
|
|
|
|
resolver: zodResolver(contentSchema),
|
2025-03-07 13:50:16 +08:00
|
|
|
values: {
|
2025-03-08 01:58:59 +08:00
|
|
|
id: newsData?.id || undefined,
|
2025-03-07 13:50:16 +08:00
|
|
|
categories: newsData?.categories || [],
|
|
|
|
|
tags: newsData?.tags || [],
|
|
|
|
|
title: newsData?.title || '',
|
|
|
|
|
content: newsData?.content || '',
|
|
|
|
|
featured_image: newsData?.featured_image || '',
|
|
|
|
|
is_premium: newsData?.is_premium || false,
|
2025-03-14 13:08:48 +08:00
|
|
|
live_at: newsData?.live_at ? dateInput(newsData.live_at) : '',
|
2025-03-06 08:18:36 +08:00
|
|
|
},
|
2025-03-05 17:57:52 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const { handleSubmit, control, watch } = formMethods
|
|
|
|
|
const watchCategories = watch('categories')
|
|
|
|
|
const watchTags = watch('tags')
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-03-15 09:16:57 +08:00
|
|
|
if (!fetcher.data?.success && fetcher.data?.message) {
|
|
|
|
|
toast.error(fetcher.data.message)
|
2025-03-12 19:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-15 09:16:57 +08:00
|
|
|
if (fetcher.data?.success) {
|
2025-03-12 19:23:46 +08:00
|
|
|
toast.success(`Artikel berhasil ${newsData ? 'diupdate' : 'dibuat'}!`)
|
|
|
|
|
navigate('/lg-admin/contents')
|
2025-03-05 17:57:52 +08:00
|
|
|
}
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2025-03-12 19:23:46 +08:00
|
|
|
}, [fetcher.data])
|
2025-03-05 17:57:52 +08:00
|
|
|
|
2025-03-02 21:51:50 +07:00
|
|
|
return (
|
|
|
|
|
<div className="relative">
|
2025-03-07 13:53:07 +08:00
|
|
|
<TitleDashboard title={`${newsData ? 'Update' : 'Buat'} Artikel`} />
|
2025-03-05 17:57:52 +08:00
|
|
|
<RemixFormProvider {...formMethods}>
|
|
|
|
|
<fetcher.Form
|
|
|
|
|
method="post"
|
|
|
|
|
onSubmit={handleSubmit}
|
2025-03-07 14:40:04 +08:00
|
|
|
action={`/actions/admin/contents/${newsData ? 'update' : 'create'}`}
|
2025-03-05 17:57:52 +08:00
|
|
|
className="space-y-4"
|
|
|
|
|
>
|
2025-03-05 23:39:09 +08:00
|
|
|
<div className="flex items-end justify-between gap-4">
|
|
|
|
|
<Input
|
|
|
|
|
id="title"
|
|
|
|
|
label="Judul"
|
|
|
|
|
placeholder="Masukkan Judul"
|
|
|
|
|
name="title"
|
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-06 03:53:15 +08:00
|
|
|
labelClassName="text-sm font-medium text-[#363636]"
|
2025-03-05 23:39:09 +08:00
|
|
|
containerClassName="flex-1"
|
2025-03-08 01:32:25 +08:00
|
|
|
disabled={!!newsData}
|
2025-03-05 23:39:09 +08:00
|
|
|
/>
|
2025-03-09 15:14:57 +08:00
|
|
|
<InputFile
|
2025-03-05 23:39:09 +08:00
|
|
|
id="featured_image"
|
|
|
|
|
label="Gambar Unggulan"
|
2025-03-09 14:55:04 +08:00
|
|
|
placeholder="Masukkan Url Gambar Unggulan"
|
2025-03-05 23:39:09 +08:00
|
|
|
name="featured_image"
|
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-06 03:53:15 +08:00
|
|
|
labelClassName="text-sm font-medium text-[#363636]"
|
2025-03-05 23:39:09 +08:00
|
|
|
containerClassName="flex-1"
|
2025-03-12 10:57:48 +08:00
|
|
|
category="featured_image"
|
2025-03-05 23:39:09 +08:00
|
|
|
/>
|
2025-03-06 05:51:03 +08:00
|
|
|
<Button
|
2025-03-11 06:13:03 +08:00
|
|
|
isLoading={fetcher.state !== 'idle'}
|
|
|
|
|
disabled={fetcher.state !== 'idle'}
|
2025-03-06 05:51:03 +08:00
|
|
|
type="submit"
|
|
|
|
|
size="lg"
|
|
|
|
|
className="text-md h-[42px] rounded-md"
|
|
|
|
|
>
|
|
|
|
|
Save
|
|
|
|
|
</Button>
|
2025-03-05 23:39:09 +08:00
|
|
|
</div>
|
2025-03-05 17:57:52 +08:00
|
|
|
<div className="flex items-end justify-between gap-4">
|
|
|
|
|
<Combobox
|
|
|
|
|
multiple
|
|
|
|
|
id="categories"
|
|
|
|
|
name="categories"
|
|
|
|
|
label="Kategori"
|
|
|
|
|
placeholder={
|
|
|
|
|
watchCategories
|
|
|
|
|
? watchCategories.map((category) => category?.name).join(', ')
|
|
|
|
|
: 'Pilih Kategori'
|
|
|
|
|
}
|
|
|
|
|
options={categories}
|
|
|
|
|
className="border-0 bg-white shadow focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none"
|
|
|
|
|
labelClassName="text-sm font-medium text-[#363636]"
|
|
|
|
|
containerClassName="flex-1"
|
|
|
|
|
/>
|
|
|
|
|
<Combobox
|
|
|
|
|
multiple
|
|
|
|
|
id="tags"
|
|
|
|
|
name="tags"
|
|
|
|
|
label="Tags"
|
|
|
|
|
placeholder={
|
|
|
|
|
watchTags
|
|
|
|
|
? watchTags.map((tag) => tag?.name).join(', ')
|
|
|
|
|
: 'Pilih Tags'
|
|
|
|
|
}
|
|
|
|
|
options={tags}
|
|
|
|
|
className="border-0 bg-white shadow focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none"
|
|
|
|
|
labelClassName="text-sm font-medium text-[#363636]"
|
|
|
|
|
containerClassName="flex-1"
|
|
|
|
|
/>
|
|
|
|
|
<Input
|
|
|
|
|
id="live_at"
|
|
|
|
|
label="Tanggal Live"
|
|
|
|
|
placeholder="Pilih Tanggal"
|
|
|
|
|
name="live_at"
|
|
|
|
|
type="date"
|
|
|
|
|
className="border-0 bg-white shadow focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none"
|
2025-03-06 03:53:15 +08:00
|
|
|
labelClassName="text-sm font-medium text-[#363636]"
|
2025-03-05 17:57:52 +08:00
|
|
|
/>
|
2025-03-06 08:18:36 +08:00
|
|
|
<Switch
|
|
|
|
|
id="is_premium"
|
|
|
|
|
name="is_premium"
|
2025-03-07 10:30:46 +08:00
|
|
|
label="Subscription"
|
2025-03-06 08:18:36 +08:00
|
|
|
labelClassName="text-sm font-medium text-[#363636]"
|
2025-03-06 09:01:26 +08:00
|
|
|
className="h-[42px]"
|
2025-03-07 10:30:46 +08:00
|
|
|
options={{ true: 'Premium', false: 'Normal' }}
|
2025-03-06 08:18:36 +08:00
|
|
|
/>
|
2025-03-05 17:57:52 +08:00
|
|
|
</div>
|
|
|
|
|
|
2025-03-05 23:39:09 +08:00
|
|
|
<TextEditor
|
|
|
|
|
id="content"
|
|
|
|
|
name="content"
|
|
|
|
|
label="Konten"
|
|
|
|
|
placeholder="Masukkan Konten"
|
2025-03-06 03:53:15 +08:00
|
|
|
className="shadow"
|
2025-03-09 14:54:18 +08:00
|
|
|
inputClassName="bg-white focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none border-0 min-h-[42px]"
|
2025-03-06 03:53:15 +08:00
|
|
|
labelClassName="text-sm font-medium text-[#363636]"
|
2025-03-05 23:39:09 +08:00
|
|
|
category="content"
|
|
|
|
|
/>
|
2025-03-05 17:57:52 +08:00
|
|
|
</fetcher.Form>
|
|
|
|
|
</RemixFormProvider>
|
|
|
|
|
|
|
|
|
|
<DevTool control={control} />
|
2025-03-02 21:51:50 +07:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|