"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import { getListArticle } from "@/service/article"; import Link from "next/link"; type Article = { id: number; title: string; description: string; categoryName: string; createdAt: string; createdByName: string; categories: { title: string }[]; customCreatorName?: string; thumbnailUrl?: string; files?: { fileUrl: string; file_alt: string }[]; }; export default function Health() { const [articles, setArticles] = useState([]); const [isLoading, setIsLoading] = useState(true); useEffect(() => { fetchArticles(); }, []); async function fetchArticles() { setIsLoading(true); try { const res = await getListArticle({ limit: "12", page: 1, search: "", categorySlug: "", // ubah sesuai slug kategori kamu isPublish: true, sortBy: "created_at", sort: "desc", }); setArticles(res?.data?.data || []); } catch (error) { console.error("Error fetching Health articles:", error); } finally { setIsLoading(false); } } // Format tanggal Indonesia const formatDate = (dateString: string) => { const date = new Date(dateString); return date.toLocaleDateString("id-ID", { day: "2-digit", month: "long", year: "numeric", }); }; // Mapping artikel ke posisi layout const leftMain = articles[0]; const leftList = articles.slice(1, 4); const centerMain = articles[4]; const centerList = articles.slice(5, 8); const rightMain = articles[8]; const rightList = articles.slice(9, 12); if (isLoading) return (

Memuat berita kesehatan...

); return (

KESEHATAN

{articles.length === 0 ? (

Belum ada berita di kategori kesehatan.

) : (
{/* === LEFT COLUMN === */} {leftMain && (
{leftMain.title} {leftMain.categories?.[0]?.title}

{leftMain.title}

by {leftMain.customCreatorName || leftMain.createdByName} ·{" "} {formatDate(leftMain.createdAt)}

{leftMain.description}

{leftList.map((item) => (
{item.title}

{item.title}

{formatDate(item.createdAt)}

))}
)} {/* === CENTER COLUMN === */} {centerMain && (
{centerMain.title} {centerMain.categories?.[0]?.title}

{centerMain.title}

by {centerMain.customCreatorName || centerMain.createdByName}{" "} · {formatDate(centerMain.createdAt)}

{centerMain.description}

{centerList.map((item) => (
{item.title}

{item.title}

{formatDate(item.createdAt)}

))}
)} {/* === RIGHT COLUMN === */} {rightMain && (
{rightMain.title} {rightMain.categories?.[0]?.title}

{rightMain.title}

by {rightMain.customCreatorName || rightMain.createdByName} ·{" "} {formatDate(rightMain.createdAt)}

{rightMain.description}

{rightList.map((item) => (
{item.title}

{item.title}

{formatDate(item.createdAt)}

))}
)}
)}
Berita Utama
); }