@jobber/hooks 2.6.2 → 2.6.3
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.
|
@@ -17,6 +17,7 @@ function useCollectionQuery({ query, queryOptions, getCollectionByPath, subscrip
|
|
|
17
17
|
const isMounted = (0, useIsMounted_1.useIsMounted)();
|
|
18
18
|
const [loadingRefresh, setLoadingRefresh] = (0, react_1.useState)(false);
|
|
19
19
|
const [loadingNextPage, setLoadingNextPage] = (0, react_1.useState)(false);
|
|
20
|
+
const [hookError, setHookError] = (0, react_1.useState)();
|
|
20
21
|
const loadingInitialContent = loading && !loadingRefresh && !loadingNextPage;
|
|
21
22
|
const isSearching = !!((_a = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.variables) === null || _a === void 0 ? void 0 : _a.searchTerm);
|
|
22
23
|
const refresh = (0, react_1.useCallback)(() => {
|
|
@@ -55,13 +56,17 @@ function useCollectionQuery({ query, queryOptions, getCollectionByPath, subscrip
|
|
|
55
56
|
return;
|
|
56
57
|
}
|
|
57
58
|
setLoadingNextPage(true);
|
|
59
|
+
setHookError(undefined);
|
|
58
60
|
fetchMore({
|
|
59
61
|
variables: {
|
|
60
62
|
cursor: pageInfo.endCursor,
|
|
61
63
|
},
|
|
62
64
|
updateQuery: (prev, { fetchMoreResult }) => fetchMoreUpdateQueryHandler(prev, fetchMoreResult, getCollectionByPath),
|
|
63
65
|
})
|
|
64
|
-
.catch(err =>
|
|
66
|
+
.catch(err => {
|
|
67
|
+
formatters_1.config.errorNotifier("FetchMore Error", err);
|
|
68
|
+
setHookError(err);
|
|
69
|
+
})
|
|
65
70
|
.finally(() => {
|
|
66
71
|
if (isMounted.current) {
|
|
67
72
|
setLoadingNextPage(false);
|
|
@@ -81,15 +86,21 @@ function useCollectionQuery({ query, queryOptions, getCollectionByPath, subscrip
|
|
|
81
86
|
if (subscription == undefined)
|
|
82
87
|
return;
|
|
83
88
|
const subscriptionOptions = subscription.options || {};
|
|
84
|
-
|
|
89
|
+
// Reset this state so we can handle errors from the subscription
|
|
90
|
+
setHookError(undefined);
|
|
91
|
+
return subscribeToMore(Object.assign(Object.assign({}, subscriptionOptions), { document: subscription.document, updateQuery: (prev, { subscriptionData }) => subscribeToMoreHandler(isSearching, prev, getCollectionByPath, subscriptionData === null || subscriptionData === void 0 ? void 0 : subscriptionData.data, subscription.getNodeByPath), onError: err => {
|
|
92
|
+
formatters_1.config.errorNotifier("Subscribe to More Error", err);
|
|
93
|
+
setHookError(err);
|
|
94
|
+
} }));
|
|
85
95
|
},
|
|
86
96
|
// Disabling this linter so we can force this only run once. If we didn't
|
|
87
97
|
// do this we would need to ensure subscription, subscribeToMore, and getNodeByPath
|
|
88
98
|
// all use useCallback.
|
|
89
99
|
[(_b = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.variables) === null || _b === void 0 ? void 0 : _b.searchTerm]);
|
|
100
|
+
const combinedError = error || hookError;
|
|
90
101
|
return {
|
|
91
102
|
data,
|
|
92
|
-
error,
|
|
103
|
+
error: combinedError,
|
|
93
104
|
refresh,
|
|
94
105
|
loadingRefresh,
|
|
95
106
|
nextPage,
|
|
@@ -213,6 +213,36 @@ describe("useCollectionQuery", () => {
|
|
|
213
213
|
yield (0, react_hooks_1.act)(test_utilities_1.wait);
|
|
214
214
|
}));
|
|
215
215
|
});
|
|
216
|
+
describe("when there is an error", () => {
|
|
217
|
+
it("should update the error state", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
218
|
+
const mockError = new Error("Failed to fetch more items");
|
|
219
|
+
const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
|
|
220
|
+
wrapper: (0, test_utilities_1.wrapper)([
|
|
221
|
+
(0, test_utilities_1.buildListRequestMock)(query, responseMock),
|
|
222
|
+
{
|
|
223
|
+
request: {
|
|
224
|
+
query: query,
|
|
225
|
+
variables: { cursor: "MZ" },
|
|
226
|
+
},
|
|
227
|
+
error: mockError,
|
|
228
|
+
},
|
|
229
|
+
(0, test_utilities_1.buildListRequestMockForNextPage)(query, responseMock),
|
|
230
|
+
]),
|
|
231
|
+
});
|
|
232
|
+
yield (0, react_hooks_1.act)(test_utilities_1.wait);
|
|
233
|
+
(0, react_hooks_1.act)(() => {
|
|
234
|
+
result.current.nextPage();
|
|
235
|
+
});
|
|
236
|
+
yield (0, react_hooks_1.act)(test_utilities_1.wait);
|
|
237
|
+
expect(result.current.error).toEqual(mockError);
|
|
238
|
+
// should clear the error after a successful fetch
|
|
239
|
+
(0, react_hooks_1.act)(() => {
|
|
240
|
+
result.current.nextPage();
|
|
241
|
+
});
|
|
242
|
+
yield (0, react_hooks_1.act)(test_utilities_1.wait);
|
|
243
|
+
expect(result.current.error).toBeUndefined();
|
|
244
|
+
}));
|
|
245
|
+
});
|
|
216
246
|
});
|
|
217
247
|
describe("#refresh", () => {
|
|
218
248
|
describe("when refresh is called while it's still loadingRefresh", () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jobber/hooks",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.js",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"@apollo/client": "^3.0.0",
|
|
46
46
|
"react": "^18"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "6e42ff7d712d09ecd2214b0fccae8c39bc51afec"
|
|
49
49
|
}
|