feat: integrate news data fetching by slug and update ContentsFormPage to utilize fetched data
This commit is contained in:
parent
2c05c543ce
commit
5c716d7210
@ -9,7 +9,7 @@ const authorSchema = z.object({
|
|||||||
name: z.string(),
|
name: z.string(),
|
||||||
profile_picture: z.string(),
|
profile_picture: z.string(),
|
||||||
})
|
})
|
||||||
const newsResponseSchema = z.object({
|
export const newsResponseSchema = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
title: z.string(),
|
title: z.string(),
|
||||||
content: z.string(),
|
content: z.string(),
|
||||||
|
|||||||
23
app/apis/common/get-news-by-slug.ts
Normal file
23
app/apis/common/get-news-by-slug.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
import { newsResponseSchema } from '~/apis/admin/get-news'
|
||||||
|
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||||
|
|
||||||
|
const dataResponseSchema = z.object({
|
||||||
|
data: z.object(newsResponseSchema.shape),
|
||||||
|
})
|
||||||
|
|
||||||
|
type TParameters = {
|
||||||
|
slug: string
|
||||||
|
} & THttpServer
|
||||||
|
|
||||||
|
export const getNewsBySlug = async (parameters: TParameters) => {
|
||||||
|
const { slug, accessToken } = parameters
|
||||||
|
try {
|
||||||
|
const { data } = await HttpServer({ accessToken }).get(`/api/news/${slug}`)
|
||||||
|
return dataResponseSchema.parse(data)
|
||||||
|
} catch (error) {
|
||||||
|
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,6 +5,7 @@ import { useFetcher, useNavigate, useRouteLoaderData } from 'react-router'
|
|||||||
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
|
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
import type { newsResponseSchema } from '~/apis/admin/get-news'
|
||||||
import { TextEditor } from '~/components/text-editor'
|
import { TextEditor } from '~/components/text-editor'
|
||||||
import { Button } from '~/components/ui/button'
|
import { Button } from '~/components/ui/button'
|
||||||
import { Combobox } from '~/components/ui/combobox'
|
import { Combobox } from '~/components/ui/combobox'
|
||||||
@ -54,8 +55,12 @@ export const contentSchema = z.object({
|
|||||||
})
|
})
|
||||||
|
|
||||||
export type TContentSchema = z.infer<typeof contentSchema>
|
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 fetcher = useFetcher()
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const loaderData = useRouteLoaderData<typeof loader>('routes/_admin.lg-admin')
|
const loaderData = useRouteLoaderData<typeof loader>('routes/_admin.lg-admin')
|
||||||
@ -68,14 +73,16 @@ export const ContentsFormPage = () => {
|
|||||||
mode: 'onSubmit',
|
mode: 'onSubmit',
|
||||||
fetcher,
|
fetcher,
|
||||||
resolver: zodResolver(contentSchema),
|
resolver: zodResolver(contentSchema),
|
||||||
defaultValues: {
|
values: {
|
||||||
categories: [],
|
categories: newsData?.categories || [],
|
||||||
tags: [],
|
tags: newsData?.tags || [],
|
||||||
title: '',
|
title: newsData?.title || '',
|
||||||
content: '',
|
content: newsData?.content || '',
|
||||||
featured_image: '',
|
featured_image: newsData?.featured_image || '',
|
||||||
is_premium: false,
|
is_premium: newsData?.is_premium || false,
|
||||||
live_at: '',
|
live_at: newsData?.live_at
|
||||||
|
? new Date(newsData.live_at).toISOString().split('T')[0]
|
||||||
|
: '',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -103,7 +110,7 @@ export const ContentsFormPage = () => {
|
|||||||
<fetcher.Form
|
<fetcher.Form
|
||||||
method="post"
|
method="post"
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
action="/actions/admin/contents/create"
|
action="/actions/admin/contents/update"
|
||||||
className="space-y-4"
|
className="space-y-4"
|
||||||
>
|
>
|
||||||
{error && (
|
{error && (
|
||||||
|
|||||||
@ -1,4 +1,20 @@
|
|||||||
|
import { getNewsBySlug } from '~/apis/common/get-news-by-slug'
|
||||||
|
import { handleCookie } from '~/libs/cookies'
|
||||||
import { ContentsFormPage } from '~/pages/contents-form'
|
import { ContentsFormPage } from '~/pages/contents-form'
|
||||||
|
|
||||||
const DashboardContentUpdateLayout = () => <ContentsFormPage />
|
import type { Route } from './+types/_admin.lg-admin._dashboard.contents.update.$slug'
|
||||||
|
|
||||||
|
export const loader = async ({ request }: Route.LoaderArgs) => {
|
||||||
|
const { staffToken } = await handleCookie(request)
|
||||||
|
const { data: newsData } = await getNewsBySlug({
|
||||||
|
accessToken: staffToken,
|
||||||
|
slug: request.url.split('/').pop() ?? '',
|
||||||
|
})
|
||||||
|
return { newsData }
|
||||||
|
}
|
||||||
|
|
||||||
|
const DashboardContentUpdateLayout = ({ loaderData }: Route.ComponentProps) => {
|
||||||
|
const newsData = loaderData.newsData
|
||||||
|
return <ContentsFormPage newsData={newsData} />
|
||||||
|
}
|
||||||
export default DashboardContentUpdateLayout
|
export default DashboardContentUpdateLayout
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user