fix: all filter search index

This commit is contained in:
Sabda Yagra 2025-07-29 21:57:36 +07:00
parent b7dce8c13c
commit 9dd9f621ce
1 changed files with 39 additions and 68 deletions

View File

@ -3,7 +3,6 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useSearchParams, useParams } from "next/navigation"; import { useSearchParams, useParams } from "next/navigation";
import Image from "next/image"; import Image from "next/image";
import Link from "next/link"; // pastikan path-nya sesuai
import { import {
Carousel, Carousel,
CarouselContent, CarouselContent,
@ -12,7 +11,7 @@ import {
CarouselPrevious, CarouselPrevious,
} from "@/components/ui/carousel"; } from "@/components/ui/carousel";
import { getIndeksData, getIndeksDataFilter } from "@/service/landing/landing"; 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 { loading } from "@/lib/swal";
import { getOnlyMonthAndYear } from "@/utils/globals"; import { getOnlyMonthAndYear } from "@/utils/globals";
@ -34,8 +33,8 @@ export default function IndeksCarouselComponent(props: {
const asPath = usePathname(); const asPath = usePathname();
const params = useParams(); const params = useParams();
const searchParams = useSearchParams(); const searchParams = useSearchParams();
const [indeksData, setIndeksData] = useState<any[]>([]); // ✅ State untuk menyimpan konten data const [indeksData, setIndeksData] = useState<any[]>([]);
const [newContent, setNewContent] = useState<any[]>([]); // Optional, bisa jadi redundant const [newContent, setNewContent] = useState<any[]>([]);
const [totalData, setTotalData] = useState<number>(0); const [totalData, setTotalData] = useState<number>(0);
const [totalPage, setTotalPage] = useState<number>(0); const [totalPage, setTotalPage] = useState<number>(0);
const [totalContent, setTotalContent] = useState<number>(0); const [totalContent, setTotalContent] = useState<number>(0);
@ -111,81 +110,53 @@ export default function IndeksCarouselComponent(props: {
]); ]);
async function initFetch() { async function initFetch() {
if (asPath?.includes("/polda/") == true) { const filter =
if (asPath?.split("/")[2] !== "[polda_name]") { categoryFilter?.length > 0
const filter = ? categoryFilter?.sort().join(",")
categoryFilter?.length > 0 : categorie || "";
? categoryFilter?.sort().join(",")
: categorie || "";
const name = title == undefined ? "" : title; const name = title ?? "";
const filterGroup = group == undefined ? asPath.split("/")[2] : group; const month = monthYearFilter
loading(); ? getOnlyMonthAndYear(monthYearFilter)?.split("/")[0]?.replace("", "")
const response = await getIndeksDataFilter( : "";
"4", const year = monthYearFilter
name, ? getOnlyMonthAndYear(monthYearFilter)?.split("/")[1]
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 == 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(); loading();
const response = await getIndeksDataFilter( const response = await getIndeksDataFilter(
"4", "4", // type
name, name, // title
filter, filter, // kategori
12, 12, // size
0, 0, // page
sortByOpt, sortByOpt, // sortBy
"",
"",
"", "",
"", // provinceId, cityId
filterGroup, // group
startDateString, startDateString,
endDateString, endDateString,
monthYearFilter month,
? getOnlyMonthAndYear(monthYearFilter)?.split("/")[0]?.replace("", "") year
: "",
monthYearFilter
? getOnlyMonthAndYear(monthYearFilter)?.split("/")[1]
: ""
); );
close(); close();
const data = response?.data?.data; const data = response?.data?.data;
const contentData = data?.content; const contentData = data?.content;
setNewContent(contentData || []);
setNewContent(contentData); setTotalData(data?.totalElements || 0);
setTotalData(data?.totalElements); setTotalPage(data?.totalPages || 0);
setTotalPage(data?.totalPages); setTotalContent(data?.totalElements || 0);
setTotalContent(response?.data?.data?.totalElements); } catch (error) {
close();
console.error("Error fetching indeks data:", error);
} }
} }
@ -230,7 +201,7 @@ export default function IndeksCarouselComponent(props: {
</Carousel> </Carousel>
<div className="flex justify-center mt-1 mb-6"> <div className="flex justify-center mt-1 mb-6">
<Link <Link
href={`${prefixPath}/image`} href={`${prefixPath}/indeks`}
className="border border-red-500 text-red-500 hover:bg-red-500 text-sm hover:text-white px-4 py-2 rounded transition duration-200" className="border border-red-500 text-red-500 hover:bg-red-500 text-sm hover:text-white px-4 py-2 rounded transition duration-200"
> >
Lihat Semua Lihat Semua