From a7eef2e1dc51be712f21016761d2cae4ac2827ca Mon Sep 17 00:00:00 2001 From: Rama Priyanto Date: Sat, 31 May 2025 20:56:53 +0700 Subject: [PATCH] fix:news all filter, pagination collapse --- components/main/detail/list-news.tsx | 201 ++++++++++++++++----------- 1 file changed, 118 insertions(+), 83 deletions(-) diff --git a/components/main/detail/list-news.tsx b/components/main/detail/list-news.tsx index d2a7411..9e6fd28 100644 --- a/components/main/detail/list-news.tsx +++ b/components/main/detail/list-news.tsx @@ -48,6 +48,7 @@ import { } from "next/navigation"; import { close, loading } from "@/config/swal"; import { format } from "date-fns"; +import { getCategoryById } from "@/services/master-categories"; const months = [ "Jan", @@ -76,11 +77,16 @@ export default function ListNews() { const searchParams = useSearchParams(); const categoryIds = searchParams.get("category_id"); const [categories, setCategories] = useState([]); - const [search, setSearch] = useState(searchParams.get("search") || ""); - const [searchValue, setSearchValue] = useState(search || ""); + const [searchValue, setSearchValue] = useState( + searchParams.get("search") || "" + ); const [categorySearch, setCategorySearch] = useState(""); const [debouncedValue, setDebouncedValue] = useState(""); - const [selectedCategoryId, setSelectedCategoryId] = useState(); + const [selectedCategoryId, setSelectedCategoryId] = useState( + categoryIds ? categoryIds : "" + ); + + const [count, setCount] = useState(0); const today = new Date(); const [year, setYear] = useState(today.getFullYear()); @@ -95,26 +101,26 @@ export default function ListNews() { setSelectedDate(new Date(year, monthIndex, 1)); }; - useEffect(() => { - setSearch(searchParams.get("search") || ""); - setSearchValue(searchParams.get("search") || ""); - console.log( - "ini", - searchParams.get("month") ? Number(searchParams.get("month")) : null - ); - setSelectedMonth( - searchParams.get("month") ? Number(searchParams.get("month")) : null - ); - }, [searchParams]); - - useEffect(() => { - getArticle(); - }, [page, category, search, searchParams]); + 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" : "", @@ -128,7 +134,6 @@ export default function ListNews() { async function getArticle() { loading(); // topRef.current?.scrollIntoView({ behavior: "smooth" }); - const req = { page: page, search: searchValue || "", @@ -139,13 +144,17 @@ export default function ListNews() { pathname.includes("polda") || pathname.includes("satker") ? String(category) : "", - categoryIds: categoryIds ? categoryIds : "", - startDate: selectedMonth - ? convertDateFormatNoTimeV2(new Date(year, selectedMonth, 1)) - : "", - endDate: selectedMonth - ? convertDateFormatNoTimeV2(new Date(year, selectedMonth + 1, 0)) - : "", + 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); @@ -163,6 +172,9 @@ export default function ListNews() { const onInputChange = (value: string) => { setCategorySearch(value); + if (value === "") { + setSelectedCategoryId(""); + } }; return ( @@ -202,18 +214,22 @@ export default function ListNews() { labelPlacement="outside" variant="bordered" placeholder="Kategori" - classNames={{ base: "!mt-0" }} inputValue={categorySearch} + // selectedKey={selectedCategoryId} onInputChange={onInputChange} onSelectionChange={(e) => setSelectedCategoryId(e)} inputProps={{ classNames: { inputWrapper: "border-1" } }} + defaultItems={categories} > - {categories.length > 0 && + {/* {categories.length > 0 && categories.map((category: any) => ( - + {category.title} - ))} + ))} */} + {(item: any) => ( + {item.title} + )}
@@ -271,67 +287,86 @@ export default function ListNews() { )}
- */} + - + Cari + + {/* */} -
- {article?.map((news: any) => ( - Tidak ada Data + ) : ( + <> +
-
- thumbnail -
-
-
{news?.title}
-
-
- -

{formatMonthString(news?.createdAt)}

+ {article?.map((news: any) => ( + +
+ thumbnail
-
- -

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

+
+
{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)} +
-
- -

{news?.createdByName}

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