From 70ac77dba44386776239bfb08bb4c86a0a4fc1fd Mon Sep 17 00:00:00 2001 From: Rama Priyanto Date: Mon, 2 Jun 2025 12:18:56 +0700 Subject: [PATCH] fix:category landing --- components/main/detail/list-news.tsx | 323 ++++++++++++++------------- components/page/detail-news.tsx | 31 ++- components/page/related-news.tsx | 4 +- 3 files changed, 191 insertions(+), 167 deletions(-) diff --git a/components/main/detail/list-news.tsx b/components/main/detail/list-news.tsx index 8fd2432..b578d0f 100644 --- a/components/main/detail/list-news.tsx +++ b/components/main/detail/list-news.tsx @@ -29,7 +29,7 @@ import { UserIcon, } from "../../icons"; import Link from "next/link"; -import { useEffect, useRef, useState } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { getArticleByCategoryLanding, getListArticle, @@ -49,6 +49,7 @@ import { import { close, loading } from "@/config/swal"; import { format } from "date-fns"; import { getCategoryById } from "@/services/master-categories"; +import AsyncSelect from "react-select/async"; const months = [ "Jan", @@ -81,12 +82,7 @@ export default function ListNews() { searchParams.get("search") || "" ); const [categorySearch, setCategorySearch] = useState(""); - const [debouncedValue, setDebouncedValue] = useState(""); - const [selectedCategoryId, setSelectedCategoryId] = useState( - categoryIds ? categoryIds : "" - ); - - const [count, setCount] = useState(0); + const [selectedCategoryId, setSelectedCategoryId] = useState([]); const today = new Date(); const [year, setYear] = useState(today.getFullYear()); @@ -101,42 +97,43 @@ export default function ListNews() { 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(); + const search = searchParams.get("search"); + const category = searchParams.get("category_id"); + if (searchParams.get("search")) { + setSearchValue(String(searchParams.get("search"))); + getArticle({ title: String(search) }); } - }, [debouncedValue]); + + if (category && category !== "") { + getCategoryFromQueries(category.split(",")); + } + }, [searchParams]); + + const getCategoryFromQueries = async (category: string[]) => { + const temp = []; + for (const element of category) { + const res = await getCategoryById(Number(element)); + if (res?.data?.data) { + temp.push(res?.data?.data); + } + } + + const setup = setupCategory(temp); + setSelectedCategoryId(setup); + getArticle({ category: setup }); + }; useEffect(() => { getArticle(); - }, [page]); + }, [page, searchParams]); - const getCategory = async () => { - const res = await getArticleByCategoryLanding({ - limit: debouncedValue === "" ? "5" : "", - title: debouncedValue, - }); - if (res?.data?.data) { - setCategories(res?.data?.data); - } - }; - - async function getArticle() { + async function getArticle(props?: { title?: string; category?: any }) { loading(); // topRef.current?.scrollIntoView({ behavior: "smooth" }); const req = { page: page, - search: searchValue || "", + search: props?.title || searchValue || "", limit: "9", isPublish: true, sort: "desc", @@ -144,9 +141,12 @@ export default function ListNews() { pathname.includes("polda") || pathname.includes("satker") ? String(category) : "", - categoryIds: selectedCategoryId, - // categoryIds: - // selectedCategoryId && categorySearch !== "" ? selectedCategoryId : "", + categoryIds: props?.category + ? props.category.map((val: any) => val.id).join(",") + : selectedCategoryId.length > 0 + ? selectedCategoryId.map((val: any) => val.id).join(",") + : "", + startDate: selectedDate && selectedMonth !== null ? convertDateFormatNoTimeV2(new Date(year, selectedMonth, 1)) @@ -162,21 +162,52 @@ export default function ListNews() { close(); } - useEffect(() => { - const timeout = setTimeout(() => { - setDebouncedValue(categorySearch); - }, 1500); + const debounceTimeout = useRef(null); - return () => clearTimeout(timeout); - }, [categorySearch]); - - const onInputChange = (value: string) => { - setCategorySearch(value); - if (value === "") { - setSelectedCategoryId(""); + const getCategory = async (search?: string) => { + const res = await getArticleByCategoryLanding({ + // limit: debouncedValue === "" ? "5" : "", + // title: debouncedValue, + limit: !search || search === "" ? "5" : "", + title: search ? search : "", + }); + if (res?.data?.data) { + setCategories(res?.data?.data); + return res?.data?.data; } + return []; }; + const setupCategory = (data: any) => { + const temp = []; + for (const element of data) { + temp.push({ + id: element.id, + label: element.title, + value: element.id, + }); + } + return temp; + }; + + const loadOptions = useCallback( + (inputValue: string, callback: (options: any) => void) => { + if (debounceTimeout.current) { + clearTimeout(debounceTimeout.current); + } + + debounceTimeout.current = setTimeout(async () => { + try { + const data = await getCategory(inputValue); + callback(setupCategory(data)); + } catch (error) { + callback([]); + } + }, 1500); + }, + [] + ); + return (
@@ -187,118 +218,104 @@ export default function ListNews() {

Berita

-
- { - // if (event.key === "Enter") { - // router.push(pathname + `?search=${searchValue}`); - // getArticle(); - // } - // }} - labelPlacement="outside" - placeholder="Judul..." - value={searchValue} - onValueChange={setSearchValue} - type="search" - /> -
- +
+ { + // if (event.key === "Enter") { + // router.push(pathname + `?search=${searchValue}`); + // getArticle(); + // } + // }} labelPlacement="outside" - variant="bordered" + 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) => ( + className="z-50 min-w-[300px] max-w-[600px]" + classNames={{ + control: () => + "border border-gray-300 border-1 rounded-xl min-w-[300px] max-w-[600px]", + menu: () => "z-50", + }} + value={selectedCategoryId} + onChange={setSelectedCategoryId} + /> +
+ + + + {" "} + {selectedDate + ? format(selectedDate, "MMMM yyyy") + : "Pilih Bulan"} + + + +
- ))} -
-
-
{" "} - {selectedDate && ( - setSelectedDate(null)} - > - - - )} + {year} + +
+ +
+ {months.map((month, idx) => ( + + ))} +
+ + {" "} + {selectedDate && ( + setSelectedDate(null)} + > + + + )} +
+ + {/* */}
- {/* */} - - {/* */}
{article?.length < 1 || !article ? (
Tidak ada Data
diff --git a/components/page/detail-news.tsx b/components/page/detail-news.tsx index b07365e..303b7bb 100644 --- a/components/page/detail-news.tsx +++ b/components/page/detail-news.tsx @@ -260,7 +260,11 @@ export default function DetailNews(props: { data: any; listArticle: any }) {

TAGS

{data?.categories?.map((category: any) => ( - + @@ -268,17 +272,20 @@ export default function DetailNews(props: { data: any; listArticle: any }) { ))}
- {data?.tags?.split(",").map((tag: any) => ( - - - - ))} + {data?.tags?.split(",").map( + (tag: any) => + tag !== "" && ( + + + + ) + )}
diff --git a/components/page/related-news.tsx b/components/page/related-news.tsx index 62c8a62..976485d 100644 --- a/components/page/related-news.tsx +++ b/components/page/related-news.tsx @@ -72,8 +72,8 @@ export default function RelatedNews(props: { categories: any }) { ); }} > - {article?.map((newsItem: any) => ( - + {article?.map((newsItem: any, index: number) => ( +