2025-03-15 01:00:32 +07:00
|
|
|
import { isRouteErrorResponse, redirect } from 'react-router'
|
2025-03-11 23:04:29 +08:00
|
|
|
|
2025-03-09 12:32:36 +08:00
|
|
|
import { getCategories } from '~/apis/common/get-categories'
|
|
|
|
|
import { getNews } from '~/apis/common/get-news'
|
2025-03-08 00:14:30 +07:00
|
|
|
import { getNewsBySlug } from '~/apis/common/get-news-by-slug'
|
2025-03-15 01:00:32 +07:00
|
|
|
import { getUser } from '~/apis/news/get-user'
|
2025-03-08 17:24:53 +08:00
|
|
|
import { APP } from '~/configs/meta'
|
2025-03-08 00:14:30 +07:00
|
|
|
import { handleCookie } from '~/libs/cookies'
|
2025-02-20 07:01:36 +08:00
|
|
|
import { NewsDetailPage } from '~/pages/news-detail'
|
|
|
|
|
|
2025-03-08 00:14:30 +07:00
|
|
|
import type { Route } from './+types/_news.detail.$slug'
|
|
|
|
|
|
2025-03-08 15:03:45 +08:00
|
|
|
export const loader = async ({ request, params }: Route.LoaderArgs) => {
|
2025-03-12 20:46:54 +08:00
|
|
|
const { userToken: accessToken } = await handleCookie(request)
|
2025-03-15 01:00:32 +07:00
|
|
|
if (!accessToken) {
|
|
|
|
|
return redirect('/')
|
|
|
|
|
}
|
|
|
|
|
const { data: userData } = await getUser({ accessToken })
|
2025-03-12 20:46:54 +08:00
|
|
|
const { slug } = params
|
|
|
|
|
const { data: newsDetailData } = await getNewsBySlug({ slug, accessToken })
|
2025-03-09 12:32:36 +08:00
|
|
|
const { data: categoriesData } = await getCategories()
|
|
|
|
|
const beritaCode = 'berita'
|
|
|
|
|
const beritaCategory = categoriesData.find(
|
|
|
|
|
(category) => category.code === beritaCode,
|
|
|
|
|
)
|
2025-03-14 12:57:59 +08:00
|
|
|
let { data: beritaNews } = await getNews({ categories: [beritaCode] })
|
|
|
|
|
beritaNews = beritaNews.filter((news) => new Date(news.live_at) <= new Date())
|
2025-03-08 00:14:30 +07:00
|
|
|
|
2025-03-15 01:00:32 +07:00
|
|
|
if (
|
|
|
|
|
userData.subscribe.subscribe_plan.code === 'basic' &&
|
|
|
|
|
newsDetailData.is_premium
|
|
|
|
|
) {
|
|
|
|
|
return redirect('/')
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-08 00:14:30 +07:00
|
|
|
return {
|
|
|
|
|
newsDetailData,
|
2025-03-09 12:32:36 +08:00
|
|
|
beritaCategory,
|
|
|
|
|
beritaNews,
|
2025-03-08 00:14:30 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-08 17:24:53 +08:00
|
|
|
export const meta = ({ data }: Route.MetaArgs) => {
|
2025-03-15 01:00:32 +07:00
|
|
|
if (data) {
|
|
|
|
|
const { newsDetailData } = data
|
|
|
|
|
const metaTitle = APP.title
|
|
|
|
|
const title = `${newsDetailData?.title} - ${metaTitle}`
|
2025-03-08 17:24:53 +08:00
|
|
|
|
2025-03-15 01:00:32 +07:00
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
title,
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
}
|
2025-03-08 17:24:53 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 23:04:29 +08:00
|
|
|
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
|
|
|
|
|
let message = 'Oops!'
|
|
|
|
|
let details = 'An unexpected error occurred.'
|
|
|
|
|
let stack: string | undefined
|
|
|
|
|
|
|
|
|
|
if (isRouteErrorResponse(error)) {
|
|
|
|
|
message = error.status === 404 ? '404' : 'Error'
|
|
|
|
|
details =
|
|
|
|
|
error.status === 404
|
|
|
|
|
? 'The requested page could not be found.'
|
|
|
|
|
: error.statusText || details
|
|
|
|
|
} else if (import.meta.env.DEV && error && error instanceof Error) {
|
|
|
|
|
details = error.message
|
|
|
|
|
stack = error.stack
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="container mx-auto p-4">
|
|
|
|
|
<h1>{message}</h1>
|
|
|
|
|
<p>{details}</p>
|
|
|
|
|
{stack && (
|
|
|
|
|
<pre className="w-full p-4 whitespace-pre-wrap">
|
|
|
|
|
<code>{stack}</code>
|
|
|
|
|
</pre>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-03 16:33:30 +08:00
|
|
|
const NewsDetailLayout = () => <NewsDetailPage />
|
2025-02-20 07:01:36 +08:00
|
|
|
|
|
|
|
|
export default NewsDetailLayout
|