diff --git a/components/landing-page/filter-all/indeks-filter-card.tsx b/components/landing-page/filter-all/indeks-filter-card.tsx index 0c2dd62b..69180135 100644 --- a/components/landing-page/filter-all/indeks-filter-card.tsx +++ b/components/landing-page/filter-all/indeks-filter-card.tsx @@ -3,7 +3,6 @@ import { useEffect, useState } from "react"; import { useSearchParams, useParams } from "next/navigation"; import Image from "next/image"; -import Link from "next/link"; // pastikan path-nya sesuai import { Carousel, CarouselContent, @@ -12,7 +11,7 @@ import { CarouselPrevious, } from "@/components/ui/carousel"; import { getIndeksData, getIndeksDataFilter } from "@/service/landing/landing"; -import { usePathname, useRouter } from "@/i18n/routing"; +import { Link, usePathname, useRouter } from "@/i18n/routing"; import { loading } from "@/lib/swal"; import { getOnlyMonthAndYear } from "@/utils/globals"; @@ -34,8 +33,8 @@ export default function IndeksCarouselComponent(props: { const asPath = usePathname(); const params = useParams(); const searchParams = useSearchParams(); - const [indeksData, setIndeksData] = useState([]); // ✅ State untuk menyimpan konten data - const [newContent, setNewContent] = useState([]); // Optional, bisa jadi redundant + const [indeksData, setIndeksData] = useState([]); + const [newContent, setNewContent] = useState([]); const [totalData, setTotalData] = useState(0); const [totalPage, setTotalPage] = useState(0); const [totalContent, setTotalContent] = useState(0); @@ -111,81 +110,53 @@ export default function IndeksCarouselComponent(props: { ]); async function initFetch() { - if (asPath?.includes("/polda/") == true) { - if (asPath?.split("/")[2] !== "[polda_name]") { - const filter = - categoryFilter?.length > 0 - ? categoryFilter?.sort().join(",") - : categorie || ""; + const filter = + categoryFilter?.length > 0 + ? categoryFilter?.sort().join(",") + : categorie || ""; - const name = title == undefined ? "" : title; - const filterGroup = group == undefined ? asPath.split("/")[2] : group; - loading(); - const response = await getIndeksDataFilter( - "4", - name, - filter, - 12, - 0, - sortByOpt, - "", - "", - filterGroup, - startDateString, - endDateString, - monthYearFilter - ? getOnlyMonthAndYear(monthYearFilter) - ?.split("/")[0] - ?.replace("", "") - : "", - monthYearFilter - ? getOnlyMonthAndYear(monthYearFilter)?.split("/")[1] - : "" - ); - close(); - const data = response?.data?.data; - const contentData = data?.content; - setNewContent(contentData); - setTotalData(data?.totalElements); - setTotalPage(data?.totalPages); - setTotalContent(response?.data?.data?.totalElements); - } - } else { - const filter = - categoryFilter?.length > 0 - ? categoryFilter?.sort().join(",") - : categorie || ""; + const name = title ?? ""; + const month = monthYearFilter + ? getOnlyMonthAndYear(monthYearFilter)?.split("/")[0]?.replace("", "") + : ""; + const year = monthYearFilter + ? getOnlyMonthAndYear(monthYearFilter)?.split("/")[1] + : ""; - const name = title == undefined ? "" : title; + // Gunakan group hanya jika asPath mengandung /polda/ atau /satker/, selain itu kosongkan + let filterGroup = ""; + if (asPath.includes("/polda/") || asPath.includes("/satker/")) { + filterGroup = group ?? asPath.split("/")[2]; + } + + try { loading(); const response = await getIndeksDataFilter( - "4", - name, - filter, - 12, - 0, - sortByOpt, - "", - "", + "4", // type + name, // title + filter, // kategori + 12, // size + 0, // page + sortByOpt, // sortBy "", + "", // provinceId, cityId + filterGroup, // group startDateString, endDateString, - monthYearFilter - ? getOnlyMonthAndYear(monthYearFilter)?.split("/")[0]?.replace("", "") - : "", - monthYearFilter - ? getOnlyMonthAndYear(monthYearFilter)?.split("/")[1] - : "" + month, + year ); close(); const data = response?.data?.data; const contentData = data?.content; - - setNewContent(contentData); - setTotalData(data?.totalElements); - setTotalPage(data?.totalPages); - setTotalContent(response?.data?.data?.totalElements); + setNewContent(contentData || []); + setTotalData(data?.totalElements || 0); + setTotalPage(data?.totalPages || 0); + setTotalContent(data?.totalElements || 0); + } catch (error) { + close(); + console.error("Error fetching indeks data:", error); } } @@ -230,7 +201,7 @@ export default function IndeksCarouselComponent(props: {
Lihat Semua