@ventlio/tanstack-query 0.2.74 → 0.2.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +10 -12
- package/dist/index.mjs.map +1 -1
- package/dist/queries/useGetInfiniteRequest.d.ts +54 -19
- package/dist/queries/useGetInfiniteRequest.js +10 -12
- package/dist/queries/useGetInfiniteRequest.js.map +1 -1
- package/package.json +1 -1
- package/src/queries/useGetInfiniteRequest.ts +25 -24
package/dist/index.mjs
CHANGED
|
@@ -459,23 +459,23 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
459
459
|
res(null);
|
|
460
460
|
}
|
|
461
461
|
};
|
|
462
|
-
const query = useInfiniteQuery([path, {}], ({ pageParam = path }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
|
|
463
|
-
enabled: load,
|
|
464
|
-
getNextPageParam: (lastPage, pages) => constructPaginationLink('next_page', lastPage, pages),
|
|
465
|
-
getPreviousPageParam: (lastPage, pages) => constructPaginationLink('previous_page', lastPage, pages),
|
|
466
|
-
...queryOptions,
|
|
467
|
-
});
|
|
468
462
|
/**
|
|
469
463
|
*
|
|
470
464
|
* This pagination implementation is currently tied to our use case
|
|
471
465
|
*/
|
|
472
|
-
const constructPaginationLink = (direction, lastPage
|
|
473
|
-
const [pathname, queryString] =
|
|
466
|
+
const constructPaginationLink = (direction, lastPage) => {
|
|
467
|
+
const [pathname, queryString] = path.split('?');
|
|
474
468
|
const queryParams = new URLSearchParams(queryString);
|
|
475
|
-
const lastPageItem =
|
|
476
|
-
queryParams.set('page', String(lastPageItem
|
|
469
|
+
const lastPageItem = lastPage.data.pagination[direction];
|
|
470
|
+
queryParams.set('page', String(lastPageItem));
|
|
477
471
|
return pathname + '?' + queryParams.toString();
|
|
478
472
|
};
|
|
473
|
+
const query = useInfiniteQuery([path, {}], ({ pageParam = path }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
|
|
474
|
+
enabled: load,
|
|
475
|
+
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
476
|
+
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
477
|
+
...queryOptions,
|
|
478
|
+
});
|
|
479
479
|
useEffect(() => {
|
|
480
480
|
if (keyTracker) {
|
|
481
481
|
// set expiration time for the tracker
|
|
@@ -486,10 +486,8 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
486
486
|
queryClient.setQueryData([keyTracker], [path, {}]);
|
|
487
487
|
}
|
|
488
488
|
}, [keyTracker, path, queryClient, queryOptions?.staleTime]);
|
|
489
|
-
const flattenedData = query.data?.pages.flat() ?? [];
|
|
490
489
|
return {
|
|
491
490
|
...query,
|
|
492
|
-
flattenedData,
|
|
493
491
|
};
|
|
494
492
|
};
|
|
495
493
|
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
import type { IRequestSuccess } from '../request';
|
|
2
2
|
import type { DefaultRequestOptions, TanstackInfiniteQueryOption } from './queries.interface';
|
|
3
|
+
interface Pagination {
|
|
4
|
+
previous_page: number;
|
|
5
|
+
current_page: number;
|
|
6
|
+
next_page: number;
|
|
7
|
+
size: number;
|
|
8
|
+
page_count: number;
|
|
9
|
+
total: number;
|
|
10
|
+
}
|
|
3
11
|
export declare const useGetInfiniteRequest: <TResponse extends Record<string, any>>({ path, load, queryOptions, keyTracker, baseUrl, headers, }: {
|
|
4
12
|
path: string;
|
|
5
13
|
load?: boolean | undefined;
|
|
6
|
-
queryOptions?: TanstackInfiniteQueryOption<TResponse
|
|
14
|
+
queryOptions?: TanstackInfiniteQueryOption<TResponse & {
|
|
15
|
+
pagination: Pagination;
|
|
16
|
+
}> | undefined;
|
|
7
17
|
keyTracker?: string | undefined;
|
|
8
18
|
} & DefaultRequestOptions) => {
|
|
9
|
-
flattenedData: IRequestSuccess<TResponse>[];
|
|
10
19
|
data: undefined;
|
|
11
20
|
error: any;
|
|
12
21
|
isError: true;
|
|
@@ -15,8 +24,12 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
15
24
|
isRefetchError: false;
|
|
16
25
|
isSuccess: false;
|
|
17
26
|
status: "error";
|
|
18
|
-
fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse
|
|
19
|
-
|
|
27
|
+
fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse & {
|
|
28
|
+
pagination: Pagination;
|
|
29
|
+
}>, any>>;
|
|
30
|
+
fetchPreviousPage: (options?: import("@tanstack/react-query").FetchPreviousPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse & {
|
|
31
|
+
pagination: Pagination;
|
|
32
|
+
}>, any>>;
|
|
20
33
|
hasNextPage?: boolean | undefined;
|
|
21
34
|
hasPreviousPage?: boolean | undefined;
|
|
22
35
|
isFetchingNextPage: boolean;
|
|
@@ -35,11 +48,12 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
35
48
|
isPreviousData: boolean;
|
|
36
49
|
isRefetching: boolean;
|
|
37
50
|
isStale: boolean;
|
|
38
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<IRequestSuccess<TResponse
|
|
51
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<IRequestSuccess<TResponse & {
|
|
52
|
+
pagination: Pagination;
|
|
53
|
+
}>>, any>>;
|
|
39
54
|
remove: () => void;
|
|
40
55
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
41
56
|
} | {
|
|
42
|
-
flattenedData: IRequestSuccess<TResponse>[];
|
|
43
57
|
data: undefined;
|
|
44
58
|
error: null;
|
|
45
59
|
isError: false;
|
|
@@ -48,8 +62,12 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
48
62
|
isRefetchError: false;
|
|
49
63
|
isSuccess: false;
|
|
50
64
|
status: "loading";
|
|
51
|
-
fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse
|
|
52
|
-
|
|
65
|
+
fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse & {
|
|
66
|
+
pagination: Pagination;
|
|
67
|
+
}>, any>>;
|
|
68
|
+
fetchPreviousPage: (options?: import("@tanstack/react-query").FetchPreviousPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse & {
|
|
69
|
+
pagination: Pagination;
|
|
70
|
+
}>, any>>;
|
|
53
71
|
hasNextPage?: boolean | undefined;
|
|
54
72
|
hasPreviousPage?: boolean | undefined;
|
|
55
73
|
isFetchingNextPage: boolean;
|
|
@@ -68,12 +86,15 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
68
86
|
isPreviousData: boolean;
|
|
69
87
|
isRefetching: boolean;
|
|
70
88
|
isStale: boolean;
|
|
71
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<IRequestSuccess<TResponse
|
|
89
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<IRequestSuccess<TResponse & {
|
|
90
|
+
pagination: Pagination;
|
|
91
|
+
}>>, any>>;
|
|
72
92
|
remove: () => void;
|
|
73
93
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
74
94
|
} | {
|
|
75
|
-
|
|
76
|
-
|
|
95
|
+
data: import("@tanstack/react-query").InfiniteData<IRequestSuccess<TResponse & {
|
|
96
|
+
pagination: Pagination;
|
|
97
|
+
}>>;
|
|
77
98
|
error: any;
|
|
78
99
|
isError: true;
|
|
79
100
|
isLoading: false;
|
|
@@ -81,8 +102,12 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
81
102
|
isRefetchError: true;
|
|
82
103
|
isSuccess: false;
|
|
83
104
|
status: "error";
|
|
84
|
-
fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse
|
|
85
|
-
|
|
105
|
+
fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse & {
|
|
106
|
+
pagination: Pagination;
|
|
107
|
+
}>, any>>;
|
|
108
|
+
fetchPreviousPage: (options?: import("@tanstack/react-query").FetchPreviousPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse & {
|
|
109
|
+
pagination: Pagination;
|
|
110
|
+
}>, any>>;
|
|
86
111
|
hasNextPage?: boolean | undefined;
|
|
87
112
|
hasPreviousPage?: boolean | undefined;
|
|
88
113
|
isFetchingNextPage: boolean;
|
|
@@ -101,12 +126,15 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
101
126
|
isPreviousData: boolean;
|
|
102
127
|
isRefetching: boolean;
|
|
103
128
|
isStale: boolean;
|
|
104
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<IRequestSuccess<TResponse
|
|
129
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<IRequestSuccess<TResponse & {
|
|
130
|
+
pagination: Pagination;
|
|
131
|
+
}>>, any>>;
|
|
105
132
|
remove: () => void;
|
|
106
133
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
107
134
|
} | {
|
|
108
|
-
|
|
109
|
-
|
|
135
|
+
data: import("@tanstack/react-query").InfiniteData<IRequestSuccess<TResponse & {
|
|
136
|
+
pagination: Pagination;
|
|
137
|
+
}>>;
|
|
110
138
|
error: null;
|
|
111
139
|
isError: false;
|
|
112
140
|
isLoading: false;
|
|
@@ -114,8 +142,12 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
114
142
|
isRefetchError: false;
|
|
115
143
|
isSuccess: true;
|
|
116
144
|
status: "success";
|
|
117
|
-
fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse
|
|
118
|
-
|
|
145
|
+
fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse & {
|
|
146
|
+
pagination: Pagination;
|
|
147
|
+
}>, any>>;
|
|
148
|
+
fetchPreviousPage: (options?: import("@tanstack/react-query").FetchPreviousPageOptions | undefined) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<IRequestSuccess<TResponse & {
|
|
149
|
+
pagination: Pagination;
|
|
150
|
+
}>, any>>;
|
|
119
151
|
hasNextPage?: boolean | undefined;
|
|
120
152
|
hasPreviousPage?: boolean | undefined;
|
|
121
153
|
isFetchingNextPage: boolean;
|
|
@@ -134,7 +166,10 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
134
166
|
isPreviousData: boolean;
|
|
135
167
|
isRefetching: boolean;
|
|
136
168
|
isStale: boolean;
|
|
137
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<IRequestSuccess<TResponse
|
|
169
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<IRequestSuccess<TResponse & {
|
|
170
|
+
pagination: Pagination;
|
|
171
|
+
}>>, any>>;
|
|
138
172
|
remove: () => void;
|
|
139
173
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
140
174
|
};
|
|
175
|
+
export {};
|
|
@@ -34,23 +34,23 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
34
34
|
res(null);
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
const query = useInfiniteQuery([path, {}], ({ pageParam = path }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
|
|
38
|
-
enabled: load,
|
|
39
|
-
getNextPageParam: (lastPage, pages) => constructPaginationLink('next_page', lastPage, pages),
|
|
40
|
-
getPreviousPageParam: (lastPage, pages) => constructPaginationLink('previous_page', lastPage, pages),
|
|
41
|
-
...queryOptions,
|
|
42
|
-
});
|
|
43
37
|
/**
|
|
44
38
|
*
|
|
45
39
|
* This pagination implementation is currently tied to our use case
|
|
46
40
|
*/
|
|
47
|
-
const constructPaginationLink = (direction, lastPage
|
|
48
|
-
const [pathname, queryString] =
|
|
41
|
+
const constructPaginationLink = (direction, lastPage) => {
|
|
42
|
+
const [pathname, queryString] = path.split('?');
|
|
49
43
|
const queryParams = new URLSearchParams(queryString);
|
|
50
|
-
const lastPageItem =
|
|
51
|
-
queryParams.set('page', String(lastPageItem
|
|
44
|
+
const lastPageItem = lastPage.data.pagination[direction];
|
|
45
|
+
queryParams.set('page', String(lastPageItem));
|
|
52
46
|
return pathname + '?' + queryParams.toString();
|
|
53
47
|
};
|
|
48
|
+
const query = useInfiniteQuery([path, {}], ({ pageParam = path }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
|
|
49
|
+
enabled: load,
|
|
50
|
+
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
51
|
+
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
52
|
+
...queryOptions,
|
|
53
|
+
});
|
|
54
54
|
useEffect(() => {
|
|
55
55
|
if (keyTracker) {
|
|
56
56
|
// set expiration time for the tracker
|
|
@@ -61,10 +61,8 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
61
61
|
queryClient.setQueryData([keyTracker], [path, {}]);
|
|
62
62
|
}
|
|
63
63
|
}, [keyTracker, path, queryClient, queryOptions?.staleTime]);
|
|
64
|
-
const flattenedData = query.data?.pages.flat() ?? [];
|
|
65
64
|
return {
|
|
66
65
|
...query,
|
|
67
|
-
flattenedData,
|
|
68
66
|
};
|
|
69
67
|
};
|
|
70
68
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGetInfiniteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useGetInfiniteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -26,7 +26,7 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
26
26
|
}: {
|
|
27
27
|
path: string;
|
|
28
28
|
load?: boolean;
|
|
29
|
-
queryOptions?: TanstackInfiniteQueryOption<TResponse>;
|
|
29
|
+
queryOptions?: TanstackInfiniteQueryOption<TResponse & { pagination: Pagination }>;
|
|
30
30
|
keyTracker?: string;
|
|
31
31
|
} & DefaultRequestOptions) => {
|
|
32
32
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
@@ -39,7 +39,10 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
39
39
|
|
|
40
40
|
const sendRequest = async (
|
|
41
41
|
res: (
|
|
42
|
-
value:
|
|
42
|
+
value:
|
|
43
|
+
| IRequestError
|
|
44
|
+
| IRequestSuccess<TResponse & { pagination: Pagination }>
|
|
45
|
+
| PromiseLike<IRequestError | IRequestSuccess<TResponse & { pagination: Pagination }>>
|
|
43
46
|
) => void,
|
|
44
47
|
rej: (reason?: any) => void,
|
|
45
48
|
pageParam?: string
|
|
@@ -56,7 +59,7 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
56
59
|
});
|
|
57
60
|
|
|
58
61
|
if (getResponse.status) {
|
|
59
|
-
res(getResponse as IRequestSuccess<TResponse>);
|
|
62
|
+
res(getResponse as IRequestSuccess<TResponse & { pagination: Pagination }>);
|
|
60
63
|
} else {
|
|
61
64
|
rej(getResponse);
|
|
62
65
|
}
|
|
@@ -65,41 +68,42 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
65
68
|
}
|
|
66
69
|
};
|
|
67
70
|
|
|
68
|
-
const query = useInfiniteQuery<any, any, IRequestSuccess<TResponse>>(
|
|
69
|
-
[path, {}],
|
|
70
|
-
({ pageParam = path }) =>
|
|
71
|
-
new Promise<IRequestSuccess<TResponse> | IRequestError>((res, rej) => sendRequest(res, rej, pageParam)),
|
|
72
|
-
{
|
|
73
|
-
enabled: load,
|
|
74
|
-
getNextPageParam: (lastPage, pages) => constructPaginationLink('next_page', lastPage, pages),
|
|
75
|
-
getPreviousPageParam: (lastPage, pages) => constructPaginationLink('previous_page', lastPage, pages),
|
|
76
|
-
...(queryOptions as any),
|
|
77
|
-
}
|
|
78
|
-
);
|
|
79
|
-
|
|
80
71
|
/**
|
|
81
72
|
*
|
|
82
73
|
* This pagination implementation is currently tied to our use case
|
|
83
74
|
*/
|
|
84
75
|
const constructPaginationLink = (
|
|
85
76
|
direction: 'next_page' | 'previous_page',
|
|
86
|
-
lastPage:
|
|
87
|
-
pages: IRequestSuccess<
|
|
77
|
+
lastPage: IRequestSuccess<
|
|
88
78
|
TResponse & {
|
|
89
79
|
pagination: Pagination;
|
|
90
80
|
}
|
|
91
|
-
>
|
|
81
|
+
>
|
|
92
82
|
) => {
|
|
93
|
-
const [pathname, queryString] =
|
|
83
|
+
const [pathname, queryString] = path.split('?');
|
|
94
84
|
|
|
95
85
|
const queryParams = new URLSearchParams(queryString);
|
|
96
|
-
const lastPageItem =
|
|
86
|
+
const lastPageItem = lastPage.data.pagination[direction];
|
|
97
87
|
|
|
98
|
-
queryParams.set('page', String(lastPageItem
|
|
88
|
+
queryParams.set('page', String(lastPageItem));
|
|
99
89
|
|
|
100
90
|
return pathname + '?' + queryParams.toString();
|
|
101
91
|
};
|
|
102
92
|
|
|
93
|
+
const query = useInfiniteQuery<any, any, IRequestSuccess<TResponse & { pagination: Pagination }>>(
|
|
94
|
+
[path, {}],
|
|
95
|
+
({ pageParam = path }) =>
|
|
96
|
+
new Promise<IRequestSuccess<TResponse & { pagination: Pagination }> | IRequestError>((res, rej) =>
|
|
97
|
+
sendRequest(res, rej, pageParam)
|
|
98
|
+
),
|
|
99
|
+
{
|
|
100
|
+
enabled: load,
|
|
101
|
+
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
102
|
+
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
103
|
+
...(queryOptions as any),
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
|
|
103
107
|
useEffect(() => {
|
|
104
108
|
if (keyTracker) {
|
|
105
109
|
// set expiration time for the tracker
|
|
@@ -112,10 +116,7 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
112
116
|
}
|
|
113
117
|
}, [keyTracker, path, queryClient, queryOptions?.staleTime]);
|
|
114
118
|
|
|
115
|
-
const flattenedData = query.data?.pages.flat() ?? [];
|
|
116
|
-
|
|
117
119
|
return {
|
|
118
120
|
...query,
|
|
119
|
-
flattenedData,
|
|
120
121
|
};
|
|
121
122
|
};
|