fix:loading fe

This commit is contained in:
Rama Priyanto 2026-02-04 12:24:19 +07:00
parent 6e071f170f
commit 076c16be28
1 changed files with 18 additions and 5 deletions

View File

@ -99,6 +99,8 @@ export default function ListNews() {
null,
);
const requestIdRef = useRef(0);
const handleMonthClick = (monthIndex: number) => {
setSelectedMonth(monthIndex);
setSelectedDate(new Date(year, monthIndex, 1));
@ -181,7 +183,7 @@ export default function ListNews() {
const yearQ = searchParams.get("year");
const pageQ = searchParams.get("page");
getArticle({
await getArticle({
title: search,
category: setup,
month: month ? Number(month) : null,
@ -201,6 +203,8 @@ export default function ListNews() {
year?: number | null;
page?: number;
}) {
requestIdRef.current += 1;
const currentRequestId = requestIdRef.current;
loading();
const usedPage = props?.page || page;
@ -255,11 +259,20 @@ export default function ListNews() {
timeStamp: getUnixTimestamp(),
};
try {
const response = await getListArticle(req);
// ❗ GUARD: hanya request terakhir yang boleh set state
if (currentRequestId !== requestIdRef.current) return;
setArticle(response?.data?.data);
setTotalPage(response?.data?.meta?.totalPage);
} finally {
if (currentRequestId === requestIdRef.current) {
close();
}
}
}
const debounceTimeout = useRef<NodeJS.Timeout | null>(null);