feat: integrate news data fetching by slug and update ContentsFormPage to utilize fetched data
This commit is contained in:
@@ -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 && (
|
||||
|
||||
Reference in New Issue
Block a user