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 { 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<any[]>([]); // ✅ State untuk menyimpan konten data
const [newContent, setNewContent] = useState<any[]>([]); // Optional, bisa jadi redundant
const [indeksData, setIndeksData] = useState<any[]>([]);
const [newContent, setNewContent] = useState<any[]>([]);
const [totalData, setTotalData] = useState<number>(0);
const [totalPage, setTotalPage] = useState<number>(0);
const [totalContent, setTotalContent] = useState<number>(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: {
</Carousel>
<div className="flex justify-center mt-1 mb-6">
<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"
>
Lihat Semua