feat: integrate news data fetching by slug and update ContentsFormPage to utilize fetched data

This commit is contained in:
Ardeman
2025-03-07 13:50:16 +08:00
parent 2c05c543ce
commit 5c716d7210
4 changed files with 58 additions and 12 deletions
+17 -10
View File
@@ -5,6 +5,7 @@ import { useFetcher, useNavigate, useRouteLoaderData } from 'react-router'
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
import { z } from 'zod'
import type { newsResponseSchema } from '~/apis/admin/get-news'
import { TextEditor } from '~/components/text-editor'
import { Button } from '~/components/ui/button'
import { Combobox } from '~/components/ui/combobox'
@@ -54,8 +55,12 @@ export const contentSchema = z.object({
})
export type TContentSchema = z.infer<typeof contentSchema>
type TProperties = {
newsData?: z.infer<typeof newsResponseSchema>
}
export const ContentsFormPage = () => {
export const ContentsFormPage = (properties: TProperties) => {
const { newsData } = properties || {}
const fetcher = useFetcher()
const navigate = useNavigate()
const loaderData = useRouteLoaderData<typeof loader>('routes/_admin.lg-admin')
@@ -68,14 +73,16 @@ export const ContentsFormPage = () => {
mode: 'onSubmit',
fetcher,
resolver: zodResolver(contentSchema),
defaultValues: {
categories: [],
tags: [],
title: '',
content: '',
featured_image: '',
is_premium: false,
live_at: '',
values: {
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]
: '',
},
})
@@ -103,7 +110,7 @@ export const ContentsFormPage = () => {
<fetcher.Form
method="post"
onSubmit={handleSubmit}
action="/actions/admin/contents/create"
action="/actions/admin/contents/update"
className="space-y-4"
>
{error && (