feat: implement news fetching API and integrate with dashboard contents

This commit is contained in:
Ardeman 2025-03-06 09:30:53 +08:00
parent d27c068f39
commit 703e347830
4 changed files with 81 additions and 17 deletions

View File

@ -0,0 +1,48 @@
import { z } from 'zod'
import { HttpServer, type THttpServer } from '~/libs/http-server'
const newsSchema = z.object({
data: z.array(
z.object({
id: z.string(),
title: z.string(),
content: z.string(),
categories: z.array(
z.object({
id: z.string(),
name: z.string(),
code: z.string(),
created_at: z.string(),
updated_at: z.string(),
}),
),
tags: z.array(
z.object({
id: z.string(),
name: z.string(),
code: z.string(),
created_at: z.string(),
updated_at: z.string(),
}),
),
is_premium: z.boolean(),
slug: z.string(),
featured_image: z.string(),
author_id: z.string(),
live_at: z.string(),
created_at: z.string(),
updated_at: z.string(),
}),
),
})
export const getNews = async (parameters: THttpServer) => {
try {
const { data } = await HttpServer(parameters).get(`/api/news`)
return newsSchema.parse(data)
} catch (error) {
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
return Promise.reject(error)
}
}

View File

@ -1,4 +1,4 @@
type TContens = {
type TContents = {
id: number
createdAt: string
author: string
@ -8,7 +8,7 @@ type TContens = {
status: string
}
export const CONTENTS: TContens[] = [
export const CONTENTS: TContents[] = [
{
id: 1,
createdAt: '24/10/2024',

View File

@ -1,35 +1,39 @@
import DT from 'datatables.net-dt'
import DataTable from 'datatables.net-react'
import { Link } from 'react-router'
import { Link, useRouteLoaderData } from 'react-router'
import { Button } from '~/components/ui/button'
import { UiTable } from '~/components/ui/table'
import { TitleDashboard } from '~/components/ui/title-dashboard'
import { CONTENTS } from './data'
import type { loader } from '~/routes/_admin.lg-admin._dashboard.contents'
export const ContentsPage = () => {
const loaderData = useRouteLoaderData<typeof loader>(
'routes/_admin.lg-admin._dashboard.contents',
)
const newsData = loaderData?.newsData
DataTable.use(DT)
const dataTable = CONTENTS
const dataTable = newsData
const dataColumns = [
{ title: 'No', data: 'id' },
{ title: 'Tanggal Kontent', data: 'createdAt' },
{ title: 'Nama Penulis', data: 'author' },
{ title: 'Tanggal Konten', data: 'created_at' },
{ title: 'Nama Penulis', data: 'author_id' },
{ title: 'Judul', data: 'title' },
{ title: 'Kategori', data: 'category' },
// { title: 'Kategori', data: 'category' },
{
title: 'Tags',
data: 'tags',
data: 'is_premium',
render: (value: string) => {
return value === 'Normal'
? `<span class="bg-[#F5F5F5] text-[#4C5CA0] px-2 py-1 rounded-md">${value}</span>`
: `<span class="bg-[#FFFCAF] px-2 py-1 rounded-md">${value}</span>`
return value
? `<span class="bg-[#FFFCAF] px-2 py-1 rounded-md">Premium</span>`
: `<span class="bg-[#F5F5F5] text-[#4C5CA0] px-2 py-1 rounded-md">Normal</span>`
},
},
{
title: 'Action',
data: 'id',
},
// {
// title: 'Action',
// data: 'id',
// },
]
const dataSlot = {
6: (value: string | number) => {

View File

@ -1,4 +1,16 @@
import { getNews } from '~/apis/admin/get-news'
import { handleCookie } from '~/libs/cookies'
import { ContentsPage } from '~/pages/dashboard-contents'
import type { Route } from './+types/_admin.lg-admin._dashboard.contents'
export const loader = async ({ request }: Route.LoaderArgs) => {
const { staffToken } = await handleCookie(request)
const { data: newsData } = await getNews({
accessToken: staffToken,
})
return { newsData }
}
const DashboardContentsLayout = () => <ContentsPage />
export default DashboardContentsLayout