feat(admin): optimize tender list pagination
continuous-integration/drone/push Build is passing

This commit is contained in:
AmirReza Jamali
2026-07-11 15:46:19 +03:30
parent 624614f1a9
commit a762a14026
7 changed files with 136 additions and 92 deletions
+8 -7
View File
@@ -1,9 +1,10 @@
import { API_ENDPOINTS, tendersService } from "@/lib/api";
import { TTenderListParams } from "@/lib/api/types/Tenders";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useMemo } from "react";
import { toast } from "react-toastify";
export const useTendersQuery = (params?: Record<string, any>) => {
export const useTendersQuery = (params?: TTenderListParams) => {
const queryKey = useMemo(
() => [API_ENDPOINTS.TENDERS.READ_ALL, "read-tenders", params],
[params],
@@ -15,7 +16,10 @@ export const useTendersQuery = (params?: Record<string, any>) => {
});
};
export const useTenderDetailQuery = (id: string, params?: Record<string, any>) => {
export const useTenderDetailQuery = (
id: string,
params?: Record<string, any>,
) => {
const queryKey = useMemo(
() => [API_ENDPOINTS.TENDERS.DETAILS(id), params],
[id, params],
@@ -52,13 +56,10 @@ export const useGetTenderDocumentsQuery = (
id: string,
options?: { enabled?: boolean },
) => {
const queryKey = useMemo(
() => [API_ENDPOINTS.TENDERS.DOCUMENTS(id)],
[id],
);
const queryKey = useMemo(() => [API_ENDPOINTS.TENDERS.DOCUMENTS(id)], [id]);
return useQuery({
queryKey,
queryFn: () => tendersService.getDocuments(id),
enabled: options?.enabled ?? true,
});
};
};
+21 -4
View File
@@ -1,6 +1,5 @@
"use client";
import { IMetaData } from "@/lib/api/types/Factory";
import { AxiosError } from "axios";
import { useCallback, useEffect, useMemo, useState } from "react";
@@ -12,7 +11,14 @@ const isInvalidCursorError = (error: unknown) => {
};
type UseHybridPaginationOptions = {
meta?: IMetaData;
meta?: {
limit: number;
offset: number;
page?: number;
pages?: number;
has_more?: boolean;
next_cursor?: string;
};
params: Record<string, unknown>;
setParams: React.Dispatch<React.SetStateAction<Record<string, any>>>;
isError?: boolean;
@@ -91,7 +97,14 @@ export const useHybridPagination = ({
if (meta?.page != null) return meta.page - 1;
return 0;
}, [cursorStack, params.cursor, params.offset, meta?.offset, meta?.page, limit]);
}, [
cursorStack,
params.cursor,
params.offset,
meta?.offset,
meta?.page,
limit,
]);
const hasPrevious = cursorStack.length > 0 || currentPageIndex > 0;
@@ -129,7 +142,11 @@ export const useHybridPagination = ({
{ cursor: nextCursor, fromPage: fromPage1Based },
]);
setParams((current) => {
const next: Record<string, any> = { ...current, cursor: nextCursor, limit };
const next: Record<string, any> = {
...current,
cursor: nextCursor,
limit,
};
delete next.offset;
return next;
});