mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-03 08:03:22 +00:00
- Typing updates the input immediately but delays the API call - Table keeps showing current results until the search settles - Resets to page 1 when search changes - Applies to all list pages using the shared hook
43 lines
1.5 KiB
JavaScript
43 lines
1.5 KiB
JavaScript
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
export default function usePaginatedList(fetchFn, extraParams = {}) {
|
|
const [rows, setRows] = useState([]);
|
|
const [search, setSearch] = useState("");
|
|
const [debouncedSearch, setDebouncedSearch] = useState("");
|
|
const [page, setPage] = useState(1);
|
|
const [pageSize, setPageSize] = useState(12);
|
|
const [totalCount, setTotalCount] = useState(0);
|
|
const [loading, setLoading] = useState(true);
|
|
const [err, setErr] = useState("");
|
|
const debounceRef = useRef(null);
|
|
|
|
const extraKey = JSON.stringify(extraParams);
|
|
|
|
useEffect(() => {
|
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
debounceRef.current = setTimeout(() => {
|
|
setDebouncedSearch(search);
|
|
setPage(1);
|
|
}, 600);
|
|
return () => clearTimeout(debounceRef.current);
|
|
}, [search]);
|
|
|
|
const reload = useCallback(() => {
|
|
setLoading(true);
|
|
setErr("");
|
|
fetchFn({ search: debouncedSearch, page, pageSize, ...extraParams })
|
|
.then(({ rows, totalCount }) => {
|
|
setRows(rows);
|
|
setTotalCount(totalCount);
|
|
})
|
|
.catch((e) => setErr(e.message || "Failed to load"))
|
|
.finally(() => setLoading(false));
|
|
}, [fetchFn, debouncedSearch, page, pageSize, extraKey]);
|
|
|
|
useEffect(() => { reload(); }, [reload]);
|
|
|
|
return useMemo(() => ({
|
|
rows, search, setSearch, page, setPage, pageSize, setPageSize,
|
|
totalCount, loading, err, reload
|
|
}), [rows, search, page, pageSize, totalCount, loading, err, reload]);
|
|
}
|