Implement debounced search functionality in ApproveHistory component and update API query structure

This commit is contained in:
Swapnil Bendal
2025-01-20 20:38:52 +05:30
parent f2d8aee6a9
commit 79ec8d92ae
2 changed files with 63 additions and 43 deletions

View File

@@ -37,6 +37,7 @@ const ApproveHistory = () => {
const [actionId, setActionId] = useState(false);
const [mouseEntered, setMouseEntered] = useState(false);
const [mouseEnteredId, setMouseEnteredId] = useState("");
const [debouncedSearchTerm, setDebouncedSearchTerm] = useState("");
const [pageSize, setPageSize] = useState(TABLE_PAGINATION?.size);
const [currentPage, setCurrentPage] = useState(TABLE_PAGINATION?.page);
@@ -60,14 +61,31 @@ const ApproveHistory = () => {
onClose: onRejectClose,
} = useDisclosure();
// Debounce the search term to avoid making a request on every keystroke
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedSearchTerm(searchTerm);
}, 500); // Adjust delay as needed
return () => {
clearTimeout(handler);
};
}, [searchTerm]);
const {
data,
isLoading: drawalRequestLoading,
error,
refetch,
} = useGetApproveHistoryQuery();
console.log(data?.data?.rows);
} = useGetApproveHistoryQuery(
{
page: debouncedSearchTerm ? undefined : currentPage, // Omit pagination for search
size: debouncedSearchTerm ? undefined : pageSize, // Omit pagination for search
search: debouncedSearchTerm,
},
{
skip: debouncedSearchTerm === "" && searchTerm !== "", // Skip if search is empty and it's not the initial request
}
);
// Use useEffect to refetch data when the component mounts
useEffect(() => {
@@ -172,11 +190,7 @@ const ApproveHistory = () => {
</Box>
),
"Deposit Date": (
<Box
w={"100px"}
isTruncated={true}
display={"flex"}
>
<Box w={"100px"} isTruncated={true} display={"flex"}>
<Text as={"span"} color={"teal.900"}>
{formatDate(item?.transaction_date)}
</Text>
@@ -202,7 +216,8 @@ const ApproveHistory = () => {
fontWeight={"500"}
className="d-flex align-items-center web-text-small"
>
{item?.spportFile_path&&<Badge
{item?.spportFile_path && (
<Badge
px={2}
py={0.5}
textTransform={"inherit"}
@@ -215,15 +230,13 @@ const ApproveHistory = () => {
display={"flex"}
alignItems={"center"}
>
<Box me={"1px"}
as="span"
cursor={"pointer"}
>
<Box me={"1px"} as="span" cursor={"pointer"}>
View
</Box>
<ExternalLinkIcon />
</Link>
</Badge>}
</Badge>
)}
</Text>
),
Status: (

View File

@@ -9,7 +9,7 @@ import { baseQuery } from "./token.serivce";
export const fawateerRequest = createApi({
reducerPath: "fawateerRequest",
baseQuery: baseQuery,
tagTypes: ["getFawateerRequest" ,"getApproveHistory","getApproveComment","getRejectComment","getFawateerMakerRequest"],
tagTypes: ["getFawateerRequest", "getApproveHistory", "getApproveComment", "getRejectComment", "getFawateerMakerRequest"],
endpoints: (builder) => ({
@@ -51,7 +51,14 @@ export const fawateerRequest = createApi({
}),
getApproveHistory: builder.query({
query: () => `/fawateer/admin/getAll`,
query: ({ page, size, searchTerm }) => {
let baseURL = `/fawateer/admin/getAll?search=${searchTerm || ""}`;
if (page !== undefined && size !== undefined) {
baseURL += `&page=${page}&size=${size}`; // Only add pagination if both are defined
}
return baseURL;
},
providesTags: ["getApproveHistory"],
}),