-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
useGetList.ts
207 lines (192 loc) · 6.28 KB
/
useGetList.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { useEffect, useMemo, useRef } from 'react';
import {
useQuery,
UseQueryOptions,
UseQueryResult,
useQueryClient,
} from '@tanstack/react-query';
import { RaRecord, GetListParams, GetListResult } from '../types';
import { useDataProvider } from './useDataProvider';
import { useEvent } from '../util';
const MAX_DATA_LENGTH_TO_CACHE = 100;
/**
* Call the dataProvider.getList() method and return the resolved result
* as well as the loading state.
*
* The return value updates according to the request state:
*
* - start: { isPending: true, refetch }
* - success: { data: [data from store], total: [total from response], isPending: false, refetch }
* - error: { error: [error from response], isPending: false, refetch }
*
* This hook will return the cached result when called a second time
* with the same parameters, until the response arrives.
*
* @param {string} resource The resource name, e.g. 'posts'
* @param {Params} params The getList parameters { pagination, sort, filter, meta }
* @param {Object} options Options object to pass to the queryClient.
* May include side effects to be executed upon success or failure, e.g. { onSuccess: () => { refresh(); } }
*
* @typedef Params
* @prop params.pagination The request pagination { page, perPage }, e.g. { page: 1, perPage: 10 }
* @prop params.sort The request sort { field, order }, e.g. { field: 'id', order: 'DESC' }
* @prop params.filter The request filters, e.g. { title: 'hello, world' }
* @prop params.meta Optional meta parameters
*
* @returns The current request state. Destructure as { data, total, error, isPending, refetch }.
*
* @example
*
* import { useGetList } from 'react-admin';
*
* const LatestNews = () => {
* const { data, total, isPending, error } = useGetList(
* 'posts',
* { pagination: { page: 1, perPage: 10 }, sort: { field: 'published_at', order: 'DESC' } }
* );
* if (isPending) { return <Loading />; }
* if (error) { return <p>ERROR</p>; }
* return <ul>{data.map(item =>
* <li key={item.id}>{item.title}</li>
* )}</ul>;
* };
*/
export const useGetList = <RecordType extends RaRecord = any>(
resource: string,
params: Partial<GetListParams> = {},
options: UseGetListOptions<RecordType> = {}
): UseGetListHookValue<RecordType> => {
const {
pagination = { page: 1, perPage: 25 },
sort = { field: 'id', order: 'DESC' },
filter = {},
meta,
} = params;
const dataProvider = useDataProvider();
const queryClient = useQueryClient();
const {
onError = noop,
onSuccess = noop,
onSettled = noop,
...queryOptions
} = options;
const onSuccessEvent = useEvent(onSuccess);
const onErrorEvent = useEvent(onError);
const onSettledEvent = useEvent(onSettled);
const result = useQuery<
GetListResult<RecordType>,
Error,
GetListResult<RecordType>
>({
queryKey: [resource, 'getList', { pagination, sort, filter, meta }],
queryFn: queryParams =>
dataProvider
.getList<RecordType>(resource, {
pagination,
sort,
filter,
meta,
signal:
dataProvider.supportAbortSignal === true
? queryParams.signal
: undefined,
})
.then(({ data, total, pageInfo }) => ({
data,
total,
pageInfo,
})),
...queryOptions,
});
const metaValue = useRef(meta);
const resourceValue = useRef(resource);
useEffect(() => {
metaValue.current = meta;
}, [meta]);
useEffect(() => {
resourceValue.current = resource;
}, [resource]);
useEffect(() => {
if (
result.data === undefined ||
result.error != null ||
result.isFetching
)
return;
// optimistically populate the getOne cache
if (
result.data?.data &&
result.data.data.length <= MAX_DATA_LENGTH_TO_CACHE
) {
result.data.data.forEach(record => {
queryClient.setQueryData(
[
resourceValue.current,
'getOne',
{ id: String(record.id), meta: metaValue.current },
],
oldRecord => oldRecord ?? record
);
});
}
onSuccessEvent(result.data);
}, [
onSuccessEvent,
queryClient,
result.data,
result.error,
result.isFetching,
]);
useEffect(() => {
if (result.error == null || result.isFetching) return;
onErrorEvent(result.error);
}, [onErrorEvent, result.error, result.isFetching]);
useEffect(() => {
if (result.status === 'pending' || result.isFetching) return;
onSettledEvent(result.data, result.error);
}, [
onSettledEvent,
result.data,
result.error,
result.status,
result.isFetching,
]);
return useMemo(
() =>
result.data
? {
...result,
data: result.data?.data,
total: result.data?.total,
pageInfo: result.data?.pageInfo,
}
: result,
[result]
) as UseQueryResult<RecordType[], Error> & {
total?: number;
pageInfo?: {
hasNextPage?: boolean;
hasPreviousPage?: boolean;
};
};
};
const noop = () => undefined;
export type UseGetListOptions<RecordType extends RaRecord = any> = Omit<
UseQueryOptions<GetListResult<RecordType>, Error>,
'queryKey' | 'queryFn'
> & {
onSuccess?: (value: GetListResult<RecordType>) => void;
onError?: (error: Error) => void;
onSettled?: (
data?: GetListResult<RecordType>,
error?: Error | null
) => void;
};
export type UseGetListHookValue<RecordType extends RaRecord = any> =
UseQueryResult<RecordType[], Error> & {
total?: number;
pageInfo?: {
hasNextPage?: boolean;
hasPreviousPage?: boolean;
};
};