From 7afabdaa03d3101c264f90ed086ff2a2e3d8b191 Mon Sep 17 00:00:00 2001 From: Ardeman Date: Sun, 9 Mar 2025 12:32:36 +0800 Subject: [PATCH] refactor: update news types and enhance news data handling in components --- app/components/ui/carousel-hero.tsx | 67 ++++++++++---------- app/components/ui/carousel-section.tsx | 16 +++-- app/data/contents.ts | 88 -------------------------- app/pages/news-detail/index.tsx | 9 ++- app/pages/news/index.tsx | 21 +++++- app/routes/_news._index.tsx | 19 ++++++ app/routes/_news.detail.$slug.tsx | 10 +++ app/types/news.ts | 19 +----- 8 files changed, 101 insertions(+), 148 deletions(-) diff --git a/app/components/ui/carousel-hero.tsx b/app/components/ui/carousel-hero.tsx index d061ce4..71bc36f 100644 --- a/app/components/ui/carousel-hero.tsx +++ b/app/components/ui/carousel-hero.tsx @@ -1,6 +1,7 @@ import useEmblaCarousel from 'embla-carousel-react' import { useCallback, useEffect, useState } from 'react' import { useRouteLoaderData } from 'react-router' +import { stripHtml } from 'string-strip-html' import { Button } from '~/components/ui/button' import { CarouselButton } from '~/components/ui/button-slide' @@ -71,41 +72,43 @@ export const CarouselHero = (properties: TNews) => { ref={emblaReference} >
- {items.map(({ featured, title, content, slug, isPremium }, index) => ( -
-
- {title} -
-
-

- {title} -

-

- {content} -

+ {items.map( + ({ featured_image, title, content, slug, is_premium }, index) => ( +
+
+ {title} +
+
+

+ {title} +

+

+ {stripHtml(content).result} +

+
+
-
-
- ))} + ), + )}
diff --git a/app/components/ui/carousel-section.tsx b/app/components/ui/carousel-section.tsx index a4cbcdb..1e5b9e3 100644 --- a/app/components/ui/carousel-section.tsx +++ b/app/components/ui/carousel-section.tsx @@ -1,6 +1,7 @@ import useEmblaCarousel from 'embla-carousel-react' import { useCallback, useEffect, useState } from 'react' import { useRouteLoaderData } from 'react-router' +import { stripHtml } from 'string-strip-html' import { Button } from '~/components/ui/button' import { CarouselButton } from '~/components/ui/button-slide' @@ -79,7 +80,10 @@ export const CarouselSection = (properties: TNews) => { >
{items.map( - ({ featured, title, content, tags, slug, isPremium }, index) => ( + ( + { featured_image, title, content, tags, slug, is_premium }, + index, + ) => (
{
{title}

{title}

-

- {content} +

+ {stripHtml(content).result}

) diff --git a/app/pages/news/index.tsx b/app/pages/news/index.tsx index 6f91735..283c05b 100644 --- a/app/pages/news/index.tsx +++ b/app/pages/news/index.tsx @@ -1,21 +1,36 @@ +import { useRouteLoaderData } from 'react-router' + import { Card } from '~/components/ui/card' import { CarouselHero } from '~/components/ui/carousel-hero' import { CarouselSection } from '~/components/ui/carousel-section' import { Newsletter } from '~/components/ui/newsletter' -import { BERITA, SPOTLIGHT } from '~/data/contents' +import type { loader } from '~/routes/_news._index' +import type { TNews } from '~/types/news' export const NewsPage = () => { + const loaderData = useRouteLoaderData('routes/_news._index') + const spotlight: TNews = { + title: loaderData?.spotlightCategory?.name || '', + description: loaderData?.spotlightCategory?.description || '', + items: loaderData?.spotlightNews || [], + } + const berita: TNews = { + title: loaderData?.beritaCategory?.name || '', + description: loaderData?.beritaCategory?.description || '', + items: loaderData?.beritaNews || [], + } + return (
- +
- + {/* diff --git a/app/routes/_news._index.tsx b/app/routes/_news._index.tsx index ffa7627..3ac1ba8 100644 --- a/app/routes/_news._index.tsx +++ b/app/routes/_news._index.tsx @@ -1,5 +1,24 @@ +import { getCategories } from '~/apis/common/get-categories' +import { getNews } from '~/apis/common/get-news' import { NewsPage } from '~/pages/news' +import type { Route } from './+types/_news._index' + +export const loader = async ({}: Route.LoaderArgs) => { + const { data: categoriesData } = await getCategories() + const spotlightCode = 'spotlight' + const spotlightCategory = categoriesData.find( + (category) => category.code === spotlightCode, + ) + const { data: spotlightNews } = await getNews({ categories: [spotlightCode] }) + const beritaCode = 'berita' + const beritaCategory = categoriesData.find( + (category) => category.code === beritaCode, + ) + const { data: beritaNews } = await getNews({ categories: [beritaCode] }) + return { spotlightCategory, spotlightNews, beritaCategory, beritaNews } +} + const NewsIndexLayout = () => export default NewsIndexLayout diff --git a/app/routes/_news.detail.$slug.tsx b/app/routes/_news.detail.$slug.tsx index 1517dd0..40b7156 100644 --- a/app/routes/_news.detail.$slug.tsx +++ b/app/routes/_news.detail.$slug.tsx @@ -1,3 +1,5 @@ +import { getCategories } from '~/apis/common/get-categories' +import { getNews } from '~/apis/common/get-news' import { getNewsBySlug } from '~/apis/common/get-news-by-slug' import { APP } from '~/configs/meta' import { handleCookie } from '~/libs/cookies' @@ -11,9 +13,17 @@ export const loader = async ({ request, params }: Route.LoaderArgs) => { slug: params.slug, accessToken: userToken, }) + const { data: categoriesData } = await getCategories() + const beritaCode = 'berita' + const beritaCategory = categoriesData.find( + (category) => category.code === beritaCode, + ) + const { data: beritaNews } = await getNews({ categories: [beritaCode] }) return { newsDetailData, + beritaCategory, + beritaNews, } } diff --git a/app/types/news.ts b/app/types/news.ts index 49e03ff..d6dbf1a 100644 --- a/app/types/news.ts +++ b/app/types/news.ts @@ -1,22 +1,7 @@ -import type { TTagResponse } from '~/apis/common/get-tags' +import type { TNewsResponse } from '~/apis/common/get-news' export type TNews = { title: string description: string - items: Pick< - TNewsDetail, - 'title' | 'content' | 'featured' | 'slug' | 'tags' | 'isPremium' - >[] -} - -type TNewsDetail = { - title: string - content: string - featured: string - author: string - date: Date - slug: string - tags?: TTagResponse[] - isPremium?: boolean - categories?: Array + items: TNewsResponse[] }