"use client"; import { Autocomplete, AutocompleteItem, BreadcrumbItem, Breadcrumbs, Button, DatePicker, Image, Input, Listbox, ListboxItem, Pagination, Popover, PopoverContent, PopoverTrigger, Select, SelectItem, } from "@heroui/react"; import { CalendarIcon, Calender, ChevronLeftIcon, ChevronRightIcon, ClockIcon, EyeFilledIcon, SearchIcon, TimesIcon, UserIcon, } from "../../icons"; import Link from "next/link"; import { useEffect, useRef, useState } from "react"; import { getArticleByCategoryLanding, getListArticle, } from "@/services/article"; import { convertDateFormatNoTimeV2, formatMonthString, htmlToString, textEllipsis, } from "@/utils/global"; import { useParams, usePathname, useRouter, useSearchParams, } from "next/navigation"; import { close, loading } from "@/config/swal"; import { format } from "date-fns"; import { getCategoryById } from "@/services/master-categories"; const months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ]; export default function ListNews() { const [article, setArticle] = useState([]); const [page, setPage] = useState(1); const router = useRouter(); const pathname = usePathname(); const [totalPage, setTotalPage] = useState(1); const topRef = useRef(null); const params = useParams(); const category = params?.name; const searchParams = useSearchParams(); const categoryIds = searchParams.get("category_id"); const [categories, setCategories] = useState([]); const [searchValue, setSearchValue] = useState( searchParams.get("search") || "" ); const [categorySearch, setCategorySearch] = useState(""); const [debouncedValue, setDebouncedValue] = useState(""); const [selectedCategoryId, setSelectedCategoryId] = useState( categoryIds ? categoryIds : "" ); const [count, setCount] = useState(0); const today = new Date(); const [year, setYear] = useState(today.getFullYear()); const [selectedMonth, setSelectedMonth] = useState( searchParams.get("month") ? Number(searchParams.get("month")) - 1 : null ); const [selectedDate, setSelectedDate] = useState(null); const handleMonthClick = (monthIndex: number) => { setSelectedMonth(monthIndex); setSelectedDate(new Date(year, monthIndex, 1)); }; const getCategoryId = async () => { if (categoryIds) { const res = await getCategoryById(Number(selectedCategoryId)); setCategorySearch(res?.data?.data.title); setCategories([res?.data?.data]); setCount(1); } }; useEffect(() => { getCategory(); if (categoryIds && count == 0) { getCategoryId(); } }, [debouncedValue]); useEffect(() => { getArticle(); }, [page]); const getCategory = async () => { const res = await getArticleByCategoryLanding({ limit: debouncedValue === "" ? "5" : "", title: debouncedValue, }); if (res?.data?.data) { setCategories(res?.data?.data); } }; async function getArticle() { loading(); // topRef.current?.scrollIntoView({ behavior: "smooth" }); const req = { page: page, search: searchValue || "", limit: "9", isPublish: true, sort: "desc", categorySlug: pathname.includes("polda") || pathname.includes("satker") ? String(category) : "", categoryIds: selectedCategoryId, // categoryIds: // selectedCategoryId && categorySearch !== "" ? selectedCategoryId : "", startDate: selectedDate && selectedMonth !== null ? convertDateFormatNoTimeV2(new Date(year, selectedMonth, 1)) : "", endDate: selectedDate && selectedMonth !== null ? convertDateFormatNoTimeV2(new Date(year, selectedMonth + 1, 0)) : "", }; const response = await getListArticle(req); setArticle(response?.data?.data); setTotalPage(response?.data?.meta?.totalPage); close(); } useEffect(() => { const timeout = setTimeout(() => { setDebouncedValue(categorySearch); }, 1500); return () => clearTimeout(timeout); }, [categorySearch]); const onInputChange = (value: string) => { setCategorySearch(value); if (value === "") { setSelectedCategoryId(""); } }; return (
Beranda

Berita

{ // if (event.key === "Enter") { // router.push(pathname + `?search=${searchValue}`); // getArticle(); // } // }} labelPlacement="outside" placeholder="Judul..." value={searchValue} onValueChange={setSearchValue} type="search" />
setSelectedCategoryId(e)} inputProps={{ classNames: { inputWrapper: "border-1" } }} defaultItems={categories} > {/* {categories.length > 0 && categories.map((category: any) => ( {category.title} ))} */} {(item: any) => ( {item.title} )}
{" "} {selectedDate ? format(selectedDate, "MMMM yyyy") : "Pilih Bulan"}
{year}
{months.map((month, idx) => ( ))}
{" "} {selectedDate && ( setSelectedDate(null)} > )}
{/* */} {/* */}
{article?.length < 1 || !article ? (
Tidak ada Data
) : ( <>
{article?.map((news: any) => (
thumbnail
{news?.title}

{formatMonthString(news?.createdAt)}

{`${new Date(news?.createdAt) .getHours() .toString() .padStart(2, "0")}:${new Date(news?.createdAt) .getMinutes() .toString() .padStart(2, "0")}`}

{news?.createdByName}

{textEllipsis(htmlToString(news?.description), 165)}
))}
)}
); }