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