"use client"; import { useEffect, useState } from "react"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Search, Filter, Eye, Pencil, Trash2, Plus } from "lucide-react"; import Link from "next/link"; import { getArticleByCategory, getArticlePagination } from "@/service/article"; import { formatDate } from "@/utils/global"; import { close, loading } from "@/config/swal"; import Cookies from "js-cookie"; export default function NewsImage() { const [articles, setArticles] = useState([]); const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(1); const [search, setSearch] = useState(""); const [levelId, setLevelId] = useState(); const [categories, setupCategory] = useState([]); const [selectedCategory, setSelectedCategory] = useState(null); useEffect(() => { const ulne = Cookies.get("ulne"); setLevelId(ulne); }, []); useEffect(() => { fetchCategory(); }, []); const fetchCategory = async () => { const res = await getArticleByCategory(); if (res?.data?.data) { setupCategory(res.data.data); } }; useEffect(() => { fetchData(); }, [page, search, selectedCategory]); async function fetchData() { loading(); const req = { limit: "10", page: page, title: search, source: "internal", categoryId: selectedCategory, search: "", sort: "desc", sortBy: "created_at", }; const res = await getArticlePagination(req); const data = res?.data?.data || []; setArticles(data); setTotalPage(res?.data?.meta?.totalPage || 1); close(); } const getStatus = (article: any) => { if (article.isDraft) return "Draft"; if (article.publishStatus?.toLowerCase() === "cancel") { return "Pending"; } if (article.isPublish) return "Published"; return "Pending"; }; const statusVariant = (status: string) => { const value = status.toLowerCase(); if (value === "published") { return "bg-green-100 text-green-700"; } if (value === "pending") { return "bg-yellow-100 text-yellow-700"; } if (value === "draft") { return "bg-gray-200 text-gray-600"; } return "bg-gray-200 text-gray-600"; }; const getCategoryCount = (title: string) => { return articles.filter((article) => article.categories?.some((c: any) => c.title === title), ).length; }; const filteredArticles = selectedCategory === null ? articles : articles.filter((article) => article.categories?.some((c: any) => c.id === selectedCategory), ); return (
{/* ================= HEADER ================= */}

News & Articles

Create and manage news articles and blog posts

{levelId === "3" && ( )}
{categories.map((cat: any) => ( ))}
{/* ================= SEARCH ================= */}
setSearch(e.target.value)} />
{/* ================= TABLE ================= */} Article Category Author Status Date Actions {filteredArticles.length > 0 ? ( filteredArticles.map((article, index) => ( {article.title} {article?.categories ?.map((c: any) => c.title) .join(", ")} {article.customCreatorName || article.createdByName} {(() => { const status = getStatus(article); return ( {status} ); })()} {formatDate(article.createdAt)} )) ) : ( No data available )}
{/* ================= PAGINATION ================= */}

Page {page} of {totalPage}

); }