@trackunit/react-graphql-hooks 0.0.270 → 0.0.271
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/index.cjs.js +30 -9
- package/index.esm.js +30 -9
- package/package.json +1 -1
package/index.cjs.js
CHANGED
|
@@ -134,9 +134,10 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
|
|
|
134
134
|
* @returns {*} {PaginationQuery}
|
|
135
135
|
*/
|
|
136
136
|
const usePaginationQuery = (document, { ...props }) => {
|
|
137
|
-
var _a;
|
|
137
|
+
var _a, _b;
|
|
138
138
|
const [lastFetchedData, setLastFetchedData] = react.useState();
|
|
139
139
|
const [resetTrigger, setResetTrigger] = react.useState(0);
|
|
140
|
+
const [abortController, setAbortController] = react.useState(new AbortController());
|
|
140
141
|
// Makes **sure** query variables are stable.
|
|
141
142
|
// Before, it required variables to always be memorized in the parent which was easy to forget and confusing.
|
|
142
143
|
// Maybe better solution exists but this is the best I could come up with without changing to much.
|
|
@@ -153,6 +154,13 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
153
154
|
}, [props]);
|
|
154
155
|
const [, { fetchMore, networkStatus }] = ApolloClient__namespace.useLazyQuery(document, {
|
|
155
156
|
...internalProps,
|
|
157
|
+
context: {
|
|
158
|
+
...internalProps.context,
|
|
159
|
+
fetchOptions: {
|
|
160
|
+
...(_a = internalProps.context) === null || _a === void 0 ? void 0 : _a.fetchOptions,
|
|
161
|
+
signal: abortController.signal,
|
|
162
|
+
},
|
|
163
|
+
},
|
|
156
164
|
notifyOnNetworkStatusChange: true, // Needed to update networkStatus
|
|
157
165
|
onCompleted: () => {
|
|
158
166
|
if (networkStatus === ApolloClient__namespace.NetworkStatus.refetch) {
|
|
@@ -171,14 +179,13 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
171
179
|
setResetTrigger(prev => prev + 1);
|
|
172
180
|
}, []);
|
|
173
181
|
const { table: { setIsLoading, isLoading, setPageInfo, pageInfo, reset, nextPage, previousPage }, variables: { first, after }, } = reactTablePagination.useRelayPagination({
|
|
174
|
-
pageSize: internalProps.pageSize || ((
|
|
182
|
+
pageSize: internalProps.pageSize || ((_b = internalProps.variables) === null || _b === void 0 ? void 0 : _b.first) || reactTablePagination.defaultPageSize,
|
|
175
183
|
onReset,
|
|
176
184
|
});
|
|
177
185
|
const doFetchMore = react.useCallback((variables, prev) => {
|
|
178
186
|
if (internalProps.skip) {
|
|
179
187
|
return;
|
|
180
188
|
}
|
|
181
|
-
const abortController = new AbortController();
|
|
182
189
|
setIsLoading(true);
|
|
183
190
|
/**
|
|
184
191
|
* To support pagination with page and pageSize, we need to convert the variables from first and after.
|
|
@@ -229,18 +236,27 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
229
236
|
setIsLoading(false);
|
|
230
237
|
return result.data;
|
|
231
238
|
},
|
|
239
|
+
// It is apparently not possible to use the onError from the useLazyQuery hook so we have to handle it here.
|
|
240
|
+
// However, if you need to pass in your own onError function, you can do so in the props of the hook.
|
|
241
|
+
// But we ignore the error if the request was aborted.
|
|
242
|
+
}).catch(error => {
|
|
243
|
+
if (abortController.signal.aborted) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (internalProps.onError) {
|
|
247
|
+
return internalProps.onError(error);
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
throw error;
|
|
251
|
+
}
|
|
232
252
|
});
|
|
233
|
-
|
|
234
|
-
}, [setIsLoading, fetchMore, internalProps, setPageInfo]);
|
|
253
|
+
}, [internalProps, setIsLoading, fetchMore, abortController, setPageInfo]);
|
|
235
254
|
react.useEffect(() => {
|
|
236
255
|
setData(undefined);
|
|
237
256
|
reset();
|
|
238
257
|
}, [internalProps.variables, reset]);
|
|
239
258
|
react.useEffect(() => {
|
|
240
|
-
|
|
241
|
-
return () => {
|
|
242
|
-
abortController === null || abortController === void 0 ? void 0 : abortController.abort();
|
|
243
|
-
};
|
|
259
|
+
doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
|
|
244
260
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
245
261
|
}, [internalProps.variables, resetTrigger]);
|
|
246
262
|
react.useEffect(() => {
|
|
@@ -249,6 +265,11 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
249
265
|
}
|
|
250
266
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
251
267
|
}, [after]);
|
|
268
|
+
react.useEffect(() => () => {
|
|
269
|
+
abortController.abort();
|
|
270
|
+
// We need ensure we reset the abort controller on unmounting, as it's not possible to re-use an aborted controller.
|
|
271
|
+
setAbortController(new AbortController());
|
|
272
|
+
}, [abortController]);
|
|
252
273
|
return react.useMemo(() => {
|
|
253
274
|
return {
|
|
254
275
|
data,
|
package/index.esm.js
CHANGED
|
@@ -113,9 +113,10 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
|
|
|
113
113
|
* @returns {*} {PaginationQuery}
|
|
114
114
|
*/
|
|
115
115
|
const usePaginationQuery = (document, { ...props }) => {
|
|
116
|
-
var _a;
|
|
116
|
+
var _a, _b;
|
|
117
117
|
const [lastFetchedData, setLastFetchedData] = useState();
|
|
118
118
|
const [resetTrigger, setResetTrigger] = useState(0);
|
|
119
|
+
const [abortController, setAbortController] = useState(new AbortController());
|
|
119
120
|
// Makes **sure** query variables are stable.
|
|
120
121
|
// Before, it required variables to always be memorized in the parent which was easy to forget and confusing.
|
|
121
122
|
// Maybe better solution exists but this is the best I could come up with without changing to much.
|
|
@@ -132,6 +133,13 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
132
133
|
}, [props]);
|
|
133
134
|
const [, { fetchMore, networkStatus }] = ApolloClient.useLazyQuery(document, {
|
|
134
135
|
...internalProps,
|
|
136
|
+
context: {
|
|
137
|
+
...internalProps.context,
|
|
138
|
+
fetchOptions: {
|
|
139
|
+
...(_a = internalProps.context) === null || _a === void 0 ? void 0 : _a.fetchOptions,
|
|
140
|
+
signal: abortController.signal,
|
|
141
|
+
},
|
|
142
|
+
},
|
|
135
143
|
notifyOnNetworkStatusChange: true, // Needed to update networkStatus
|
|
136
144
|
onCompleted: () => {
|
|
137
145
|
if (networkStatus === ApolloClient.NetworkStatus.refetch) {
|
|
@@ -150,14 +158,13 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
150
158
|
setResetTrigger(prev => prev + 1);
|
|
151
159
|
}, []);
|
|
152
160
|
const { table: { setIsLoading, isLoading, setPageInfo, pageInfo, reset, nextPage, previousPage }, variables: { first, after }, } = useRelayPagination({
|
|
153
|
-
pageSize: internalProps.pageSize || ((
|
|
161
|
+
pageSize: internalProps.pageSize || ((_b = internalProps.variables) === null || _b === void 0 ? void 0 : _b.first) || defaultPageSize,
|
|
154
162
|
onReset,
|
|
155
163
|
});
|
|
156
164
|
const doFetchMore = useCallback((variables, prev) => {
|
|
157
165
|
if (internalProps.skip) {
|
|
158
166
|
return;
|
|
159
167
|
}
|
|
160
|
-
const abortController = new AbortController();
|
|
161
168
|
setIsLoading(true);
|
|
162
169
|
/**
|
|
163
170
|
* To support pagination with page and pageSize, we need to convert the variables from first and after.
|
|
@@ -208,18 +215,27 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
208
215
|
setIsLoading(false);
|
|
209
216
|
return result.data;
|
|
210
217
|
},
|
|
218
|
+
// It is apparently not possible to use the onError from the useLazyQuery hook so we have to handle it here.
|
|
219
|
+
// However, if you need to pass in your own onError function, you can do so in the props of the hook.
|
|
220
|
+
// But we ignore the error if the request was aborted.
|
|
221
|
+
}).catch(error => {
|
|
222
|
+
if (abortController.signal.aborted) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (internalProps.onError) {
|
|
226
|
+
return internalProps.onError(error);
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
211
231
|
});
|
|
212
|
-
|
|
213
|
-
}, [setIsLoading, fetchMore, internalProps, setPageInfo]);
|
|
232
|
+
}, [internalProps, setIsLoading, fetchMore, abortController, setPageInfo]);
|
|
214
233
|
useEffect(() => {
|
|
215
234
|
setData(undefined);
|
|
216
235
|
reset();
|
|
217
236
|
}, [internalProps.variables, reset]);
|
|
218
237
|
useEffect(() => {
|
|
219
|
-
|
|
220
|
-
return () => {
|
|
221
|
-
abortController === null || abortController === void 0 ? void 0 : abortController.abort();
|
|
222
|
-
};
|
|
238
|
+
doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
|
|
223
239
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
224
240
|
}, [internalProps.variables, resetTrigger]);
|
|
225
241
|
useEffect(() => {
|
|
@@ -228,6 +244,11 @@ const usePaginationQuery = (document, { ...props }) => {
|
|
|
228
244
|
}
|
|
229
245
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
230
246
|
}, [after]);
|
|
247
|
+
useEffect(() => () => {
|
|
248
|
+
abortController.abort();
|
|
249
|
+
// We need ensure we reset the abort controller on unmounting, as it's not possible to re-use an aborted controller.
|
|
250
|
+
setAbortController(new AbortController());
|
|
251
|
+
}, [abortController]);
|
|
231
252
|
return useMemo(() => {
|
|
232
253
|
return {
|
|
233
254
|
data,
|