2025-03-05 17:57:52 +08:00
|
|
|
import { DevTool } from '@hookform/devtools'
|
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
|
|
|
|
import { useEffect, useState } from 'react'
|
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-07 13:50:16 +08:00
|
|
|
import type { newsResponseSchema } from '~/apis/admin/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-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-05 17:57:52 +08:00
|
|
|
import type { loader } from '~/routes/_admin.lg-admin'
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}),
|
|
|
|
|
featured_image: z.string().optional(),
|
|
|
|
|
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-05 17:57:52 +08:00
|
|
|
const loaderData = useRouteLoaderData<typeof loader>('routes/_admin.lg-admin')
|
|
|
|
|
const categories = loaderData?.categoriesData
|
|
|
|
|
const tags = loaderData?.tagsData
|
|
|
|
|
const [error, setError] = useState<string>()
|
|
|
|
|
const [disabled, setDisabled] = useState(false)
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
live_at: newsData?.live_at
|
|
|
|
|
? new Date(newsData.live_at).toISOString().split('T')[0]
|
|
|
|
|
: '',
|
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(() => {
|
|
|
|
|
if (!fetcher.data?.success) {
|
|
|
|
|
setError(fetcher.data?.message)
|
|
|
|
|
setDisabled(false)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-06 05:46:25 +08:00
|
|
|
navigate('/lg-admin/contents')
|
2025-03-05 17:57:52 +08:00
|
|
|
setDisabled(true)
|
|
|
|
|
setError(undefined)
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [fetcher])
|
|
|
|
|
|
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"
|
|
|
|
|
>
|
|
|
|
|
{error && (
|
|
|
|
|
<div className="text-sm text-red-500 capitalize">{error}</div>
|
|
|
|
|
)}
|
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
|
|
|
/>
|
|
|
|
|
<Input
|
|
|
|
|
id="featured_image"
|
|
|
|
|
label="Gambar Unggulan"
|
|
|
|
|
placeholder="Masukkan Gambar Unggulan"
|
|
|
|
|
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-06 05:51:03 +08:00
|
|
|
<Button
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
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"
|
|
|
|
|
inputClassName="bg-white focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none border-0"
|
|
|
|
|
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>
|
|
|
|
|
)
|
|
|
|
|
}
|