feat: add API endpoints for fetching news by ID and slug, and update routing accordingly
This commit is contained in:
parent
f2f49de86b
commit
1885eab4c3
25
app/apis/admin/get-news-by-id.ts
Normal file
25
app/apis/admin/get-news-by-id.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
import { newsResponseSchema } from '~/apis/common/get-news'
|
||||
import { HttpServer, type THttpServer } from '~/libs/http-server'
|
||||
|
||||
const dataResponseSchema = z.object({
|
||||
data: z.object(newsResponseSchema.shape),
|
||||
})
|
||||
|
||||
type TParameters = {
|
||||
id: string
|
||||
} & THttpServer
|
||||
|
||||
export const getNewsById = async (parameters: TParameters) => {
|
||||
const { id, ...restParameters } = parameters
|
||||
try {
|
||||
const { data } = await HttpServer(restParameters).get(
|
||||
`/api/staff/news/${encodeURIComponent(id)}`,
|
||||
)
|
||||
return dataResponseSchema.parse(data)
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
|
||||
return Promise.reject(error)
|
||||
}
|
||||
}
|
||||
@ -60,7 +60,7 @@ export const ContentsPage = () => {
|
||||
},
|
||||
{
|
||||
title: 'Action',
|
||||
data: 'slug',
|
||||
data: 'id',
|
||||
},
|
||||
]
|
||||
const dataSlot: DataTableSlots = {
|
||||
|
||||
@ -126,7 +126,6 @@ export const FormContentsPage = (properties: TProperties) => {
|
||||
className="border-0 bg-white shadow read-only:bg-gray-100 focus:ring-1 focus:ring-[#2E2F7C] focus:outline-none disabled:bg-gray-100"
|
||||
labelClassName="text-sm font-medium text-[#363636]"
|
||||
containerClassName="flex-1"
|
||||
disabled={!!newsData}
|
||||
/>
|
||||
<InputFile
|
||||
id="featured_image"
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
import { isRouteErrorResponse } from 'react-router'
|
||||
|
||||
import { getNewsBySlug } from '~/apis/common/get-news-by-slug'
|
||||
import { getNewsById } from '~/apis/admin/get-news-by-id'
|
||||
import { handleCookie } from '~/libs/cookies'
|
||||
import { FormContentsPage } from '~/pages/form-contents'
|
||||
|
||||
import type { Route } from './+types/_admin.lg-admin._dashboard.contents.update.$slug'
|
||||
import type { Route } from './+types/_admin.lg-admin._dashboard.contents.update.$id'
|
||||
|
||||
export const loader = async ({ request, params }: Route.LoaderArgs) => {
|
||||
const { staffToken: accessToken } = await handleCookie(request)
|
||||
const { slug } = params
|
||||
const { data: newsData } = await getNewsBySlug({ accessToken, slug })
|
||||
const { id } = params
|
||||
const { data: newsData } = await getNewsById({ accessToken, id })
|
||||
return { newsData }
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@ import { stripHtml } from 'string-strip-html'
|
||||
|
||||
import { getCategories } from '~/apis/common/get-categories'
|
||||
import { getNews } from '~/apis/common/get-news'
|
||||
import { getNewsBySlug } from '~/apis/common/get-news-by-slug'
|
||||
import { getNewsBySlug } from '~/apis/news/get-news-by-slug'
|
||||
import { getUser } from '~/apis/news/get-user'
|
||||
import { APP } from '~/configs/meta'
|
||||
import { handleCookie } from '~/libs/cookies'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user