@trackunit/react-graphql-hooks 1.0.15 → 1.0.17

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.
Files changed (3) hide show
  1. package/index.cjs.js +38 -13
  2. package/index.esm.js +38 -13
  3. package/package.json +1 -1
package/index.cjs.js CHANGED
@@ -79,14 +79,14 @@ const setupLibraryTranslations = () => {
79
79
  * @returns {TEdge[]} A new array with the edges from the previous array and the new array, but only if the item's key is unique.
80
80
  */
81
81
  const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
82
- const previousIds = (previousEdges === null || previousEdges === void 0 ? void 0 : previousEdges.map(edge => edge.node[key])) || [];
83
- const newIds = (newEdges === null || newEdges === void 0 ? void 0 : newEdges.map(edge => edge.node[key])) || [];
82
+ const previousIds = previousEdges?.map(edge => edge.node[key]) || [];
83
+ const newIds = newEdges?.map(edge => edge.node[key]) || [];
84
84
  const mergedIds = [...previousIds, ...newIds];
85
85
  const uniqueIds = [...new Set(mergedIds)];
86
86
  return uniqueIds
87
87
  .map(id => {
88
- const previousEdge = previousEdges === null || previousEdges === void 0 ? void 0 : previousEdges.find(edge => edge.node[key] === id);
89
- const newEdge = newEdges === null || newEdges === void 0 ? void 0 : newEdges.find(edge => edge.node[key] === id);
88
+ const previousEdge = previousEdges?.find(edge => edge.node[key] === id);
89
+ const newEdge = newEdges?.find(edge => edge.node[key] === id);
90
90
  return newEdge || previousEdge;
91
91
  })
92
92
  .filter(sharedUtils.truthy);
@@ -134,9 +134,9 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
134
134
  * @returns {*} {PaginationQuery}
135
135
  */
136
136
  const usePaginationQuery = (document, { ...props }) => {
137
- var _a;
138
137
  const [lastFetchedData, setLastFetchedData] = react.useState();
139
138
  const [resetTrigger, setResetTrigger] = react.useState(0);
139
+ const [abortController, setAbortController] = react.useState(new AbortController());
140
140
  // Makes **sure** query variables are stable.
141
141
  // Before, it required variables to always be memorized in the parent which was easy to forget and confusing.
142
142
  // Maybe better solution exists but this is the best I could come up with without changing to much.
@@ -153,6 +153,13 @@ const usePaginationQuery = (document, { ...props }) => {
153
153
  }, [props]);
154
154
  const [, { fetchMore, networkStatus }] = ApolloClient__namespace.useLazyQuery(document, {
155
155
  ...internalProps,
156
+ context: {
157
+ ...internalProps.context,
158
+ fetchOptions: {
159
+ ...internalProps.context?.fetchOptions,
160
+ signal: abortController.signal,
161
+ },
162
+ },
156
163
  notifyOnNetworkStatusChange: true, // Needed to update networkStatus
157
164
  onCompleted: () => {
158
165
  if (networkStatus === ApolloClient__namespace.NetworkStatus.refetch) {
@@ -171,14 +178,13 @@ const usePaginationQuery = (document, { ...props }) => {
171
178
  setResetTrigger(prev => prev + 1);
172
179
  }, []);
173
180
  const { table: { setIsLoading, isLoading, setPageInfo, pageInfo, reset, nextPage, previousPage }, variables: { first, after }, } = reactTablePagination.useRelayPagination({
174
- pageSize: internalProps.pageSize || ((_a = internalProps.variables) === null || _a === void 0 ? void 0 : _a.first) || reactTablePagination.defaultPageSize,
181
+ pageSize: internalProps.pageSize || internalProps.variables?.first || reactTablePagination.defaultPageSize,
175
182
  onReset,
176
183
  });
177
184
  const doFetchMore = react.useCallback((variables, prev) => {
178
185
  if (internalProps.skip) {
179
186
  return;
180
187
  }
181
- const abortController = new AbortController();
182
188
  setIsLoading(true);
183
189
  /**
184
190
  * To support pagination with page and pageSize, we need to convert the variables from first and after.
@@ -229,18 +235,27 @@ const usePaginationQuery = (document, { ...props }) => {
229
235
  setIsLoading(false);
230
236
  return result.data;
231
237
  },
238
+ // It is apparently not possible to use the onError from the useLazyQuery hook so we have to handle it here.
239
+ // However, if you need to pass in your own onError function, you can do so in the props of the hook.
240
+ // But we ignore the error if the request was aborted.
241
+ }).catch(error => {
242
+ if (abortController.signal.aborted) {
243
+ return;
244
+ }
245
+ if (internalProps.onError) {
246
+ return internalProps.onError(error);
247
+ }
248
+ else {
249
+ throw error;
250
+ }
232
251
  });
233
- return abortController;
234
- }, [setIsLoading, fetchMore, internalProps, setPageInfo]);
252
+ }, [internalProps, setIsLoading, fetchMore, abortController, setPageInfo]);
235
253
  react.useEffect(() => {
236
254
  setData(undefined);
237
255
  reset();
238
256
  }, [internalProps.variables, reset]);
239
257
  react.useEffect(() => {
240
- const abortController = doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
241
- return () => {
242
- abortController === null || abortController === void 0 ? void 0 : abortController.abort();
243
- };
258
+ doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
244
259
  // eslint-disable-next-line react-hooks/exhaustive-deps
245
260
  }, [internalProps.variables, resetTrigger]);
246
261
  react.useEffect(() => {
@@ -249,6 +264,16 @@ const usePaginationQuery = (document, { ...props }) => {
249
264
  }
250
265
  // eslint-disable-next-line react-hooks/exhaustive-deps
251
266
  }, [after]);
267
+ react.useEffect(() => {
268
+ return () => {
269
+ abortController.abort();
270
+ setAbortController(new AbortController());
271
+ };
272
+ },
273
+ // Due to issues with StritMode and useEffect cleanup, we need to disable the exhaustive-deps rule here.
274
+ // For some reason, when the rule is enabled, the cleanup function is not called when the component is unmounted properly.
275
+ // eslint-disable-next-line react-hooks/exhaustive-deps
276
+ []);
252
277
  return react.useMemo(() => {
253
278
  return {
254
279
  data,
package/index.esm.js CHANGED
@@ -58,14 +58,14 @@ const setupLibraryTranslations = () => {
58
58
  * @returns {TEdge[]} A new array with the edges from the previous array and the new array, but only if the item's key is unique.
59
59
  */
60
60
  const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
61
- const previousIds = (previousEdges === null || previousEdges === void 0 ? void 0 : previousEdges.map(edge => edge.node[key])) || [];
62
- const newIds = (newEdges === null || newEdges === void 0 ? void 0 : newEdges.map(edge => edge.node[key])) || [];
61
+ const previousIds = previousEdges?.map(edge => edge.node[key]) || [];
62
+ const newIds = newEdges?.map(edge => edge.node[key]) || [];
63
63
  const mergedIds = [...previousIds, ...newIds];
64
64
  const uniqueIds = [...new Set(mergedIds)];
65
65
  return uniqueIds
66
66
  .map(id => {
67
- const previousEdge = previousEdges === null || previousEdges === void 0 ? void 0 : previousEdges.find(edge => edge.node[key] === id);
68
- const newEdge = newEdges === null || newEdges === void 0 ? void 0 : newEdges.find(edge => edge.node[key] === id);
67
+ const previousEdge = previousEdges?.find(edge => edge.node[key] === id);
68
+ const newEdge = newEdges?.find(edge => edge.node[key] === id);
69
69
  return newEdge || previousEdge;
70
70
  })
71
71
  .filter(truthy);
@@ -113,9 +113,9 @@ const unionEdgesByNodeKey = (key, previousEdges, newEdges) => {
113
113
  * @returns {*} {PaginationQuery}
114
114
  */
115
115
  const usePaginationQuery = (document, { ...props }) => {
116
- var _a;
117
116
  const [lastFetchedData, setLastFetchedData] = useState();
118
117
  const [resetTrigger, setResetTrigger] = useState(0);
118
+ const [abortController, setAbortController] = useState(new AbortController());
119
119
  // Makes **sure** query variables are stable.
120
120
  // Before, it required variables to always be memorized in the parent which was easy to forget and confusing.
121
121
  // Maybe better solution exists but this is the best I could come up with without changing to much.
@@ -132,6 +132,13 @@ const usePaginationQuery = (document, { ...props }) => {
132
132
  }, [props]);
133
133
  const [, { fetchMore, networkStatus }] = ApolloClient.useLazyQuery(document, {
134
134
  ...internalProps,
135
+ context: {
136
+ ...internalProps.context,
137
+ fetchOptions: {
138
+ ...internalProps.context?.fetchOptions,
139
+ signal: abortController.signal,
140
+ },
141
+ },
135
142
  notifyOnNetworkStatusChange: true, // Needed to update networkStatus
136
143
  onCompleted: () => {
137
144
  if (networkStatus === ApolloClient.NetworkStatus.refetch) {
@@ -150,14 +157,13 @@ const usePaginationQuery = (document, { ...props }) => {
150
157
  setResetTrigger(prev => prev + 1);
151
158
  }, []);
152
159
  const { table: { setIsLoading, isLoading, setPageInfo, pageInfo, reset, nextPage, previousPage }, variables: { first, after }, } = useRelayPagination({
153
- pageSize: internalProps.pageSize || ((_a = internalProps.variables) === null || _a === void 0 ? void 0 : _a.first) || defaultPageSize,
160
+ pageSize: internalProps.pageSize || internalProps.variables?.first || defaultPageSize,
154
161
  onReset,
155
162
  });
156
163
  const doFetchMore = useCallback((variables, prev) => {
157
164
  if (internalProps.skip) {
158
165
  return;
159
166
  }
160
- const abortController = new AbortController();
161
167
  setIsLoading(true);
162
168
  /**
163
169
  * To support pagination with page and pageSize, we need to convert the variables from first and after.
@@ -208,18 +214,27 @@ const usePaginationQuery = (document, { ...props }) => {
208
214
  setIsLoading(false);
209
215
  return result.data;
210
216
  },
217
+ // It is apparently not possible to use the onError from the useLazyQuery hook so we have to handle it here.
218
+ // However, if you need to pass in your own onError function, you can do so in the props of the hook.
219
+ // But we ignore the error if the request was aborted.
220
+ }).catch(error => {
221
+ if (abortController.signal.aborted) {
222
+ return;
223
+ }
224
+ if (internalProps.onError) {
225
+ return internalProps.onError(error);
226
+ }
227
+ else {
228
+ throw error;
229
+ }
211
230
  });
212
- return abortController;
213
- }, [setIsLoading, fetchMore, internalProps, setPageInfo]);
231
+ }, [internalProps, setIsLoading, fetchMore, abortController, setPageInfo]);
214
232
  useEffect(() => {
215
233
  setData(undefined);
216
234
  reset();
217
235
  }, [internalProps.variables, reset]);
218
236
  useEffect(() => {
219
- const abortController = doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
220
- return () => {
221
- abortController === null || abortController === void 0 ? void 0 : abortController.abort();
222
- };
237
+ doFetchMore({ first, last: undefined, before: undefined, after: undefined }, undefined);
223
238
  // eslint-disable-next-line react-hooks/exhaustive-deps
224
239
  }, [internalProps.variables, resetTrigger]);
225
240
  useEffect(() => {
@@ -228,6 +243,16 @@ const usePaginationQuery = (document, { ...props }) => {
228
243
  }
229
244
  // eslint-disable-next-line react-hooks/exhaustive-deps
230
245
  }, [after]);
246
+ useEffect(() => {
247
+ return () => {
248
+ abortController.abort();
249
+ setAbortController(new AbortController());
250
+ };
251
+ },
252
+ // Due to issues with StritMode and useEffect cleanup, we need to disable the exhaustive-deps rule here.
253
+ // For some reason, when the rule is enabled, the cleanup function is not called when the component is unmounted properly.
254
+ // eslint-disable-next-line react-hooks/exhaustive-deps
255
+ []);
231
256
  return useMemo(() => {
232
257
  return {
233
258
  data,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-graphql-hooks",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {