@jobber/hooks 1.13.3 → 2.0.1
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.js +24 -13
- package/dist/useAssert/index.js +2 -1
- package/dist/useAssert/useAssert.js +2 -1
- package/dist/useCollectionQuery/index.js +2 -1
- package/dist/useCollectionQuery/mdxUtils.js +19 -7
- package/dist/useCollectionQuery/test-utilities/index.js +17 -6
- package/dist/useCollectionQuery/test-utilities/mocks.d.ts +3 -3
- package/dist/useCollectionQuery/test-utilities/mocks.js +17 -17
- package/dist/useCollectionQuery/test-utilities/queries.js +57 -9
- package/dist/useCollectionQuery/test-utilities/utils.js +4 -38
- package/dist/useCollectionQuery/uniqueEdges.js +5 -4
- package/dist/useCollectionQuery/uniqueNodes.js +4 -3
- package/dist/useCollectionQuery/useCollectionQuery.d.ts +2 -2
- package/dist/useCollectionQuery/useCollectionQuery.js +60 -101
- package/dist/useCollectionQuery/useCollectionQuery.test.js +240 -429
- package/dist/useFocusTrap/index.js +2 -1
- package/dist/useFocusTrap/useFocusTrap.d.ts +0 -1
- package/dist/useFocusTrap/useFocusTrap.js +11 -10
- package/dist/useFocusTrap/useFocusTrap.test.js +19 -20
- package/dist/useFormState/index.js +2 -1
- package/dist/useFormState/useFormState.d.ts +0 -1
- package/dist/useFormState/useFormState.js +5 -20
- package/dist/useIsMounted/index.js +2 -1
- package/dist/useIsMounted/useIsMounted.js +5 -4
- package/dist/useIsMounted/useIsMounted.test.js +8 -8
- package/dist/useLiveAnnounce/index.js +2 -1
- package/dist/useLiveAnnounce/useLiveAnnounce.js +9 -24
- package/dist/useLiveAnnounce/useLiveAnnounce.test.js +36 -97
- package/dist/useOnKeyDown/index.js +2 -1
- package/dist/useOnKeyDown/useOnKeyDown.d.ts +2 -2
- package/dist/useOnKeyDown/useOnKeyDown.js +7 -6
- package/dist/useOnKeyDown/useOnKeyDown.test.js +9 -10
- package/dist/usePasswordStrength/index.js +2 -1
- package/dist/usePasswordStrength/usePasswordStrength.js +9 -8
- package/dist/useRefocusOnActivator/index.js +2 -1
- package/dist/useRefocusOnActivator/useRefocusOnActivator.js +5 -4
- package/dist/useResizeObserver/index.js +15 -4
- package/dist/useResizeObserver/useResizeObserver.d.ts +0 -1
- package/dist/useResizeObserver/useResizeObserver.js +20 -36
- package/package.json +6 -8
|
@@ -1,53 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
-
if (!m) return o;
|
|
16
|
-
var i = m.call(o), r, ar = [], e;
|
|
17
|
-
try {
|
|
18
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
-
}
|
|
20
|
-
catch (error) { e = { error: error }; }
|
|
21
|
-
finally {
|
|
22
|
-
try {
|
|
23
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
-
}
|
|
25
|
-
finally { if (e) throw e.error; }
|
|
26
|
-
}
|
|
27
|
-
return ar;
|
|
28
|
-
};
|
|
29
|
-
var __spread = (this && this.__spread) || function () {
|
|
30
|
-
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
|
|
31
|
-
return ar;
|
|
32
|
-
};
|
|
33
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
var
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
3
|
+
exports.isAlreadyUpdated = exports.useCollectionQuery = void 0;
|
|
4
|
+
const client_1 = require("@apollo/client");
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
const formatters_1 = require("@jobber/formatters");
|
|
8
|
+
const uniqueNodes_1 = require("./uniqueNodes");
|
|
9
|
+
const uniqueEdges_1 = require("./uniqueEdges");
|
|
10
|
+
const useIsMounted_1 = require("../useIsMounted");
|
|
11
|
+
function useCollectionQuery({ query, queryOptions, getCollectionByPath, subscription, }) {
|
|
12
|
+
var _a, _b;
|
|
13
|
+
const { data, loading, refetch, error, fetchMore, subscribeToMore } = (0, client_1.useQuery)(query, queryOptions);
|
|
14
|
+
const isMounted = (0, useIsMounted_1.useIsMounted)();
|
|
15
|
+
const [loadingRefresh, setLoadingRefresh] = (0, react_1.useState)(false);
|
|
16
|
+
const [loadingNextPage, setLoadingNextPage] = (0, react_1.useState)(false);
|
|
17
|
+
const loadingInitialContent = loading && !loadingRefresh && !loadingNextPage;
|
|
18
|
+
const isSearching = !!((_a = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.variables) === null || _a === void 0 ? void 0 : _a.searchTerm);
|
|
19
|
+
const refresh = (0, react_1.useCallback)(() => {
|
|
51
20
|
if (loadingInitialContent || loadingRefresh) {
|
|
52
21
|
return;
|
|
53
22
|
}
|
|
@@ -58,13 +27,10 @@ function useCollectionQuery(_a) {
|
|
|
58
27
|
// These changes can be reverted once we can update to version 3.4
|
|
59
28
|
// (the current release candidate)
|
|
60
29
|
variables: {},
|
|
61
|
-
updateQuery:
|
|
62
|
-
var fetchMoreResult = _a.fetchMoreResult;
|
|
63
|
-
return fetchMoreResult || prev;
|
|
64
|
-
},
|
|
30
|
+
updateQuery: (prev, { fetchMoreResult }) => fetchMoreResult || prev,
|
|
65
31
|
})
|
|
66
|
-
.catch(
|
|
67
|
-
.finally(
|
|
32
|
+
.catch(err => formatters_1.config.errorNotifier("Refetch Error", err))
|
|
33
|
+
.finally(() => {
|
|
68
34
|
if (isMounted.current) {
|
|
69
35
|
setLoadingRefresh(false);
|
|
70
36
|
}
|
|
@@ -76,12 +42,12 @@ function useCollectionQuery(_a) {
|
|
|
76
42
|
refetch,
|
|
77
43
|
isMounted,
|
|
78
44
|
]);
|
|
79
|
-
|
|
45
|
+
const nextPage = (0, react_1.useCallback)(() => {
|
|
80
46
|
var _a;
|
|
81
47
|
if (loadingInitialContent || loadingRefresh || loadingNextPage) {
|
|
82
48
|
return;
|
|
83
49
|
}
|
|
84
|
-
|
|
50
|
+
const pageInfo = (_a = getCollectionByPath(data)) === null || _a === void 0 ? void 0 : _a.pageInfo;
|
|
85
51
|
if (!pageInfo || !pageInfo.hasNextPage) {
|
|
86
52
|
return;
|
|
87
53
|
}
|
|
@@ -90,13 +56,10 @@ function useCollectionQuery(_a) {
|
|
|
90
56
|
variables: {
|
|
91
57
|
cursor: pageInfo.endCursor,
|
|
92
58
|
},
|
|
93
|
-
updateQuery:
|
|
94
|
-
var fetchMoreResult = _a.fetchMoreResult;
|
|
95
|
-
return fetchMoreUpdateQueryHandler(prev, fetchMoreResult, getCollectionByPath);
|
|
96
|
-
},
|
|
59
|
+
updateQuery: (prev, { fetchMoreResult }) => fetchMoreUpdateQueryHandler(prev, fetchMoreResult, getCollectionByPath),
|
|
97
60
|
})
|
|
98
|
-
.catch(
|
|
99
|
-
.finally(
|
|
61
|
+
.catch(err => formatters_1.config.errorNotifier("FetchMore Error", err))
|
|
62
|
+
.finally(() => {
|
|
100
63
|
if (isMounted.current) {
|
|
101
64
|
setLoadingNextPage(false);
|
|
102
65
|
}
|
|
@@ -111,27 +74,24 @@ function useCollectionQuery(_a) {
|
|
|
111
74
|
getCollectionByPath,
|
|
112
75
|
isMounted,
|
|
113
76
|
]);
|
|
114
|
-
react_1.useEffect(
|
|
77
|
+
(0, react_1.useEffect)(() => {
|
|
115
78
|
if (subscription == undefined)
|
|
116
79
|
return;
|
|
117
|
-
|
|
118
|
-
return subscribeToMore(
|
|
119
|
-
var subscriptionData = _a.subscriptionData;
|
|
120
|
-
return subscribeToMoreHandler(isSearching, prev, getCollectionByPath, subscriptionData === null || subscriptionData === void 0 ? void 0 : subscriptionData.data, subscription.getNodeByPath);
|
|
121
|
-
}, onError: function (err) { return formatters_1.config.errorNotifier("Subscribe to More Error", err); } }));
|
|
80
|
+
const subscriptionOptions = subscription.options || {};
|
|
81
|
+
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 => formatters_1.config.errorNotifier("Subscribe to More Error", err) }));
|
|
122
82
|
},
|
|
123
83
|
// Disabling this linter so we can force this only run once. If we didn't
|
|
124
84
|
// do this we would need to ensure subscription, subscribeToMore, and getNodeByPath
|
|
125
85
|
// all use useCallback.
|
|
126
|
-
[(
|
|
86
|
+
[(_b = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.variables) === null || _b === void 0 ? void 0 : _b.searchTerm]);
|
|
127
87
|
return {
|
|
128
|
-
data
|
|
129
|
-
error
|
|
130
|
-
refresh
|
|
131
|
-
loadingRefresh
|
|
132
|
-
nextPage
|
|
133
|
-
loadingNextPage
|
|
134
|
-
loadingInitialContent
|
|
88
|
+
data,
|
|
89
|
+
error,
|
|
90
|
+
refresh,
|
|
91
|
+
loadingRefresh,
|
|
92
|
+
nextPage,
|
|
93
|
+
loadingNextPage,
|
|
94
|
+
loadingInitialContent,
|
|
135
95
|
};
|
|
136
96
|
}
|
|
137
97
|
exports.useCollectionQuery = useCollectionQuery;
|
|
@@ -153,9 +113,9 @@ exports.useCollectionQuery = useCollectionQuery;
|
|
|
153
113
|
* makes the `useCollectionQuery` interface more complicated by adding arguments
|
|
154
114
|
*/
|
|
155
115
|
function fetchMoreUpdateQueryHandler(prev, fetchMoreResult, getCollectionByPath) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
116
|
+
const nextCollection = getCollectionByPath(fetchMoreResult);
|
|
117
|
+
const output = (0, lodash_1.cloneDeep)(prev);
|
|
118
|
+
const outputCollection = getCollectionByPath(output);
|
|
159
119
|
if (outputCollection === undefined || nextCollection === undefined) {
|
|
160
120
|
return output;
|
|
161
121
|
}
|
|
@@ -165,13 +125,13 @@ function fetchMoreUpdateQueryHandler(prev, fetchMoreResult, getCollectionByPath)
|
|
|
165
125
|
if (outputCollection.edges && nextCollection.edges) {
|
|
166
126
|
outputCollection.edges = getUpdatedEdges(outputCollection.edges, nextCollection.edges);
|
|
167
127
|
}
|
|
168
|
-
Object.assign(outputCollection,
|
|
128
|
+
Object.assign(outputCollection, Object.assign({ pageInfo: (0, lodash_1.cloneDeep)(nextCollection.pageInfo) }, getTotalCount(nextCollection.totalCount)));
|
|
169
129
|
return output;
|
|
170
130
|
}
|
|
171
131
|
function subscribeToMoreHandler(isSearching, prev, getCollectionByPath, subscriptionData, getNodeByPath) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
132
|
+
const node = getNodeByPath(subscriptionData);
|
|
133
|
+
const output = (0, lodash_1.cloneDeep)(prev);
|
|
134
|
+
const outputCollection = getCollectionByPath(output);
|
|
175
135
|
if (outputCollection == undefined || node == undefined)
|
|
176
136
|
return output;
|
|
177
137
|
if (isAlreadyUpdated(outputCollection, node) || isSearching) {
|
|
@@ -181,42 +141,41 @@ function subscribeToMoreHandler(isSearching, prev, getCollectionByPath, subscrip
|
|
|
181
141
|
outputCollection.nodes = getUpdatedNodes(outputCollection.nodes, [node], false);
|
|
182
142
|
}
|
|
183
143
|
if (outputCollection.edges) {
|
|
184
|
-
outputCollection.edges = getUpdatedEdges(outputCollection.edges, [uniqueEdges_1.createEdge(node)], false);
|
|
144
|
+
outputCollection.edges = getUpdatedEdges(outputCollection.edges, [(0, uniqueEdges_1.createEdge)(node)], false);
|
|
185
145
|
}
|
|
186
|
-
Object.assign(outputCollection,
|
|
146
|
+
Object.assign(outputCollection, Object.assign({ pageInfo: (0, lodash_1.cloneDeep)(outputCollection.pageInfo) }, getTotalCount(outputCollection.totalCount, 1)));
|
|
187
147
|
return output;
|
|
188
148
|
}
|
|
189
|
-
function getTotalCount(totalCount, additionalCount) {
|
|
190
|
-
if (additionalCount === void 0) { additionalCount = 0; }
|
|
149
|
+
function getTotalCount(totalCount, additionalCount = 0) {
|
|
191
150
|
return totalCount !== undefined
|
|
192
151
|
? { totalCount: totalCount + additionalCount }
|
|
193
152
|
: {};
|
|
194
153
|
}
|
|
195
154
|
function isAlreadyUpdated(outputCollection, newNode) {
|
|
196
|
-
|
|
197
|
-
|
|
155
|
+
let edgesAlreadyUpdated = true;
|
|
156
|
+
let nodesAlreadyUpdated = true;
|
|
198
157
|
if (outputCollection.edges) {
|
|
199
|
-
edgesAlreadyUpdated = outputCollection.edges.some(
|
|
158
|
+
edgesAlreadyUpdated = outputCollection.edges.some(edge => {
|
|
200
159
|
return edge.node.id === newNode.id;
|
|
201
160
|
});
|
|
202
161
|
}
|
|
203
162
|
if (outputCollection.nodes) {
|
|
204
|
-
nodesAlreadyUpdated = outputCollection.nodes.some(
|
|
163
|
+
nodesAlreadyUpdated = outputCollection.nodes.some(node => {
|
|
205
164
|
return node.id === newNode.id;
|
|
206
165
|
});
|
|
207
166
|
}
|
|
208
167
|
return edgesAlreadyUpdated && nodesAlreadyUpdated;
|
|
209
168
|
}
|
|
210
169
|
exports.isAlreadyUpdated = isAlreadyUpdated;
|
|
211
|
-
function getUpdatedEdges(prevEdges, nextEdges, appendToEnd) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
return uniqueEdges_1.uniqueEdges(newEdges);
|
|
170
|
+
function getUpdatedEdges(prevEdges, nextEdges, appendToEnd = true) {
|
|
171
|
+
const newEdges = appendToEnd
|
|
172
|
+
? [...prevEdges, ...nextEdges]
|
|
173
|
+
: [...nextEdges, ...prevEdges];
|
|
174
|
+
return (0, uniqueEdges_1.uniqueEdges)(newEdges);
|
|
216
175
|
}
|
|
217
|
-
function getUpdatedNodes(prevNodes, nextNodes, appendToEnd) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return uniqueNodes_1.uniqueNodes(newNodes);
|
|
176
|
+
function getUpdatedNodes(prevNodes, nextNodes, appendToEnd = true) {
|
|
177
|
+
const newNodes = appendToEnd
|
|
178
|
+
? [...prevNodes, ...nextNodes]
|
|
179
|
+
: [...nextNodes, ...prevNodes];
|
|
180
|
+
return (0, uniqueNodes_1.uniqueNodes)(newNodes);
|
|
222
181
|
}
|