feat: enhance news fetching with pagination and active status filters
This commit is contained in:
parent
d65aed6828
commit
6edca07fa6
@ -33,15 +33,22 @@ export type TAuthorResponse = z.infer<typeof authorSchema>
|
|||||||
type TParameters = {
|
type TParameters = {
|
||||||
categories?: string[]
|
categories?: string[]
|
||||||
tags?: string[]
|
tags?: string[]
|
||||||
|
active?: boolean
|
||||||
|
limit?: number
|
||||||
|
page?: number
|
||||||
} & THttpServer
|
} & THttpServer
|
||||||
|
|
||||||
export const getNews = async (parameters?: TParameters) => {
|
export const getNews = async (parameters?: TParameters) => {
|
||||||
const { categories, tags, ...restParameters } = parameters || {}
|
const { categories, tags, active, limit, page, ...restParameters } =
|
||||||
|
parameters || {}
|
||||||
try {
|
try {
|
||||||
const { data } = await HttpServer(restParameters).get(`/api/news`, {
|
const { data } = await HttpServer(restParameters).get(`/api/news`, {
|
||||||
params: {
|
params: {
|
||||||
...(categories && { categories: categories.join('+') }),
|
...(categories && { categories: categories.join('+') }),
|
||||||
...(tags && { tags: tags.join('+') }),
|
...(tags && { tags: tags.join('+') }),
|
||||||
|
...(active && { active }),
|
||||||
|
...(limit && { limit }),
|
||||||
|
...(page && { page }),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
return dataResponseSchema.parse(data)
|
return dataResponseSchema.parse(data)
|
||||||
|
|||||||
@ -1,23 +1,18 @@
|
|||||||
import { useRouteLoaderData } from 'react-router'
|
import { Suspense } from 'react'
|
||||||
|
import { Await, useRouteLoaderData } from 'react-router'
|
||||||
import { stripHtml } from 'string-strip-html'
|
import { stripHtml } from 'string-strip-html'
|
||||||
import { twMerge } from 'tailwind-merge'
|
import { twMerge } from 'tailwind-merge'
|
||||||
|
|
||||||
import type { TNewsResponse } from '~/apis/common/get-news'
|
|
||||||
import { CarouselNextIcon } from '~/components/icons/carousel-next'
|
import { CarouselNextIcon } from '~/components/icons/carousel-next'
|
||||||
import { CarouselPreviousIcon } from '~/components/icons/carousel-previous'
|
import { CarouselPreviousIcon } from '~/components/icons/carousel-previous'
|
||||||
import { Button } from '~/components/ui/button'
|
import { Button } from '~/components/ui/button'
|
||||||
import { useNewsContext } from '~/contexts/news'
|
import { useNewsContext } from '~/contexts/news'
|
||||||
import type { loader } from '~/routes/_news'
|
import type { loader } from '~/routes/_news'
|
||||||
|
import type { TNews } from '~/types/news'
|
||||||
import { getPremiumAttribute } from '~/utils/render'
|
import { getPremiumAttribute } from '~/utils/render'
|
||||||
|
|
||||||
import { Tags } from './tags'
|
import { Tags } from './tags'
|
||||||
|
|
||||||
type TNews = {
|
|
||||||
title: string
|
|
||||||
description: string
|
|
||||||
items: TNewsResponse[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export const CategorySection = (properties: TNews) => {
|
export const CategorySection = (properties: TNews) => {
|
||||||
const { setIsSuccessOpen } = useNewsContext()
|
const { setIsSuccessOpen } = useNewsContext()
|
||||||
const loaderData = useRouteLoaderData<typeof loader>('routes/_news')
|
const loaderData = useRouteLoaderData<typeof loader>('routes/_news')
|
||||||
@ -47,7 +42,10 @@ export const CategorySection = (properties: TNews) => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="grid sm:grid-cols-3 sm:gap-x-8">
|
<div className="grid sm:grid-cols-3 sm:gap-x-8">
|
||||||
{items.map(
|
<Suspense fallback={<div>Loading...</div>}>
|
||||||
|
<Await resolve={items}>
|
||||||
|
{(value) =>
|
||||||
|
value.data.map(
|
||||||
(
|
(
|
||||||
{ featured_image, title, content, tags, slug, is_premium },
|
{ featured_image, title, content, tags, slug, is_premium },
|
||||||
index,
|
index,
|
||||||
@ -63,7 +61,9 @@ export const CategorySection = (properties: TNews) => {
|
|||||||
src={featured_image}
|
src={featured_image}
|
||||||
alt={title}
|
alt={title}
|
||||||
/>
|
/>
|
||||||
<div className={twMerge('flex flex-col justify-between gap-4')}>
|
<div
|
||||||
|
className={twMerge('flex flex-col justify-between gap-4')}
|
||||||
|
>
|
||||||
<Tags
|
<Tags
|
||||||
tags={tags}
|
tags={tags}
|
||||||
is_premium={is_premium}
|
is_premium={is_premium}
|
||||||
@ -96,7 +96,10 @@ export const CategorySection = (properties: TNews) => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
),
|
),
|
||||||
)}
|
)
|
||||||
|
}
|
||||||
|
</Await>
|
||||||
|
</Suspense>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="my-5 mt-5 flex flex-row-reverse">
|
<div className="my-5 mt-5 flex flex-row-reverse">
|
||||||
|
|||||||
@ -17,7 +17,7 @@ export const NewsCategoriesPage = () => {
|
|||||||
<CategorySection
|
<CategorySection
|
||||||
title={name || ''}
|
title={name || ''}
|
||||||
description={description || ''}
|
description={description || ''}
|
||||||
items={newsData || []}
|
items={newsData || Promise.resolve({ data: [] })}
|
||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -21,7 +21,7 @@ export const NewsDetailPage = () => {
|
|||||||
const berita: TNews = {
|
const berita: TNews = {
|
||||||
title: loaderData?.beritaCategory?.name || '',
|
title: loaderData?.beritaCategory?.name || '',
|
||||||
description: loaderData?.beritaCategory?.description || '',
|
description: loaderData?.beritaCategory?.description || '',
|
||||||
items: loaderData?.beritaNews || [],
|
items: loaderData?.beritaData || Promise.resolve({ data: [] }),
|
||||||
}
|
}
|
||||||
const currentUrl = globalThis.location
|
const currentUrl = globalThis.location
|
||||||
const { title, content, featured_image, author, live_at, tags } =
|
const { title, content, featured_image, author, live_at, tags } =
|
||||||
|
|||||||
@ -27,12 +27,15 @@ export const loader = async ({}: Route.LoaderArgs) => {
|
|||||||
|
|
||||||
const spotlightData = getNews({
|
const spotlightData = getNews({
|
||||||
categories: [spotlightCode],
|
categories: [spotlightCode],
|
||||||
|
active: true,
|
||||||
})
|
})
|
||||||
const beritaData = getNews({
|
const beritaData = getNews({
|
||||||
categories: [beritaCode],
|
categories: [beritaCode],
|
||||||
|
active: true,
|
||||||
})
|
})
|
||||||
const kajianData = getNews({
|
const kajianData = getNews({
|
||||||
categories: [kajianCode],
|
categories: [kajianCode],
|
||||||
|
active: true,
|
||||||
})
|
})
|
||||||
return {
|
return {
|
||||||
spotlightCategory,
|
spotlightCategory,
|
||||||
|
|||||||
@ -11,8 +11,7 @@ export const loader = async ({ params }: Route.LoaderArgs) => {
|
|||||||
const { data: categoriesData } = await getCategories()
|
const { data: categoriesData } = await getCategories()
|
||||||
const { code } = params
|
const { code } = params
|
||||||
const categoryData = categoriesData.find((category) => category.code === code)
|
const categoryData = categoriesData.find((category) => category.code === code)
|
||||||
let { data: newsData } = await getNews({ categories: [code] })
|
const newsData = getNews({ categories: [code], active: true })
|
||||||
newsData = newsData.filter((news) => new Date(news.live_at) <= new Date())
|
|
||||||
return { categoryData, newsData }
|
return { categoryData, newsData }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,8 @@ export const loader = async ({ request, params }: Route.LoaderArgs) => {
|
|||||||
const userAgent = request.headers.get('user-agent')
|
const userAgent = request.headers.get('user-agent')
|
||||||
const ipAddress =
|
const ipAddress =
|
||||||
request.headers.get('cf-connecting-ip') ||
|
request.headers.get('cf-connecting-ip') ||
|
||||||
request.headers.get('x-forwarded-for')
|
request.headers.get('x-forwarded-for') ||
|
||||||
|
'localhost'
|
||||||
const { userToken: accessToken } = await handleCookie(request)
|
const { userToken: accessToken } = await handleCookie(request)
|
||||||
let userData
|
let userData
|
||||||
if (accessToken) {
|
if (accessToken) {
|
||||||
@ -43,13 +44,12 @@ export const loader = async ({ request, params }: Route.LoaderArgs) => {
|
|||||||
const beritaCategory = categoriesData.find(
|
const beritaCategory = categoriesData.find(
|
||||||
(category) => category.code === beritaCode,
|
(category) => category.code === beritaCode,
|
||||||
)
|
)
|
||||||
let { data: beritaNews } = await getNews({ categories: [beritaCode] })
|
const beritaData = getNews({ categories: [beritaCode], active: true })
|
||||||
beritaNews = beritaNews.filter((news) => new Date(news.live_at) <= new Date())
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
newsDetailData,
|
newsDetailData,
|
||||||
beritaCategory,
|
beritaCategory,
|
||||||
beritaNews,
|
beritaData,
|
||||||
shouldSubscribe,
|
shouldSubscribe,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,8 @@ export const action = async ({ request, params }: Route.ActionArgs) => {
|
|||||||
const userAgent = request.headers.get('user-agent')
|
const userAgent = request.headers.get('user-agent')
|
||||||
const ipAddress =
|
const ipAddress =
|
||||||
request.headers.get('cf-connecting-ip') ||
|
request.headers.get('cf-connecting-ip') ||
|
||||||
request.headers.get('x-forwarded-for')
|
request.headers.get('x-forwarded-for') ||
|
||||||
|
'localhost'
|
||||||
const { userToken: accessToken } = await handleCookie(request)
|
const { userToken: accessToken } = await handleCookie(request)
|
||||||
const { id } = params
|
const { id } = params
|
||||||
try {
|
try {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user