@panneau/data 3.0.223 → 3.0.224
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/package.json +7 -4
- package/lib/index.js +0 -2171
package/lib/index.js
DELETED
|
@@ -1,2171 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var PropTypes = require('prop-types');
|
|
4
|
-
var React = require('react');
|
|
5
|
-
var contexts = require('@panneau/core/contexts');
|
|
6
|
-
var _createClass = require('@babel/runtime/helpers/createClass');
|
|
7
|
-
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
|
|
8
|
-
var _callSuper = require('@babel/runtime/helpers/callSuper');
|
|
9
|
-
var _inherits = require('@babel/runtime/helpers/inherits');
|
|
10
|
-
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
11
|
-
var fetch$1 = require('@folklore/fetch');
|
|
12
|
-
var pathToRegexp = require('path-to-regexp');
|
|
13
|
-
var _typeof = require('@babel/runtime/helpers/typeof');
|
|
14
|
-
var _createForOfIteratorHelper = require('@babel/runtime/helpers/createForOfIteratorHelper');
|
|
15
|
-
var reactQuery = require('@tanstack/react-query');
|
|
16
|
-
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
17
|
-
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
18
|
-
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
|
|
19
|
-
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
|
|
20
|
-
var isEmpty = require('lodash/isEmpty');
|
|
21
|
-
|
|
22
|
-
var useData = function useData(loadData) {
|
|
23
|
-
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
24
|
-
_ref$initialData = _ref.initialData,
|
|
25
|
-
initialData = _ref$initialData === void 0 ? null : _ref$initialData,
|
|
26
|
-
_ref$autoload = _ref.autoload,
|
|
27
|
-
autoload = _ref$autoload === void 0 ? true : _ref$autoload;
|
|
28
|
-
var _useState = React.useState(false),
|
|
29
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
30
|
-
loading = _useState2[0],
|
|
31
|
-
setLoading = _useState2[1];
|
|
32
|
-
var _useState3 = React.useState(false),
|
|
33
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
34
|
-
error = _useState4[0],
|
|
35
|
-
setError = _useState4[1];
|
|
36
|
-
var _useState5 = React.useState(initialData),
|
|
37
|
-
_useState6 = _slicedToArray(_useState5, 2),
|
|
38
|
-
data = _useState6[0],
|
|
39
|
-
setData = _useState6[1];
|
|
40
|
-
var load = React.useCallback(function () {
|
|
41
|
-
var canceled = false;
|
|
42
|
-
setLoading(true);
|
|
43
|
-
var promise = loadData.apply(void 0, arguments).then(function (newData) {
|
|
44
|
-
if (!canceled) {
|
|
45
|
-
setData(newData);
|
|
46
|
-
setLoading(false);
|
|
47
|
-
}
|
|
48
|
-
return newData;
|
|
49
|
-
})["catch"](function (newError) {
|
|
50
|
-
setError(newError);
|
|
51
|
-
setLoading(false);
|
|
52
|
-
});
|
|
53
|
-
promise.cancel = function () {
|
|
54
|
-
canceled = true;
|
|
55
|
-
setLoading(false);
|
|
56
|
-
};
|
|
57
|
-
return promise;
|
|
58
|
-
}, [loadData, setLoading, setData]);
|
|
59
|
-
React.useEffect(function () {
|
|
60
|
-
var loader = null;
|
|
61
|
-
if (autoload) {
|
|
62
|
-
loader = load();
|
|
63
|
-
}
|
|
64
|
-
return function () {
|
|
65
|
-
if (loader !== null) {
|
|
66
|
-
loader.cancel();
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
}, [autoload, load]);
|
|
70
|
-
return {
|
|
71
|
-
data: data,
|
|
72
|
-
load: load,
|
|
73
|
-
loading: loading,
|
|
74
|
-
error: error
|
|
75
|
-
};
|
|
76
|
-
};
|
|
77
|
-
var useData$1 = useData;
|
|
78
|
-
|
|
79
|
-
var token = '%[a-f0-9]{2}';
|
|
80
|
-
var singleMatcher = new RegExp('(' + token + ')|([^%]+?)', 'gi');
|
|
81
|
-
var multiMatcher = new RegExp('(' + token + ')+', 'gi');
|
|
82
|
-
function decodeComponents(components, split) {
|
|
83
|
-
try {
|
|
84
|
-
// Try to decode the entire string first
|
|
85
|
-
return [decodeURIComponent(components.join(''))];
|
|
86
|
-
} catch (_unused) {
|
|
87
|
-
// Do nothing
|
|
88
|
-
}
|
|
89
|
-
if (components.length === 1) {
|
|
90
|
-
return components;
|
|
91
|
-
}
|
|
92
|
-
split = split || 1;
|
|
93
|
-
|
|
94
|
-
// Split the array in 2 parts
|
|
95
|
-
var left = components.slice(0, split);
|
|
96
|
-
var right = components.slice(split);
|
|
97
|
-
return Array.prototype.concat.call([], decodeComponents(left), decodeComponents(right));
|
|
98
|
-
}
|
|
99
|
-
function decode$1(input) {
|
|
100
|
-
try {
|
|
101
|
-
return decodeURIComponent(input);
|
|
102
|
-
} catch (_unused2) {
|
|
103
|
-
var tokens = input.match(singleMatcher) || [];
|
|
104
|
-
for (var i = 1; i < tokens.length; i++) {
|
|
105
|
-
input = decodeComponents(tokens, i).join('');
|
|
106
|
-
tokens = input.match(singleMatcher) || [];
|
|
107
|
-
}
|
|
108
|
-
return input;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
function customDecodeURIComponent(input) {
|
|
112
|
-
// Keep track of all the replacements and prefill the map with the `BOM`
|
|
113
|
-
var replaceMap = {
|
|
114
|
-
'%FE%FF': "\uFFFD\uFFFD",
|
|
115
|
-
'%FF%FE': "\uFFFD\uFFFD"
|
|
116
|
-
};
|
|
117
|
-
var match = multiMatcher.exec(input);
|
|
118
|
-
while (match) {
|
|
119
|
-
try {
|
|
120
|
-
// Decode as big chunks as possible
|
|
121
|
-
replaceMap[match[0]] = decodeURIComponent(match[0]);
|
|
122
|
-
} catch (_unused3) {
|
|
123
|
-
var result = decode$1(match[0]);
|
|
124
|
-
if (result !== match[0]) {
|
|
125
|
-
replaceMap[match[0]] = result;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
match = multiMatcher.exec(input);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Add `%C2` at the end of the map to make sure it does not replace the combinator before everything else
|
|
132
|
-
replaceMap['%C2'] = "\uFFFD";
|
|
133
|
-
var entries = Object.keys(replaceMap);
|
|
134
|
-
for (var _i = 0, _entries = entries; _i < _entries.length; _i++) {
|
|
135
|
-
var key = _entries[_i];
|
|
136
|
-
// Replace all decoded components
|
|
137
|
-
input = input.replace(new RegExp(key, 'g'), replaceMap[key]);
|
|
138
|
-
}
|
|
139
|
-
return input;
|
|
140
|
-
}
|
|
141
|
-
function decodeUriComponent(encodedURI) {
|
|
142
|
-
if (typeof encodedURI !== 'string') {
|
|
143
|
-
throw new TypeError('Expected `encodedURI` to be of type `string`, got `' + _typeof(encodedURI) + '`');
|
|
144
|
-
}
|
|
145
|
-
try {
|
|
146
|
-
// Try the built in decoder first
|
|
147
|
-
return decodeURIComponent(encodedURI);
|
|
148
|
-
} catch (_unused4) {
|
|
149
|
-
// Fallback to a more advanced decoder
|
|
150
|
-
return customDecodeURIComponent(encodedURI);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function splitOnFirst(string, separator) {
|
|
155
|
-
if (!(typeof string === 'string' && typeof separator === 'string')) {
|
|
156
|
-
throw new TypeError('Expected the arguments to be of type `string`');
|
|
157
|
-
}
|
|
158
|
-
if (string === '' || separator === '') {
|
|
159
|
-
return [];
|
|
160
|
-
}
|
|
161
|
-
var separatorIndex = string.indexOf(separator);
|
|
162
|
-
if (separatorIndex === -1) {
|
|
163
|
-
return [];
|
|
164
|
-
}
|
|
165
|
-
return [string.slice(0, separatorIndex), string.slice(separatorIndex + separator.length)];
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function includeKeys(object, predicate) {
|
|
169
|
-
var result = {};
|
|
170
|
-
if (Array.isArray(predicate)) {
|
|
171
|
-
var _iterator = _createForOfIteratorHelper(predicate),
|
|
172
|
-
_step;
|
|
173
|
-
try {
|
|
174
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
175
|
-
var key = _step.value;
|
|
176
|
-
var descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
177
|
-
if (descriptor !== null && descriptor !== void 0 && descriptor.enumerable) {
|
|
178
|
-
Object.defineProperty(result, key, descriptor);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
} catch (err) {
|
|
182
|
-
_iterator.e(err);
|
|
183
|
-
} finally {
|
|
184
|
-
_iterator.f();
|
|
185
|
-
}
|
|
186
|
-
} else {
|
|
187
|
-
// `Reflect.ownKeys()` is required to retrieve symbol properties
|
|
188
|
-
var _iterator2 = _createForOfIteratorHelper(Reflect.ownKeys(object)),
|
|
189
|
-
_step2;
|
|
190
|
-
try {
|
|
191
|
-
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
192
|
-
var _key = _step2.value;
|
|
193
|
-
var _descriptor = Object.getOwnPropertyDescriptor(object, _key);
|
|
194
|
-
if (_descriptor.enumerable) {
|
|
195
|
-
var value = object[_key];
|
|
196
|
-
if (predicate(_key, value, object)) {
|
|
197
|
-
Object.defineProperty(result, _key, _descriptor);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
} catch (err) {
|
|
202
|
-
_iterator2.e(err);
|
|
203
|
-
} finally {
|
|
204
|
-
_iterator2.f();
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
return result;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const isNullOrUndefined = value => value === null || value === undefined;
|
|
211
|
-
|
|
212
|
-
// eslint-disable-next-line unicorn/prefer-code-point
|
|
213
|
-
const strictUriEncode = string => encodeURIComponent(string).replaceAll(/[!'()*]/g, x => `%${x.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
214
|
-
|
|
215
|
-
const encodeFragmentIdentifier = Symbol('encodeFragmentIdentifier');
|
|
216
|
-
|
|
217
|
-
function encoderForArrayFormat(options) {
|
|
218
|
-
switch (options.arrayFormat) {
|
|
219
|
-
case 'index': {
|
|
220
|
-
return key => (result, value) => {
|
|
221
|
-
const index = result.length;
|
|
222
|
-
|
|
223
|
-
if (
|
|
224
|
-
value === undefined
|
|
225
|
-
|| (options.skipNull && value === null)
|
|
226
|
-
|| (options.skipEmptyString && value === '')
|
|
227
|
-
) {
|
|
228
|
-
return result;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if (value === null) {
|
|
232
|
-
return [
|
|
233
|
-
...result, [encode(key, options), '[', index, ']'].join(''),
|
|
234
|
-
];
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
return [
|
|
238
|
-
...result,
|
|
239
|
-
[encode(key, options), '[', encode(index, options), ']=', encode(value, options)].join(''),
|
|
240
|
-
];
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
case 'bracket': {
|
|
245
|
-
return key => (result, value) => {
|
|
246
|
-
if (
|
|
247
|
-
value === undefined
|
|
248
|
-
|| (options.skipNull && value === null)
|
|
249
|
-
|| (options.skipEmptyString && value === '')
|
|
250
|
-
) {
|
|
251
|
-
return result;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (value === null) {
|
|
255
|
-
return [
|
|
256
|
-
...result,
|
|
257
|
-
[encode(key, options), '[]'].join(''),
|
|
258
|
-
];
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
return [
|
|
262
|
-
...result,
|
|
263
|
-
[encode(key, options), '[]=', encode(value, options)].join(''),
|
|
264
|
-
];
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
case 'colon-list-separator': {
|
|
269
|
-
return key => (result, value) => {
|
|
270
|
-
if (
|
|
271
|
-
value === undefined
|
|
272
|
-
|| (options.skipNull && value === null)
|
|
273
|
-
|| (options.skipEmptyString && value === '')
|
|
274
|
-
) {
|
|
275
|
-
return result;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (value === null) {
|
|
279
|
-
return [
|
|
280
|
-
...result,
|
|
281
|
-
[encode(key, options), ':list='].join(''),
|
|
282
|
-
];
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
return [
|
|
286
|
-
...result,
|
|
287
|
-
[encode(key, options), ':list=', encode(value, options)].join(''),
|
|
288
|
-
];
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
case 'comma':
|
|
293
|
-
case 'separator':
|
|
294
|
-
case 'bracket-separator': {
|
|
295
|
-
const keyValueSeparator = options.arrayFormat === 'bracket-separator'
|
|
296
|
-
? '[]='
|
|
297
|
-
: '=';
|
|
298
|
-
|
|
299
|
-
return key => (result, value) => {
|
|
300
|
-
if (
|
|
301
|
-
value === undefined
|
|
302
|
-
|| (options.skipNull && value === null)
|
|
303
|
-
|| (options.skipEmptyString && value === '')
|
|
304
|
-
) {
|
|
305
|
-
return result;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
// Translate null to an empty string so that it doesn't serialize as 'null'
|
|
309
|
-
value = value === null ? '' : value;
|
|
310
|
-
|
|
311
|
-
if (result.length === 0) {
|
|
312
|
-
return [[encode(key, options), keyValueSeparator, encode(value, options)].join('')];
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
return [[result, encode(value, options)].join(options.arrayFormatSeparator)];
|
|
316
|
-
};
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
default: {
|
|
320
|
-
return key => (result, value) => {
|
|
321
|
-
if (
|
|
322
|
-
value === undefined
|
|
323
|
-
|| (options.skipNull && value === null)
|
|
324
|
-
|| (options.skipEmptyString && value === '')
|
|
325
|
-
) {
|
|
326
|
-
return result;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
if (value === null) {
|
|
330
|
-
return [
|
|
331
|
-
...result,
|
|
332
|
-
encode(key, options),
|
|
333
|
-
];
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
return [
|
|
337
|
-
...result,
|
|
338
|
-
[encode(key, options), '=', encode(value, options)].join(''),
|
|
339
|
-
];
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
function parserForArrayFormat(options) {
|
|
346
|
-
let result;
|
|
347
|
-
|
|
348
|
-
switch (options.arrayFormat) {
|
|
349
|
-
case 'index': {
|
|
350
|
-
return (key, value, accumulator) => {
|
|
351
|
-
result = /\[(\d*)]$/.exec(key);
|
|
352
|
-
|
|
353
|
-
key = key.replace(/\[\d*]$/, '');
|
|
354
|
-
|
|
355
|
-
if (!result) {
|
|
356
|
-
accumulator[key] = value;
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
if (accumulator[key] === undefined) {
|
|
361
|
-
accumulator[key] = {};
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
accumulator[key][result[1]] = value;
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
case 'bracket': {
|
|
369
|
-
return (key, value, accumulator) => {
|
|
370
|
-
result = /(\[])$/.exec(key);
|
|
371
|
-
key = key.replace(/\[]$/, '');
|
|
372
|
-
|
|
373
|
-
if (!result) {
|
|
374
|
-
accumulator[key] = value;
|
|
375
|
-
return;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
if (accumulator[key] === undefined) {
|
|
379
|
-
accumulator[key] = [value];
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
accumulator[key] = [...accumulator[key], value];
|
|
384
|
-
};
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
case 'colon-list-separator': {
|
|
388
|
-
return (key, value, accumulator) => {
|
|
389
|
-
result = /(:list)$/.exec(key);
|
|
390
|
-
key = key.replace(/:list$/, '');
|
|
391
|
-
|
|
392
|
-
if (!result) {
|
|
393
|
-
accumulator[key] = value;
|
|
394
|
-
return;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
if (accumulator[key] === undefined) {
|
|
398
|
-
accumulator[key] = [value];
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
accumulator[key] = [...accumulator[key], value];
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
case 'comma':
|
|
407
|
-
case 'separator': {
|
|
408
|
-
return (key, value, accumulator) => {
|
|
409
|
-
const isArray = typeof value === 'string' && value.includes(options.arrayFormatSeparator);
|
|
410
|
-
const isEncodedArray = (typeof value === 'string' && !isArray && decode(value, options).includes(options.arrayFormatSeparator));
|
|
411
|
-
value = isEncodedArray ? decode(value, options) : value;
|
|
412
|
-
const newValue = isArray || isEncodedArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : (value === null ? value : decode(value, options));
|
|
413
|
-
accumulator[key] = newValue;
|
|
414
|
-
};
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
case 'bracket-separator': {
|
|
418
|
-
return (key, value, accumulator) => {
|
|
419
|
-
const isArray = /(\[])$/.test(key);
|
|
420
|
-
key = key.replace(/\[]$/, '');
|
|
421
|
-
|
|
422
|
-
if (!isArray) {
|
|
423
|
-
accumulator[key] = value ? decode(value, options) : value;
|
|
424
|
-
return;
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
const arrayValue = value === null
|
|
428
|
-
? []
|
|
429
|
-
: value.split(options.arrayFormatSeparator).map(item => decode(item, options));
|
|
430
|
-
|
|
431
|
-
if (accumulator[key] === undefined) {
|
|
432
|
-
accumulator[key] = arrayValue;
|
|
433
|
-
return;
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
accumulator[key] = [...accumulator[key], ...arrayValue];
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
default: {
|
|
441
|
-
return (key, value, accumulator) => {
|
|
442
|
-
if (accumulator[key] === undefined) {
|
|
443
|
-
accumulator[key] = value;
|
|
444
|
-
return;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
accumulator[key] = [...[accumulator[key]].flat(), value];
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
function validateArrayFormatSeparator(value) {
|
|
454
|
-
if (typeof value !== 'string' || value.length !== 1) {
|
|
455
|
-
throw new TypeError('arrayFormatSeparator must be single character string');
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
function encode(value, options) {
|
|
460
|
-
if (options.encode) {
|
|
461
|
-
return options.strict ? strictUriEncode(value) : encodeURIComponent(value);
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
return value;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
function decode(value, options) {
|
|
468
|
-
if (options.decode) {
|
|
469
|
-
return decodeUriComponent(value);
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
return value;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
function keysSorter(input) {
|
|
476
|
-
if (Array.isArray(input)) {
|
|
477
|
-
return input.sort();
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
if (typeof input === 'object') {
|
|
481
|
-
return keysSorter(Object.keys(input))
|
|
482
|
-
.sort((a, b) => Number(a) - Number(b))
|
|
483
|
-
.map(key => input[key]);
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
return input;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
function removeHash(input) {
|
|
490
|
-
const hashStart = input.indexOf('#');
|
|
491
|
-
if (hashStart !== -1) {
|
|
492
|
-
input = input.slice(0, hashStart);
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
return input;
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
function getHash(url) {
|
|
499
|
-
let hash = '';
|
|
500
|
-
const hashStart = url.indexOf('#');
|
|
501
|
-
if (hashStart !== -1) {
|
|
502
|
-
hash = url.slice(hashStart);
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
return hash;
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
function parseValue(value, options) {
|
|
509
|
-
if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
|
|
510
|
-
value = Number(value);
|
|
511
|
-
} else if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
|
|
512
|
-
value = value.toLowerCase() === 'true';
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
return value;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
function extract(input) {
|
|
519
|
-
input = removeHash(input);
|
|
520
|
-
const queryStart = input.indexOf('?');
|
|
521
|
-
if (queryStart === -1) {
|
|
522
|
-
return '';
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
return input.slice(queryStart + 1);
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
function parse(query, options) {
|
|
529
|
-
options = {
|
|
530
|
-
decode: true,
|
|
531
|
-
sort: true,
|
|
532
|
-
arrayFormat: 'none',
|
|
533
|
-
arrayFormatSeparator: ',',
|
|
534
|
-
parseNumbers: false,
|
|
535
|
-
parseBooleans: false,
|
|
536
|
-
...options,
|
|
537
|
-
};
|
|
538
|
-
|
|
539
|
-
validateArrayFormatSeparator(options.arrayFormatSeparator);
|
|
540
|
-
|
|
541
|
-
const formatter = parserForArrayFormat(options);
|
|
542
|
-
|
|
543
|
-
// Create an object with no prototype
|
|
544
|
-
const returnValue = Object.create(null);
|
|
545
|
-
|
|
546
|
-
if (typeof query !== 'string') {
|
|
547
|
-
return returnValue;
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
query = query.trim().replace(/^[?#&]/, '');
|
|
551
|
-
|
|
552
|
-
if (!query) {
|
|
553
|
-
return returnValue;
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
for (const parameter of query.split('&')) {
|
|
557
|
-
if (parameter === '') {
|
|
558
|
-
continue;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
const parameter_ = options.decode ? parameter.replaceAll('+', ' ') : parameter;
|
|
562
|
-
|
|
563
|
-
let [key, value] = splitOnFirst(parameter_, '=');
|
|
564
|
-
|
|
565
|
-
if (key === undefined) {
|
|
566
|
-
key = parameter_;
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
// Missing `=` should be `null`:
|
|
570
|
-
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
|
|
571
|
-
value = value === undefined ? null : (['comma', 'separator', 'bracket-separator'].includes(options.arrayFormat) ? value : decode(value, options));
|
|
572
|
-
formatter(decode(key, options), value, returnValue);
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
for (const [key, value] of Object.entries(returnValue)) {
|
|
576
|
-
if (typeof value === 'object' && value !== null) {
|
|
577
|
-
for (const [key2, value2] of Object.entries(value)) {
|
|
578
|
-
value[key2] = parseValue(value2, options);
|
|
579
|
-
}
|
|
580
|
-
} else {
|
|
581
|
-
returnValue[key] = parseValue(value, options);
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
if (options.sort === false) {
|
|
586
|
-
return returnValue;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
// TODO: Remove the use of `reduce`.
|
|
590
|
-
// eslint-disable-next-line unicorn/no-array-reduce
|
|
591
|
-
return (options.sort === true ? Object.keys(returnValue).sort() : Object.keys(returnValue).sort(options.sort)).reduce((result, key) => {
|
|
592
|
-
const value = returnValue[key];
|
|
593
|
-
result[key] = Boolean(value) && typeof value === 'object' && !Array.isArray(value) ? keysSorter(value) : value;
|
|
594
|
-
return result;
|
|
595
|
-
}, Object.create(null));
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
function stringify(object, options) {
|
|
599
|
-
if (!object) {
|
|
600
|
-
return '';
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
options = {
|
|
604
|
-
encode: true,
|
|
605
|
-
strict: true,
|
|
606
|
-
arrayFormat: 'none',
|
|
607
|
-
arrayFormatSeparator: ',',
|
|
608
|
-
...options,
|
|
609
|
-
};
|
|
610
|
-
|
|
611
|
-
validateArrayFormatSeparator(options.arrayFormatSeparator);
|
|
612
|
-
|
|
613
|
-
const shouldFilter = key => (
|
|
614
|
-
(options.skipNull && isNullOrUndefined(object[key]))
|
|
615
|
-
|| (options.skipEmptyString && object[key] === '')
|
|
616
|
-
);
|
|
617
|
-
|
|
618
|
-
const formatter = encoderForArrayFormat(options);
|
|
619
|
-
|
|
620
|
-
const objectCopy = {};
|
|
621
|
-
|
|
622
|
-
for (const [key, value] of Object.entries(object)) {
|
|
623
|
-
if (!shouldFilter(key)) {
|
|
624
|
-
objectCopy[key] = value;
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
const keys = Object.keys(objectCopy);
|
|
629
|
-
|
|
630
|
-
if (options.sort !== false) {
|
|
631
|
-
keys.sort(options.sort);
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
return keys.map(key => {
|
|
635
|
-
const value = object[key];
|
|
636
|
-
|
|
637
|
-
if (value === undefined) {
|
|
638
|
-
return '';
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
if (value === null) {
|
|
642
|
-
return encode(key, options);
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
if (Array.isArray(value)) {
|
|
646
|
-
if (value.length === 0 && options.arrayFormat === 'bracket-separator') {
|
|
647
|
-
return encode(key, options) + '[]';
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
return value
|
|
651
|
-
.reduce(formatter(key), [])
|
|
652
|
-
.join('&');
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
return encode(key, options) + '=' + encode(value, options);
|
|
656
|
-
}).filter(x => x.length > 0).join('&');
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
function parseUrl(url, options) {
|
|
660
|
-
options = {
|
|
661
|
-
decode: true,
|
|
662
|
-
...options,
|
|
663
|
-
};
|
|
664
|
-
|
|
665
|
-
let [url_, hash] = splitOnFirst(url, '#');
|
|
666
|
-
|
|
667
|
-
if (url_ === undefined) {
|
|
668
|
-
url_ = url;
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
return {
|
|
672
|
-
url: url_?.split('?')?.[0] ?? '',
|
|
673
|
-
query: parse(extract(url), options),
|
|
674
|
-
...(options && options.parseFragmentIdentifier && hash ? {fragmentIdentifier: decode(hash, options)} : {}),
|
|
675
|
-
};
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
function stringifyUrl(object, options) {
|
|
679
|
-
options = {
|
|
680
|
-
encode: true,
|
|
681
|
-
strict: true,
|
|
682
|
-
[encodeFragmentIdentifier]: true,
|
|
683
|
-
...options,
|
|
684
|
-
};
|
|
685
|
-
|
|
686
|
-
const url = removeHash(object.url).split('?')[0] || '';
|
|
687
|
-
const queryFromUrl = extract(object.url);
|
|
688
|
-
|
|
689
|
-
const query = {
|
|
690
|
-
...parse(queryFromUrl, {sort: false}),
|
|
691
|
-
...object.query,
|
|
692
|
-
};
|
|
693
|
-
|
|
694
|
-
let queryString = stringify(query, options);
|
|
695
|
-
queryString &&= `?${queryString}`;
|
|
696
|
-
|
|
697
|
-
let hash = getHash(object.url);
|
|
698
|
-
if (typeof object.fragmentIdentifier === 'string') {
|
|
699
|
-
const urlObjectForFragmentEncode = new URL(url);
|
|
700
|
-
urlObjectForFragmentEncode.hash = object.fragmentIdentifier;
|
|
701
|
-
hash = options[encodeFragmentIdentifier] ? urlObjectForFragmentEncode.hash : `#${object.fragmentIdentifier}`;
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
return `${url}${queryString}${hash}`;
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
function pick(input, filter, options) {
|
|
708
|
-
options = {
|
|
709
|
-
parseFragmentIdentifier: true,
|
|
710
|
-
[encodeFragmentIdentifier]: false,
|
|
711
|
-
...options,
|
|
712
|
-
};
|
|
713
|
-
|
|
714
|
-
const {url, query, fragmentIdentifier} = parseUrl(input, options);
|
|
715
|
-
|
|
716
|
-
return stringifyUrl({
|
|
717
|
-
url,
|
|
718
|
-
query: includeKeys(query, filter),
|
|
719
|
-
fragmentIdentifier,
|
|
720
|
-
}, options);
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
function exclude(input, filter, options) {
|
|
724
|
-
const exclusionFilter = Array.isArray(filter) ? key => !filter.includes(key) : (key, value) => !filter(key, value);
|
|
725
|
-
|
|
726
|
-
return pick(input, exclusionFilter, options);
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
var queryString = /*#__PURE__*/Object.freeze({
|
|
730
|
-
__proto__: null,
|
|
731
|
-
exclude: exclude,
|
|
732
|
-
extract: extract,
|
|
733
|
-
parse: parse,
|
|
734
|
-
parseUrl: parseUrl,
|
|
735
|
-
pick: pick,
|
|
736
|
-
stringify: stringify,
|
|
737
|
-
stringifyUrl: stringifyUrl
|
|
738
|
-
});
|
|
739
|
-
|
|
740
|
-
var _excluded$3 = ["store", "url", "paginated", "query", "onQueryChange", "queryConfig", "keepData"],
|
|
741
|
-
_excluded2$1 = ["page", "count"],
|
|
742
|
-
_excluded3 = ["data", "refetch", "isLoading", "isFetching", "isRefetching", "isFetched", "status", "error"];
|
|
743
|
-
|
|
744
|
-
// For backwards compatibility
|
|
745
|
-
|
|
746
|
-
var useItems$2 = function useItems(_ref) {
|
|
747
|
-
var store = _ref.store,
|
|
748
|
-
url = _ref.url,
|
|
749
|
-
_ref$paginated = _ref.paginated,
|
|
750
|
-
initialPaginated = _ref$paginated === void 0 ? false : _ref$paginated,
|
|
751
|
-
_ref$query = _ref.query,
|
|
752
|
-
initialQuery = _ref$query === void 0 ? null : _ref$query,
|
|
753
|
-
onQueryChange = _ref.onQueryChange,
|
|
754
|
-
_ref$queryConfig = _ref.queryConfig,
|
|
755
|
-
queryConfig = _ref$queryConfig === void 0 ? null : _ref$queryConfig,
|
|
756
|
-
_ref$keepData = _ref.keepData,
|
|
757
|
-
keepData = _ref$keepData === void 0 ? true : _ref$keepData,
|
|
758
|
-
oldProps = _objectWithoutProperties(_ref, _excluded$3);
|
|
759
|
-
// Legacy compatibility
|
|
760
|
-
var _ref2 = oldProps || {},
|
|
761
|
-
_ref2$getPage = _ref2.getPage,
|
|
762
|
-
getPage = _ref2$getPage === void 0 ? null : _ref2$getPage,
|
|
763
|
-
_ref2$getItems = _ref2.getItems,
|
|
764
|
-
getItems = _ref2$getItems === void 0 ? null : _ref2$getItems,
|
|
765
|
-
_ref2$page = _ref2.page,
|
|
766
|
-
oldPage = _ref2$page === void 0 ? null : _ref2$page,
|
|
767
|
-
_ref2$count = _ref2.count,
|
|
768
|
-
oldCount = _ref2$count === void 0 ? null : _ref2$count;
|
|
769
|
-
_ref2.items;
|
|
770
|
-
var _ref2$pages = _ref2.pages,
|
|
771
|
-
initialPages = _ref2$pages === void 0 ? null : _ref2$pages,
|
|
772
|
-
_ref2$getPageFromResp = _ref2.getPageFromResponse,
|
|
773
|
-
getPageFromResponse = _ref2$getPageFromResp === void 0 ? function () {
|
|
774
|
-
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
775
|
-
_ref3$pagination = _ref3.pagination,
|
|
776
|
-
_ref3$pagination$page = _ref3$pagination.page,
|
|
777
|
-
currentPage = _ref3$pagination$page === void 0 ? null : _ref3$pagination$page,
|
|
778
|
-
_ref3$pagination$last = _ref3$pagination.last_page,
|
|
779
|
-
lastPage = _ref3$pagination$last === void 0 ? null : _ref3$pagination$last,
|
|
780
|
-
_ref3$pagination$tota = _ref3$pagination.total,
|
|
781
|
-
total = _ref3$pagination$tota === void 0 ? null : _ref3$pagination$tota,
|
|
782
|
-
_ref3$pagination$per_ = _ref3$pagination.per_page,
|
|
783
|
-
perPage = _ref3$pagination$per_ === void 0 ? null : _ref3$pagination$per_,
|
|
784
|
-
_ref3$data = _ref3.data,
|
|
785
|
-
items = _ref3$data === void 0 ? null : _ref3$data;
|
|
786
|
-
return {
|
|
787
|
-
page: parseInt(currentPage, 10),
|
|
788
|
-
lastPage: parseInt(lastPage, 10),
|
|
789
|
-
total: parseInt(total, 10),
|
|
790
|
-
count: parseInt(perPage, 10),
|
|
791
|
-
items: items
|
|
792
|
-
};
|
|
793
|
-
} : _ref2$getPageFromResp,
|
|
794
|
-
_ref2$getItemsFromRes = _ref2.getItemsFromResponse,
|
|
795
|
-
getItemsFromResponse = _ref2$getItemsFromRes === void 0 ? function (data) {
|
|
796
|
-
return data;
|
|
797
|
-
} : _ref2$getItemsFromRes,
|
|
798
|
-
_ref2$onItemsLoaded = _ref2.onItemsLoaded,
|
|
799
|
-
onItemsLoaded = _ref2$onItemsLoaded === void 0 ? null : _ref2$onItemsLoaded,
|
|
800
|
-
_ref2$onPageLoaded = _ref2.onPageLoaded,
|
|
801
|
-
onPageLoaded = _ref2$onPageLoaded === void 0 ? null : _ref2$onPageLoaded,
|
|
802
|
-
_ref2$onLoaded = _ref2.onLoaded,
|
|
803
|
-
onLoaded = _ref2$onLoaded === void 0 ? null : _ref2$onLoaded,
|
|
804
|
-
_ref2$onError = _ref2.onError,
|
|
805
|
-
onError = _ref2$onError === void 0 ? null : _ref2$onError;
|
|
806
|
-
var paginated = React.useMemo(function () {
|
|
807
|
-
return getPage !== null || initialPages !== null || initialPaginated;
|
|
808
|
-
}, [getPage, initialPages, initialPaginated]);
|
|
809
|
-
var query = React.useMemo(function () {
|
|
810
|
-
// Backwards compatible
|
|
811
|
-
if (oldPage !== null || oldCount !== null) {
|
|
812
|
-
return _objectSpread({
|
|
813
|
-
page: oldPage,
|
|
814
|
-
count: oldCount
|
|
815
|
-
}, initialQuery);
|
|
816
|
-
}
|
|
817
|
-
return initialQuery;
|
|
818
|
-
}, [initialQuery]);
|
|
819
|
-
var _ref4 = query || {},
|
|
820
|
-
_ref4$page = _ref4.page,
|
|
821
|
-
page = _ref4$page === void 0 ? null : _ref4$page,
|
|
822
|
-
_ref4$count = _ref4.count,
|
|
823
|
-
count = _ref4$count === void 0 ? null : _ref4$count,
|
|
824
|
-
queryWithoutPageAndCount = _objectWithoutProperties(_ref4, _excluded2$1);
|
|
825
|
-
var queryKey = React.useMemo(function () {
|
|
826
|
-
return paginated && getPage !== null ? [store, page, count || 10, queryWithoutPageAndCount] : [store, query];
|
|
827
|
-
}, [paginated, page, count, queryWithoutPageAndCount]);
|
|
828
|
-
var _useQuery = reactQuery.useQuery(_objectSpread(_objectSpread({
|
|
829
|
-
queryKey: queryKey,
|
|
830
|
-
queryFn: function queryFn(_ref5) {
|
|
831
|
-
var key = _ref5.queryKey;
|
|
832
|
-
if (paginated && getPage !== null) {
|
|
833
|
-
var _key = _slicedToArray(key, 4),
|
|
834
|
-
_key$ = _key[1],
|
|
835
|
-
p = _key$ === void 0 ? null : _key$,
|
|
836
|
-
_key$2 = _key[2],
|
|
837
|
-
c = _key$2 === void 0 ? null : _key$2,
|
|
838
|
-
_key$3 = _key[3],
|
|
839
|
-
_q = _key$3 === void 0 ? null : _key$3;
|
|
840
|
-
return getPage(p, c, _q);
|
|
841
|
-
}
|
|
842
|
-
if (!paginated && getItems !== null) {
|
|
843
|
-
var _key2 = _slicedToArray(key, 2),
|
|
844
|
-
_key2$ = _key2[1],
|
|
845
|
-
_q2 = _key2$ === void 0 ? null : _key2$;
|
|
846
|
-
return getItems(_q2);
|
|
847
|
-
}
|
|
848
|
-
var _key3 = _slicedToArray(key, 2),
|
|
849
|
-
_key3$ = _key3[1],
|
|
850
|
-
q = _key3$ === void 0 ? null : _key3$;
|
|
851
|
-
return fetch$1.getJSON("".concat(url).concat(!isEmpty(q) ? "?".concat(queryString.stringify(q, {
|
|
852
|
-
arrayFormat: 'bracket'
|
|
853
|
-
})) : ''));
|
|
854
|
-
}
|
|
855
|
-
}, keepData ? {
|
|
856
|
-
placeholderData: reactQuery.keepPreviousData
|
|
857
|
-
} : null), queryConfig)),
|
|
858
|
-
_useQuery$data = _useQuery.data,
|
|
859
|
-
data = _useQuery$data === void 0 ? null : _useQuery$data,
|
|
860
|
-
reload = _useQuery.refetch,
|
|
861
|
-
isLoading = _useQuery.isLoading,
|
|
862
|
-
isFetching = _useQuery.isFetching,
|
|
863
|
-
isRefetching = _useQuery.isRefetching,
|
|
864
|
-
isFetched = _useQuery.isFetched,
|
|
865
|
-
_useQuery$status = _useQuery.status,
|
|
866
|
-
status = _useQuery$status === void 0 ? null : _useQuery$status,
|
|
867
|
-
_useQuery$error = _useQuery.error,
|
|
868
|
-
error = _useQuery$error === void 0 ? null : _useQuery$error,
|
|
869
|
-
otherProps = _objectWithoutProperties(_useQuery, _excluded3);
|
|
870
|
-
var _ref6 = data || {},
|
|
871
|
-
_ref6$data = _ref6.data,
|
|
872
|
-
items = _ref6$data === void 0 ? [] : _ref6$data,
|
|
873
|
-
_ref6$pagination = _ref6.pagination,
|
|
874
|
-
pagination = _ref6$pagination === void 0 ? null : _ref6$pagination,
|
|
875
|
-
_ref6$meta = _ref6.meta,
|
|
876
|
-
meta = _ref6$meta === void 0 ? null : _ref6$meta;
|
|
877
|
-
var _ref7 = pagination || meta || {},
|
|
878
|
-
_ref7$page = _ref7.page,
|
|
879
|
-
currentPage = _ref7$page === void 0 ? null : _ref7$page,
|
|
880
|
-
_ref7$last_page = _ref7.last_page,
|
|
881
|
-
lastPage = _ref7$last_page === void 0 ? null : _ref7$last_page,
|
|
882
|
-
_ref7$total = _ref7.total,
|
|
883
|
-
total = _ref7$total === void 0 ? null : _ref7$total;
|
|
884
|
-
var loadNextPage = React.useCallback(function () {
|
|
885
|
-
if (onQueryChange !== null && paginated && page !== null && page < lastPage) {
|
|
886
|
-
onQueryChange(_objectSpread(_objectSpread({}, query), {}, {
|
|
887
|
-
page: page + 1
|
|
888
|
-
}));
|
|
889
|
-
}
|
|
890
|
-
}, [paginated, pagination, page]);
|
|
891
|
-
var loadPage = React.useCallback(function (newPage) {
|
|
892
|
-
if (onQueryChange !== null && newPage !== null) {
|
|
893
|
-
onQueryChange(_objectSpread(_objectSpread({}, query), {}, {
|
|
894
|
-
page: newPage
|
|
895
|
-
}));
|
|
896
|
-
}
|
|
897
|
-
}, [query, onQueryChange]);
|
|
898
|
-
|
|
899
|
-
// Keep a list of updated items
|
|
900
|
-
var _useState = React.useState([]),
|
|
901
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
902
|
-
updatedItems = _useState2[0],
|
|
903
|
-
setUpdatedItems = _useState2[1];
|
|
904
|
-
var updateItem = React.useCallback(function (item) {
|
|
905
|
-
var _ref8 = item || {},
|
|
906
|
-
_ref8$id = _ref8.id,
|
|
907
|
-
itemId = _ref8$id === void 0 ? null : _ref8$id;
|
|
908
|
-
if (itemId !== null) {
|
|
909
|
-
setUpdatedItems([].concat(_toConsumableArray((updatedItems || []).filter(function () {
|
|
910
|
-
var _ref9 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
911
|
-
_ref9$id = _ref9.id,
|
|
912
|
-
id = _ref9$id === void 0 ? null : _ref9$id;
|
|
913
|
-
return id !== itemId;
|
|
914
|
-
})), [item]));
|
|
915
|
-
}
|
|
916
|
-
}, [updatedItems, setUpdatedItems]);
|
|
917
|
-
var replaceUpdatedItems = React.useCallback(function (currentItems) {
|
|
918
|
-
if (currentItems === null || updatedItems === null || updatedItems.length === 0) {
|
|
919
|
-
return currentItems;
|
|
920
|
-
}
|
|
921
|
-
return (currentItems || []).map(function (item) {
|
|
922
|
-
var _ref10 = item || {},
|
|
923
|
-
_ref10$id = _ref10.id,
|
|
924
|
-
itemId = _ref10$id === void 0 ? null : _ref10$id;
|
|
925
|
-
var updated = (updatedItems || []).find(function () {
|
|
926
|
-
var _ref11 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
927
|
-
_ref11$id = _ref11.id,
|
|
928
|
-
id = _ref11$id === void 0 ? null : _ref11$id;
|
|
929
|
-
return id === itemId;
|
|
930
|
-
}) || null;
|
|
931
|
-
if (updated !== null) {
|
|
932
|
-
return updated;
|
|
933
|
-
}
|
|
934
|
-
return item;
|
|
935
|
-
}, []);
|
|
936
|
-
}, [updatedItems]);
|
|
937
|
-
var partialItems = React.useMemo(function () {
|
|
938
|
-
if (data === null) {
|
|
939
|
-
return data;
|
|
940
|
-
}
|
|
941
|
-
if (paginated) {
|
|
942
|
-
if (getPageFromResponse !== null) {
|
|
943
|
-
return getPageFromResponse(items);
|
|
944
|
-
}
|
|
945
|
-
return items;
|
|
946
|
-
}
|
|
947
|
-
if (getItemsFromResponse !== null) {
|
|
948
|
-
return getItemsFromResponse(data);
|
|
949
|
-
}
|
|
950
|
-
return data;
|
|
951
|
-
}, [items, data, paginated, getPageFromResponse, getItemsFromResponse, updatedItems]);
|
|
952
|
-
var finalItems = replaceUpdatedItems(partialItems);
|
|
953
|
-
|
|
954
|
-
// Pseudo-events for compatibility
|
|
955
|
-
React.useEffect(function () {
|
|
956
|
-
if (status === 'success') {
|
|
957
|
-
if (onPageLoaded !== null && paginated) {
|
|
958
|
-
onItemsLoaded(data);
|
|
959
|
-
}
|
|
960
|
-
if (onItemsLoaded !== null && !paginated) {
|
|
961
|
-
onItemsLoaded(data);
|
|
962
|
-
}
|
|
963
|
-
if (onLoaded !== null) {
|
|
964
|
-
onLoaded(data);
|
|
965
|
-
}
|
|
966
|
-
}
|
|
967
|
-
if (status === 'error' && onError !== null) {
|
|
968
|
-
onError(error);
|
|
969
|
-
}
|
|
970
|
-
}, [status]);
|
|
971
|
-
|
|
972
|
-
// Keep a list of pages, useEffect wont work here because delayed
|
|
973
|
-
var pages = React.useRef(null);
|
|
974
|
-
if (isFetched && page !== null && data !== null) {
|
|
975
|
-
pages.current = _objectSpread(_objectSpread({}, pages.current), {}, _defineProperty({}, page, data));
|
|
976
|
-
}
|
|
977
|
-
var allItems = pages.current !== null ? replaceUpdatedItems(Object.keys(pages.current).flatMap(function (k) {
|
|
978
|
-
var _pages$current$k;
|
|
979
|
-
return (_pages$current$k = pages.current[k]) === null || _pages$current$k === void 0 ? void 0 : _pages$current$k.data;
|
|
980
|
-
})) : null;
|
|
981
|
-
var finalLoading = isLoading || isFetching || isRefetching;
|
|
982
|
-
return _objectSpread(_objectSpread({}, otherProps), {}, {
|
|
983
|
-
items: finalItems,
|
|
984
|
-
allItems: allItems,
|
|
985
|
-
pages: pages.current,
|
|
986
|
-
pageItems: paginated ? finalItems : null,
|
|
987
|
-
pagination: paginated ? {
|
|
988
|
-
page: currentPage,
|
|
989
|
-
lastPage: lastPage,
|
|
990
|
-
total: total
|
|
991
|
-
} : null,
|
|
992
|
-
total: total,
|
|
993
|
-
lastPage: lastPage,
|
|
994
|
-
loaded: isFetched,
|
|
995
|
-
allLoaded: paginated && pages.current !== null && Object.keys(pages.current).length === lastPage || !paginated && isFetched,
|
|
996
|
-
// very basic stuff, TODO
|
|
997
|
-
loading: finalLoading,
|
|
998
|
-
loadNextPage: loadNextPage,
|
|
999
|
-
loadPage: loadPage,
|
|
1000
|
-
reloadPage: reload,
|
|
1001
|
-
reload: reload,
|
|
1002
|
-
reset: reload,
|
|
1003
|
-
updateItem: updateItem,
|
|
1004
|
-
status: status,
|
|
1005
|
-
error: error
|
|
1006
|
-
});
|
|
1007
|
-
};
|
|
1008
|
-
var useItems$3 = useItems$2;
|
|
1009
|
-
|
|
1010
|
-
var useItems = function useItems(_ref) {
|
|
1011
|
-
var _ref$getPage = _ref.getPage,
|
|
1012
|
-
getPage = _ref$getPage === void 0 ? null : _ref$getPage,
|
|
1013
|
-
_ref$getItems = _ref.getItems,
|
|
1014
|
-
getItems = _ref$getItems === void 0 ? null : _ref$getItems,
|
|
1015
|
-
_ref$page = _ref.page,
|
|
1016
|
-
page = _ref$page === void 0 ? null : _ref$page,
|
|
1017
|
-
_ref$count = _ref.count,
|
|
1018
|
-
count = _ref$count === void 0 ? 10 : _ref$count,
|
|
1019
|
-
_ref$items = _ref.items,
|
|
1020
|
-
providedItems = _ref$items === void 0 ? null : _ref$items,
|
|
1021
|
-
_ref$pages = _ref.pages,
|
|
1022
|
-
initialPages = _ref$pages === void 0 ? null : _ref$pages,
|
|
1023
|
-
_ref$getPageFromRespo = _ref.getPageFromResponse,
|
|
1024
|
-
getPageFromResponse = _ref$getPageFromRespo === void 0 ? function (_ref2) {
|
|
1025
|
-
var _ref2$pagination = _ref2.pagination,
|
|
1026
|
-
currentPage = _ref2$pagination.page,
|
|
1027
|
-
lastPage = _ref2$pagination.last_page,
|
|
1028
|
-
total = _ref2$pagination.total,
|
|
1029
|
-
perPage = _ref2$pagination.per_page,
|
|
1030
|
-
items = _ref2.data;
|
|
1031
|
-
return {
|
|
1032
|
-
page: parseInt(currentPage, 10),
|
|
1033
|
-
lastPage: parseInt(lastPage, 10),
|
|
1034
|
-
total: parseInt(total, 10),
|
|
1035
|
-
count: parseInt(perPage, 10),
|
|
1036
|
-
items: items
|
|
1037
|
-
};
|
|
1038
|
-
} : _ref$getPageFromRespo,
|
|
1039
|
-
_ref$getItemsFromResp = _ref.getItemsFromResponse,
|
|
1040
|
-
getItemsFromResponse = _ref$getItemsFromResp === void 0 ? function (data) {
|
|
1041
|
-
return data;
|
|
1042
|
-
} : _ref$getItemsFromResp,
|
|
1043
|
-
_ref$onItemsLoaded = _ref.onItemsLoaded,
|
|
1044
|
-
onItemsLoaded = _ref$onItemsLoaded === void 0 ? null : _ref$onItemsLoaded,
|
|
1045
|
-
_ref$onPageLoaded = _ref.onPageLoaded,
|
|
1046
|
-
onPageLoaded = _ref$onPageLoaded === void 0 ? null : _ref$onPageLoaded,
|
|
1047
|
-
_ref$onLoaded = _ref.onLoaded,
|
|
1048
|
-
onLoaded = _ref$onLoaded === void 0 ? null : _ref$onLoaded,
|
|
1049
|
-
_ref$onError = _ref.onError,
|
|
1050
|
-
onError = _ref$onError === void 0 ? null : _ref$onError;
|
|
1051
|
-
var isPaginated = getPage !== null || initialPages !== null;
|
|
1052
|
-
var lastState = React.useRef(null);
|
|
1053
|
-
var initialState = React.useMemo(function () {
|
|
1054
|
-
var finalInitialPages = initialPages !== null ? initialPages.map(function (it) {
|
|
1055
|
-
return getPageFromResponse(it);
|
|
1056
|
-
}) : null;
|
|
1057
|
-
return {
|
|
1058
|
-
lastPage: finalInitialPages !== null ? finalInitialPages.reduce(function (currentLastPage, _ref3) {
|
|
1059
|
-
var initialLastPage = _ref3.lastPage;
|
|
1060
|
-
return initialLastPage > currentLastPage ? initialLastPage : currentLastPage;
|
|
1061
|
-
}, -1) : -1,
|
|
1062
|
-
total: finalInitialPages !== null ? finalInitialPages[0].total : (providedItems || []).length,
|
|
1063
|
-
loaded: providedItems !== null,
|
|
1064
|
-
loading: false,
|
|
1065
|
-
pages: finalInitialPages !== null ? finalInitialPages : null,
|
|
1066
|
-
items: null
|
|
1067
|
-
};
|
|
1068
|
-
}, [initialPages, providedItems]);
|
|
1069
|
-
var _useState = React.useState(initialState),
|
|
1070
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
1071
|
-
state = _useState2[0],
|
|
1072
|
-
setState = _useState2[1];
|
|
1073
|
-
var lastPage = state.lastPage,
|
|
1074
|
-
loaded = state.loaded,
|
|
1075
|
-
loading = state.loading,
|
|
1076
|
-
stateItems = state.items,
|
|
1077
|
-
pages = state.pages,
|
|
1078
|
-
total = state.total;
|
|
1079
|
-
var items = providedItems || (isPaginated && pages !== null ? pages.reduce(function (pagesItems, _ref4) {
|
|
1080
|
-
var pageItems = _ref4.items;
|
|
1081
|
-
return pagesItems.concat(pageItems);
|
|
1082
|
-
}, []) : stateItems) || null;
|
|
1083
|
-
var updateState = function updateState(update) {
|
|
1084
|
-
return setState(_objectSpread(_objectSpread({}, state), update));
|
|
1085
|
-
};
|
|
1086
|
-
var updateFromResponse = function updateFromResponse(response) {
|
|
1087
|
-
var error = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
1088
|
-
var reset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
1089
|
-
if (error !== null) {
|
|
1090
|
-
updateState({
|
|
1091
|
-
loaded: false,
|
|
1092
|
-
loading: false
|
|
1093
|
-
});
|
|
1094
|
-
throw error;
|
|
1095
|
-
}
|
|
1096
|
-
if (isPaginated) {
|
|
1097
|
-
var newPage = getPageFromResponse(response);
|
|
1098
|
-
var newPages = (reset ? [newPage] : [].concat(_toConsumableArray((pages || []).filter(function (it) {
|
|
1099
|
-
return it.page !== newPage.page;
|
|
1100
|
-
})), [newPage])).sort(function (a, b) {
|
|
1101
|
-
if (a === b) {
|
|
1102
|
-
return 0;
|
|
1103
|
-
}
|
|
1104
|
-
return a > b ? 1 : -1;
|
|
1105
|
-
});
|
|
1106
|
-
updateState({
|
|
1107
|
-
loaded: true,
|
|
1108
|
-
loading: false,
|
|
1109
|
-
lastPage: newPage.lastPage,
|
|
1110
|
-
total: newPage.total,
|
|
1111
|
-
pages: newPages
|
|
1112
|
-
});
|
|
1113
|
-
return newPage;
|
|
1114
|
-
}
|
|
1115
|
-
var newItems = _toConsumableArray(getItemsFromResponse(response));
|
|
1116
|
-
updateState({
|
|
1117
|
-
loaded: true,
|
|
1118
|
-
loading: false,
|
|
1119
|
-
items: newItems,
|
|
1120
|
-
total: newItems.length
|
|
1121
|
-
});
|
|
1122
|
-
return newItems;
|
|
1123
|
-
};
|
|
1124
|
-
var getNextPage = function getNextPage() {
|
|
1125
|
-
var allPages = lastPage !== -1 ? Array.call.apply(Array, [null].concat(_toConsumableArray(Array(lastPage)))).map(function (it, index) {
|
|
1126
|
-
return index + 1;
|
|
1127
|
-
}) : [];
|
|
1128
|
-
var remainingPages = allPages.filter(function (pageNumber) {
|
|
1129
|
-
return pages.findIndex(function (it) {
|
|
1130
|
-
return it.page === pageNumber;
|
|
1131
|
-
}) === -1;
|
|
1132
|
-
});
|
|
1133
|
-
var firstItem = remainingPages.length > 0 ? remainingPages.shift() : null;
|
|
1134
|
-
return firstItem !== null ? firstItem : null;
|
|
1135
|
-
};
|
|
1136
|
-
var loadItems = function loadItems(requestPage) {
|
|
1137
|
-
updateState({
|
|
1138
|
-
loading: true
|
|
1139
|
-
});
|
|
1140
|
-
var canceled = false;
|
|
1141
|
-
var request = isPaginated ? getPage(requestPage, count) : getItems();
|
|
1142
|
-
var promise = request.then(function (response) {
|
|
1143
|
-
return !canceled ? updateFromResponse(response) : Promise.reject();
|
|
1144
|
-
})["catch"](function (error) {
|
|
1145
|
-
return !canceled ? updateFromResponse(null, error) : Promise.reject();
|
|
1146
|
-
}).then(function (response) {
|
|
1147
|
-
if (isPaginated && onPageLoaded !== null) {
|
|
1148
|
-
onPageLoaded(response);
|
|
1149
|
-
} else if (!isPaginated && onItemsLoaded !== null) {
|
|
1150
|
-
onItemsLoaded(response);
|
|
1151
|
-
}
|
|
1152
|
-
if (onLoaded !== null) {
|
|
1153
|
-
onLoaded(response);
|
|
1154
|
-
}
|
|
1155
|
-
return response;
|
|
1156
|
-
})["catch"](function (error) {
|
|
1157
|
-
if (!canceled && onError !== null) {
|
|
1158
|
-
onError(error);
|
|
1159
|
-
}
|
|
1160
|
-
});
|
|
1161
|
-
promise.cancel = function () {
|
|
1162
|
-
canceled = true;
|
|
1163
|
-
};
|
|
1164
|
-
return promise;
|
|
1165
|
-
};
|
|
1166
|
-
var loadPage = function loadPage(pageToLoad) {
|
|
1167
|
-
if (loading) {
|
|
1168
|
-
return Promise.reject();
|
|
1169
|
-
}
|
|
1170
|
-
if (pages.find(function (it) {
|
|
1171
|
-
return it.page === pageToLoad;
|
|
1172
|
-
}) !== -1) {
|
|
1173
|
-
return Promise.reject();
|
|
1174
|
-
}
|
|
1175
|
-
return loadItems(pageToLoad);
|
|
1176
|
-
};
|
|
1177
|
-
var loadNextPage = function loadNextPage() {
|
|
1178
|
-
if (loading) {
|
|
1179
|
-
return Promise.reject();
|
|
1180
|
-
}
|
|
1181
|
-
var nextPage = getNextPage();
|
|
1182
|
-
return nextPage !== null ? loadItems(nextPage) : Promise.resolve();
|
|
1183
|
-
};
|
|
1184
|
-
var reloadPage = function reloadPage() {
|
|
1185
|
-
if (loading) {
|
|
1186
|
-
return Promise.reject();
|
|
1187
|
-
}
|
|
1188
|
-
return loadItems(page);
|
|
1189
|
-
};
|
|
1190
|
-
var reload = function reload() {
|
|
1191
|
-
if (loading) {
|
|
1192
|
-
return Promise.reject();
|
|
1193
|
-
}
|
|
1194
|
-
return loadItems();
|
|
1195
|
-
};
|
|
1196
|
-
var reset = React.useCallback(function () {
|
|
1197
|
-
setState({
|
|
1198
|
-
loaded: false,
|
|
1199
|
-
loading: false,
|
|
1200
|
-
lastPage: -1,
|
|
1201
|
-
total: 0,
|
|
1202
|
-
pages: null,
|
|
1203
|
-
items: null
|
|
1204
|
-
});
|
|
1205
|
-
}, [page, setState]);
|
|
1206
|
-
var updateItem = function updateItem(newItem) {
|
|
1207
|
-
if (loading) {
|
|
1208
|
-
return;
|
|
1209
|
-
}
|
|
1210
|
-
if (isPaginated) {
|
|
1211
|
-
var currentPages = pages || [];
|
|
1212
|
-
updateState({
|
|
1213
|
-
pages: currentPages.map(function (pageData) {
|
|
1214
|
-
var pageItems = pageData.items || [];
|
|
1215
|
-
if (pageItems.findIndex(function (_ref5) {
|
|
1216
|
-
var id = _ref5.id;
|
|
1217
|
-
return id === newItem.id;
|
|
1218
|
-
}) !== -1) {
|
|
1219
|
-
return _objectSpread(_objectSpread({}, pageData), {}, {
|
|
1220
|
-
items: pageItems.map(function (it) {
|
|
1221
|
-
return it.id === newItem.id ? newItem : it;
|
|
1222
|
-
})
|
|
1223
|
-
});
|
|
1224
|
-
}
|
|
1225
|
-
return pageData;
|
|
1226
|
-
})
|
|
1227
|
-
});
|
|
1228
|
-
} else {
|
|
1229
|
-
var currentItems = items || [];
|
|
1230
|
-
updateState({
|
|
1231
|
-
items: currentItems.map(function (it) {
|
|
1232
|
-
return it.id === newItem.id ? newItem : it;
|
|
1233
|
-
})
|
|
1234
|
-
});
|
|
1235
|
-
}
|
|
1236
|
-
};
|
|
1237
|
-
React.useEffect(function () {
|
|
1238
|
-
var hadState = lastState.current !== null;
|
|
1239
|
-
lastState.current = initialState;
|
|
1240
|
-
if (hadState) {
|
|
1241
|
-
setState(initialState);
|
|
1242
|
-
}
|
|
1243
|
-
}, [initialState]);
|
|
1244
|
-
React.useEffect(function () {
|
|
1245
|
-
if (getPage === null && getItems === null || providedItems !== null) {
|
|
1246
|
-
return function () {};
|
|
1247
|
-
}
|
|
1248
|
-
var loadPromise = null;
|
|
1249
|
-
var pageToLoad = isPaginated && initialPages === null && page === null ? 1 : page;
|
|
1250
|
-
if (!isPaginated || pageToLoad !== null) {
|
|
1251
|
-
loadPromise = loadItems(pageToLoad);
|
|
1252
|
-
}
|
|
1253
|
-
return function () {
|
|
1254
|
-
if (loadPromise !== null) {
|
|
1255
|
-
loadPromise.cancel();
|
|
1256
|
-
}
|
|
1257
|
-
};
|
|
1258
|
-
}, [getPage, getItems, page]);
|
|
1259
|
-
var currentPage = isPaginated && pages !== null ? pages.find(function (_ref6) {
|
|
1260
|
-
var pageNumber = _ref6.page;
|
|
1261
|
-
return parseInt(pageNumber, 10) === parseInt(page, 10);
|
|
1262
|
-
}) || null : null;
|
|
1263
|
-
return {
|
|
1264
|
-
items: items,
|
|
1265
|
-
pages: pages,
|
|
1266
|
-
pageItems: currentPage !== null ? currentPage.items : null,
|
|
1267
|
-
total: total,
|
|
1268
|
-
lastPage: lastPage,
|
|
1269
|
-
loaded: loaded,
|
|
1270
|
-
allLoaded: !isPaginated && loaded || lastPage !== -1 && isPaginated && pages.length === lastPage,
|
|
1271
|
-
loading: loading,
|
|
1272
|
-
loadNextPage: loadNextPage,
|
|
1273
|
-
loadPage: loadPage,
|
|
1274
|
-
reloadPage: reloadPage,
|
|
1275
|
-
reload: reload,
|
|
1276
|
-
reset: reset,
|
|
1277
|
-
updateItem: updateItem
|
|
1278
|
-
};
|
|
1279
|
-
};
|
|
1280
|
-
var useItems$1 = useItems;
|
|
1281
|
-
|
|
1282
|
-
var _excluded$2 = ["page", "count"],
|
|
1283
|
-
_excluded2 = ["data", "refetch", "isLoading", "isFetching", "isRefetching", "isFetched"];
|
|
1284
|
-
|
|
1285
|
-
// The new, better version
|
|
1286
|
-
|
|
1287
|
-
var useItemsStore = function useItemsStore(store, _ref) {
|
|
1288
|
-
var _ref$url = _ref.url,
|
|
1289
|
-
url = _ref$url === void 0 ? null : _ref$url,
|
|
1290
|
-
_ref$getPage = _ref.getPage,
|
|
1291
|
-
getPage = _ref$getPage === void 0 ? null : _ref$getPage,
|
|
1292
|
-
_ref$getItems = _ref.getItems,
|
|
1293
|
-
getItems = _ref$getItems === void 0 ? null : _ref$getItems,
|
|
1294
|
-
_ref$getPageFromRespo = _ref.getPageFromResponse,
|
|
1295
|
-
getPageFromResponse = _ref$getPageFromRespo === void 0 ? null : _ref$getPageFromRespo,
|
|
1296
|
-
_ref$getItemsFromResp = _ref.getItemsFromResponse,
|
|
1297
|
-
getItemsFromResponse = _ref$getItemsFromResp === void 0 ? null : _ref$getItemsFromResp,
|
|
1298
|
-
_ref$page = _ref.page,
|
|
1299
|
-
initialPage = _ref$page === void 0 ? null : _ref$page,
|
|
1300
|
-
_ref$count = _ref.count,
|
|
1301
|
-
initialCount = _ref$count === void 0 ? null : _ref$count,
|
|
1302
|
-
_ref$query = _ref.query,
|
|
1303
|
-
query = _ref$query === void 0 ? null : _ref$query,
|
|
1304
|
-
_ref$onQueryChange = _ref.onQueryChange,
|
|
1305
|
-
onQueryChange = _ref$onQueryChange === void 0 ? null : _ref$onQueryChange,
|
|
1306
|
-
_ref$queryConfig = _ref.queryConfig,
|
|
1307
|
-
queryConfig = _ref$queryConfig === void 0 ? null : _ref$queryConfig,
|
|
1308
|
-
_ref$keepData = _ref.keepData,
|
|
1309
|
-
keepData = _ref$keepData === void 0 ? true : _ref$keepData;
|
|
1310
|
-
var _ref2 = query || {},
|
|
1311
|
-
_ref2$page = _ref2.page,
|
|
1312
|
-
page = _ref2$page === void 0 ? initialPage : _ref2$page,
|
|
1313
|
-
_ref2$count = _ref2.count,
|
|
1314
|
-
count = _ref2$count === void 0 ? initialCount : _ref2$count,
|
|
1315
|
-
queryWithoutPage = _objectWithoutProperties(_ref2, _excluded$2);
|
|
1316
|
-
var paginated = page !== null;
|
|
1317
|
-
var queryKey = React.useMemo(function () {
|
|
1318
|
-
return paginated ? [store, page || 1, count || 12, queryWithoutPage] : [store, query];
|
|
1319
|
-
}, [paginated, store, page, count, query, queryWithoutPage, getPage, getItems]);
|
|
1320
|
-
var _useQuery = reactQuery.useQuery(_objectSpread(_objectSpread({
|
|
1321
|
-
queryKey: queryKey,
|
|
1322
|
-
queryFn: function queryFn(_ref3) {
|
|
1323
|
-
var _ref3$queryKey = _ref3.queryKey,
|
|
1324
|
-
key = _ref3$queryKey === void 0 ? null : _ref3$queryKey;
|
|
1325
|
-
if (paginated && getPage !== null) {
|
|
1326
|
-
var _key = _slicedToArray(key, 4),
|
|
1327
|
-
_key$ = _key[1],
|
|
1328
|
-
p = _key$ === void 0 ? null : _key$,
|
|
1329
|
-
_key$2 = _key[2],
|
|
1330
|
-
c = _key$2 === void 0 ? null : _key$2,
|
|
1331
|
-
_key$3 = _key[3],
|
|
1332
|
-
_q = _key$3 === void 0 ? null : _key$3;
|
|
1333
|
-
return getPage(p, c, _q);
|
|
1334
|
-
}
|
|
1335
|
-
if (!paginated && getItems !== null) {
|
|
1336
|
-
var _key2 = _slicedToArray(key, 2),
|
|
1337
|
-
_key2$ = _key2[1],
|
|
1338
|
-
_q2 = _key2$ === void 0 ? null : _key2$;
|
|
1339
|
-
return getItems(_q2);
|
|
1340
|
-
}
|
|
1341
|
-
var _key3 = _slicedToArray(key, 2),
|
|
1342
|
-
_key3$ = _key3[1],
|
|
1343
|
-
q = _key3$ === void 0 ? null : _key3$;
|
|
1344
|
-
return fetch$1.getJSON("".concat(url).concat(!isEmpty(q) ? "?".concat(queryString.stringify(q, {
|
|
1345
|
-
arrayFormat: 'bracket'
|
|
1346
|
-
})) : ''));
|
|
1347
|
-
}
|
|
1348
|
-
}, keepData ? {
|
|
1349
|
-
placeholderData: reactQuery.keepPreviousData
|
|
1350
|
-
} : null), queryConfig)),
|
|
1351
|
-
_useQuery$data = _useQuery.data,
|
|
1352
|
-
data = _useQuery$data === void 0 ? null : _useQuery$data,
|
|
1353
|
-
reload = _useQuery.refetch,
|
|
1354
|
-
isLoading = _useQuery.isLoading,
|
|
1355
|
-
isFetching = _useQuery.isFetching,
|
|
1356
|
-
isRefetching = _useQuery.isRefetching,
|
|
1357
|
-
isFetched = _useQuery.isFetched,
|
|
1358
|
-
otherProps = _objectWithoutProperties(_useQuery, _excluded2);
|
|
1359
|
-
var _ref4 = data || {},
|
|
1360
|
-
_ref4$data = _ref4.data,
|
|
1361
|
-
items = _ref4$data === void 0 ? [] : _ref4$data,
|
|
1362
|
-
_ref4$pagination = _ref4.pagination,
|
|
1363
|
-
pagination = _ref4$pagination === void 0 ? null : _ref4$pagination,
|
|
1364
|
-
_ref4$meta = _ref4.meta,
|
|
1365
|
-
meta = _ref4$meta === void 0 ? null : _ref4$meta;
|
|
1366
|
-
var _ref5 = pagination || meta || {},
|
|
1367
|
-
_ref5$page = _ref5.page,
|
|
1368
|
-
currentPage = _ref5$page === void 0 ? null : _ref5$page,
|
|
1369
|
-
_ref5$last_page = _ref5.last_page,
|
|
1370
|
-
lastPage = _ref5$last_page === void 0 ? 0 : _ref5$last_page,
|
|
1371
|
-
_ref5$total = _ref5.total,
|
|
1372
|
-
total = _ref5$total === void 0 ? null : _ref5$total;
|
|
1373
|
-
var loadNextPage = React.useCallback(function () {
|
|
1374
|
-
if (onQueryChange !== null && paginated && page !== null && page < lastPage) {
|
|
1375
|
-
onQueryChange(_objectSpread(_objectSpread({}, query), {}, {
|
|
1376
|
-
page: page + 1
|
|
1377
|
-
}));
|
|
1378
|
-
}
|
|
1379
|
-
}, [paginated, pagination, page]);
|
|
1380
|
-
var loadPage = React.useCallback(function (newPage) {
|
|
1381
|
-
if (onQueryChange !== null && newPage !== null) {
|
|
1382
|
-
onQueryChange(_objectSpread(_objectSpread({}, query), {}, {
|
|
1383
|
-
page: newPage
|
|
1384
|
-
}));
|
|
1385
|
-
}
|
|
1386
|
-
}, [query, onQueryChange]);
|
|
1387
|
-
|
|
1388
|
-
// Keep a list of updated items
|
|
1389
|
-
var _useState = React.useState([]),
|
|
1390
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
1391
|
-
updatedItems = _useState2[0],
|
|
1392
|
-
setUpdatedItems = _useState2[1];
|
|
1393
|
-
var updateItem = React.useCallback(function (item) {
|
|
1394
|
-
var _ref6 = item || {},
|
|
1395
|
-
_ref6$id = _ref6.id,
|
|
1396
|
-
itemId = _ref6$id === void 0 ? null : _ref6$id;
|
|
1397
|
-
if (itemId !== null) {
|
|
1398
|
-
setUpdatedItems([].concat(_toConsumableArray((updatedItems || []).filter(function () {
|
|
1399
|
-
var _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
1400
|
-
_ref7$id = _ref7.id,
|
|
1401
|
-
id = _ref7$id === void 0 ? null : _ref7$id;
|
|
1402
|
-
return id !== itemId;
|
|
1403
|
-
})), [item]));
|
|
1404
|
-
}
|
|
1405
|
-
}, [updatedItems, setUpdatedItems]);
|
|
1406
|
-
var replaceUpdatedItems = React.useCallback(function (currentItems) {
|
|
1407
|
-
if (currentItems === null || updatedItems === null || updatedItems.length === 0) {
|
|
1408
|
-
return currentItems;
|
|
1409
|
-
}
|
|
1410
|
-
return (currentItems || []).map(function (item) {
|
|
1411
|
-
var _ref8 = item || {},
|
|
1412
|
-
_ref8$id = _ref8.id,
|
|
1413
|
-
itemId = _ref8$id === void 0 ? null : _ref8$id;
|
|
1414
|
-
var updated = (updatedItems || []).find(function () {
|
|
1415
|
-
var _ref9 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
1416
|
-
_ref9$id = _ref9.id,
|
|
1417
|
-
id = _ref9$id === void 0 ? null : _ref9$id;
|
|
1418
|
-
return id === itemId;
|
|
1419
|
-
}) || null;
|
|
1420
|
-
if (updated !== null) {
|
|
1421
|
-
return updated;
|
|
1422
|
-
}
|
|
1423
|
-
return item;
|
|
1424
|
-
}, []);
|
|
1425
|
-
}, [updatedItems]);
|
|
1426
|
-
var partialItems = React.useMemo(function () {
|
|
1427
|
-
if (data === null) {
|
|
1428
|
-
return data;
|
|
1429
|
-
}
|
|
1430
|
-
if (paginated) {
|
|
1431
|
-
if (getPageFromResponse !== null) {
|
|
1432
|
-
return getPageFromResponse(items);
|
|
1433
|
-
}
|
|
1434
|
-
return items;
|
|
1435
|
-
}
|
|
1436
|
-
if (getItemsFromResponse !== null) {
|
|
1437
|
-
return getItemsFromResponse(data);
|
|
1438
|
-
}
|
|
1439
|
-
return data;
|
|
1440
|
-
}, [items, data, paginated, getPageFromResponse, getItemsFromResponse, updatedItems]);
|
|
1441
|
-
var finalItems = replaceUpdatedItems(partialItems);
|
|
1442
|
-
|
|
1443
|
-
// Keep a list of pages, useEffect wont work here because delayed
|
|
1444
|
-
var pages = React.useRef(null);
|
|
1445
|
-
if (isFetched && page !== null && data !== null) {
|
|
1446
|
-
pages.current = _objectSpread(_objectSpread({}, pages.current), {}, _defineProperty({}, page, data));
|
|
1447
|
-
}
|
|
1448
|
-
var allItems = pages.current !== null ? replaceUpdatedItems(Object.keys(pages.current).flatMap(function (k) {
|
|
1449
|
-
var _pages$current$k;
|
|
1450
|
-
return (_pages$current$k = pages.current[k]) === null || _pages$current$k === void 0 ? void 0 : _pages$current$k.data;
|
|
1451
|
-
})) : finalItems;
|
|
1452
|
-
var finalLoading = isLoading || isFetching || isRefetching;
|
|
1453
|
-
return _objectSpread(_objectSpread({}, otherProps), {}, {
|
|
1454
|
-
isLoading: isLoading,
|
|
1455
|
-
isFetching: isFetching,
|
|
1456
|
-
isRefetching: isRefetching,
|
|
1457
|
-
isFetched: isFetched,
|
|
1458
|
-
items: finalItems,
|
|
1459
|
-
pages: pages.current,
|
|
1460
|
-
allItems: allItems,
|
|
1461
|
-
loading: finalLoading,
|
|
1462
|
-
loaded: isFetched,
|
|
1463
|
-
reload: reload,
|
|
1464
|
-
loadPage: loadPage,
|
|
1465
|
-
loadNextPage: loadNextPage,
|
|
1466
|
-
updateItem: updateItem,
|
|
1467
|
-
pagination: paginated ? {
|
|
1468
|
-
page: currentPage,
|
|
1469
|
-
lastPage: lastPage,
|
|
1470
|
-
total: total
|
|
1471
|
-
} : null
|
|
1472
|
-
});
|
|
1473
|
-
};
|
|
1474
|
-
var useItemsStore$1 = useItemsStore;
|
|
1475
|
-
|
|
1476
|
-
var AccountApi = /*#__PURE__*/function () {
|
|
1477
|
-
function AccountApi(api, opts) {
|
|
1478
|
-
_classCallCheck(this, AccountApi);
|
|
1479
|
-
this.api = api;
|
|
1480
|
-
this.options = _objectSpread({
|
|
1481
|
-
withCredentials: false
|
|
1482
|
-
}, opts);
|
|
1483
|
-
}
|
|
1484
|
-
return _createClass(AccountApi, [{
|
|
1485
|
-
key: "update",
|
|
1486
|
-
value: function update(data) {
|
|
1487
|
-
var withCredentials = this.options.withCredentials;
|
|
1488
|
-
return this.api.requestPost(this.api.route('account'), data, {
|
|
1489
|
-
withSession: !withCredentials,
|
|
1490
|
-
withCredentials: withCredentials
|
|
1491
|
-
});
|
|
1492
|
-
}
|
|
1493
|
-
}, {
|
|
1494
|
-
key: "updatePassword",
|
|
1495
|
-
value: function updatePassword(data) {
|
|
1496
|
-
var withCredentials = this.options.withCredentials;
|
|
1497
|
-
return this.api.requestPost(this.api.route('account.password'), data, {
|
|
1498
|
-
withSession: !withCredentials,
|
|
1499
|
-
withCredentials: withCredentials
|
|
1500
|
-
});
|
|
1501
|
-
}
|
|
1502
|
-
}, {
|
|
1503
|
-
key: "updateTaxes",
|
|
1504
|
-
value: function updateTaxes(data) {
|
|
1505
|
-
var withCredentials = this.options.withCredentials;
|
|
1506
|
-
return this.api.requestPost(this.api.route('account.taxes'), data, {
|
|
1507
|
-
withSession: !withCredentials,
|
|
1508
|
-
withCredentials: withCredentials
|
|
1509
|
-
});
|
|
1510
|
-
}
|
|
1511
|
-
}, {
|
|
1512
|
-
key: "delete",
|
|
1513
|
-
value: function _delete(data) {
|
|
1514
|
-
var withCredentials = this.options.withCredentials;
|
|
1515
|
-
return this.api.requestPost(this.api.route('account.delete'), data, {
|
|
1516
|
-
withSession: !withCredentials,
|
|
1517
|
-
withCredentials: withCredentials
|
|
1518
|
-
});
|
|
1519
|
-
}
|
|
1520
|
-
}]);
|
|
1521
|
-
}();
|
|
1522
|
-
|
|
1523
|
-
var AuthApi = /*#__PURE__*/function () {
|
|
1524
|
-
function AuthApi(api, opts) {
|
|
1525
|
-
_classCallCheck(this, AuthApi);
|
|
1526
|
-
this.api = api;
|
|
1527
|
-
this.options = _objectSpread({
|
|
1528
|
-
withCredentials: true
|
|
1529
|
-
}, opts);
|
|
1530
|
-
}
|
|
1531
|
-
return _createClass(AuthApi, [{
|
|
1532
|
-
key: "check",
|
|
1533
|
-
value: function check() {
|
|
1534
|
-
var withCredentials = this.options.withCredentials;
|
|
1535
|
-
return this.api.requestGet(this.api.route('auth.check'), null, {
|
|
1536
|
-
withSession: !withCredentials,
|
|
1537
|
-
withCredentials: withCredentials
|
|
1538
|
-
});
|
|
1539
|
-
}
|
|
1540
|
-
}, {
|
|
1541
|
-
key: "login",
|
|
1542
|
-
value: function login(email, password) {
|
|
1543
|
-
var withCredentials = this.options.withCredentials;
|
|
1544
|
-
return this.api.requestPost(this.api.route('auth.login'), {
|
|
1545
|
-
email: email,
|
|
1546
|
-
password: password
|
|
1547
|
-
}, {
|
|
1548
|
-
withSession: !withCredentials,
|
|
1549
|
-
withCredentials: withCredentials
|
|
1550
|
-
});
|
|
1551
|
-
}
|
|
1552
|
-
}, {
|
|
1553
|
-
key: "register",
|
|
1554
|
-
value: function register(data) {
|
|
1555
|
-
var withCredentials = this.options.withCredentials;
|
|
1556
|
-
return this.api.requestPost(this.api.route('auth.register'), data, {
|
|
1557
|
-
withSession: !withCredentials,
|
|
1558
|
-
withCredentials: withCredentials
|
|
1559
|
-
});
|
|
1560
|
-
}
|
|
1561
|
-
}, {
|
|
1562
|
-
key: "logout",
|
|
1563
|
-
value: function logout() {
|
|
1564
|
-
var url = this.api.getFullUrl(this.api.route('auth.logout'));
|
|
1565
|
-
return fetch(url, {
|
|
1566
|
-
method: 'POST',
|
|
1567
|
-
headers: _objectSpread({}, fetch$1.getCSRFHeaders()),
|
|
1568
|
-
credentials: 'include'
|
|
1569
|
-
})["catch"](function (err) {
|
|
1570
|
-
console.log('logout err', err); // eslint-disable-line
|
|
1571
|
-
throw err;
|
|
1572
|
-
});
|
|
1573
|
-
}
|
|
1574
|
-
}, {
|
|
1575
|
-
key: "requestPassword",
|
|
1576
|
-
value: function requestPassword(email) {
|
|
1577
|
-
var withCredentials = this.options.withCredentials;
|
|
1578
|
-
return this.api.requestPost(this.api.route('auth.password.email'), {
|
|
1579
|
-
email: email
|
|
1580
|
-
}, {
|
|
1581
|
-
withSession: !withCredentials,
|
|
1582
|
-
withCredentials: withCredentials
|
|
1583
|
-
});
|
|
1584
|
-
}
|
|
1585
|
-
}, {
|
|
1586
|
-
key: "resetPassword",
|
|
1587
|
-
value: function resetPassword(data) {
|
|
1588
|
-
var withCredentials = this.options.withCredentials;
|
|
1589
|
-
return this.api.requestPost(this.api.route('auth.password.update'), data, {
|
|
1590
|
-
withSession: !withCredentials,
|
|
1591
|
-
withCredentials: withCredentials
|
|
1592
|
-
});
|
|
1593
|
-
}
|
|
1594
|
-
}]);
|
|
1595
|
-
}();
|
|
1596
|
-
|
|
1597
|
-
({
|
|
1598
|
-
children: PropTypes.node.isRequired,
|
|
1599
|
-
routes: PropTypes.objectOf(PropTypes.string).isRequired,
|
|
1600
|
-
basePath: PropTypes.string
|
|
1601
|
-
});
|
|
1602
|
-
var compilers = {};
|
|
1603
|
-
function generatePath(fullPath, data) {
|
|
1604
|
-
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1605
|
-
var fullUrlMatches = fullPath.match(/^(https?:\/\/)/);
|
|
1606
|
-
if (typeof compilers[fullPath] === 'undefined') {
|
|
1607
|
-
compilers[fullPath] = pathToRegexp.compile(fullPath.replace(/^(https?:\/\/)/, ''), opts);
|
|
1608
|
-
}
|
|
1609
|
-
var compiler = compilers[fullPath];
|
|
1610
|
-
return fullUrlMatches !== null ? "".concat(fullUrlMatches[1]).concat(compiler(data)) : compiler(data);
|
|
1611
|
-
}
|
|
1612
|
-
|
|
1613
|
-
var Base = /*#__PURE__*/function () {
|
|
1614
|
-
function Base() {
|
|
1615
|
-
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1616
|
-
_classCallCheck(this, Base);
|
|
1617
|
-
this.options = _objectSpread(_objectSpread({
|
|
1618
|
-
routes: {},
|
|
1619
|
-
generateUrl: null
|
|
1620
|
-
}, opts), {}, {
|
|
1621
|
-
baseUrl: opts.baseUrl,
|
|
1622
|
-
onUnauthorized: opts.onUnauthorized || null
|
|
1623
|
-
});
|
|
1624
|
-
}
|
|
1625
|
-
return _createClass(Base, [{
|
|
1626
|
-
key: "requestGet",
|
|
1627
|
-
value: function requestGet(path) {
|
|
1628
|
-
var _this = this;
|
|
1629
|
-
var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
1630
|
-
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
1631
|
-
var finalQuery = query !== null ? queryString.stringify(query, {
|
|
1632
|
-
arrayFormat: 'bracket'
|
|
1633
|
-
}) : null;
|
|
1634
|
-
return fetch$1.getJSON("".concat(this.getFullUrl(path)).concat(finalQuery !== null && finalQuery.length > 0 ? "?".concat(finalQuery) : ''), _objectSpread({
|
|
1635
|
-
credentials: 'include',
|
|
1636
|
-
headers: fetch$1.getCSRFHeaders()
|
|
1637
|
-
}, opts))["catch"](function (error) {
|
|
1638
|
-
return _this.onError(error);
|
|
1639
|
-
});
|
|
1640
|
-
}
|
|
1641
|
-
}, {
|
|
1642
|
-
key: "requestPost",
|
|
1643
|
-
value: function requestPost(path, data) {
|
|
1644
|
-
var _this2 = this;
|
|
1645
|
-
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
1646
|
-
return fetch$1.postJSON(this.getFullUrl(path), data, _objectSpread({
|
|
1647
|
-
credentials: 'include',
|
|
1648
|
-
headers: fetch$1.getCSRFHeaders()
|
|
1649
|
-
}, opts))["catch"](function (error) {
|
|
1650
|
-
return _this2.onError(error);
|
|
1651
|
-
});
|
|
1652
|
-
}
|
|
1653
|
-
}, {
|
|
1654
|
-
key: "requestPut",
|
|
1655
|
-
value: function requestPut(path, data) {
|
|
1656
|
-
var _this3 = this;
|
|
1657
|
-
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
1658
|
-
return fetch$1.postJSON(this.getFullUrl(path), _objectSpread({
|
|
1659
|
-
_method: 'PUT'
|
|
1660
|
-
}, data), _objectSpread({
|
|
1661
|
-
credentials: 'include',
|
|
1662
|
-
headers: fetch$1.getCSRFHeaders()
|
|
1663
|
-
}, opts))["catch"](function (error) {
|
|
1664
|
-
return _this3.onError(error);
|
|
1665
|
-
});
|
|
1666
|
-
}
|
|
1667
|
-
}, {
|
|
1668
|
-
key: "requestPatch",
|
|
1669
|
-
value: function requestPatch(path, data) {
|
|
1670
|
-
var _this4 = this;
|
|
1671
|
-
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
1672
|
-
return fetch$1.postJSON(this.getFullUrl(path), _objectSpread({
|
|
1673
|
-
_method: 'PATCH'
|
|
1674
|
-
}, data), _objectSpread({
|
|
1675
|
-
credentials: 'include',
|
|
1676
|
-
headers: fetch$1.getCSRFHeaders()
|
|
1677
|
-
}, opts))["catch"](function (error) {
|
|
1678
|
-
return _this4.onError(error);
|
|
1679
|
-
});
|
|
1680
|
-
}
|
|
1681
|
-
}, {
|
|
1682
|
-
key: "requestDelete",
|
|
1683
|
-
value: function requestDelete(path) {
|
|
1684
|
-
var _this5 = this;
|
|
1685
|
-
return this.requestPost(this.getFullUrl(path), {
|
|
1686
|
-
_method: 'DELETE'
|
|
1687
|
-
})["catch"](function (error) {
|
|
1688
|
-
return _this5.onError(error);
|
|
1689
|
-
});
|
|
1690
|
-
}
|
|
1691
|
-
}, {
|
|
1692
|
-
key: "route",
|
|
1693
|
-
value: function route(_route, params) {
|
|
1694
|
-
var _this$options = this.options,
|
|
1695
|
-
routes = _this$options.routes,
|
|
1696
|
-
_this$options$generat = _this$options.generateUrl,
|
|
1697
|
-
generateUrl = _this$options$generat === void 0 ? null : _this$options$generat;
|
|
1698
|
-
if (generateUrl !== null) {
|
|
1699
|
-
return generateUrl(_route, params);
|
|
1700
|
-
}
|
|
1701
|
-
return generatePath(routes[_route] || _route, params);
|
|
1702
|
-
}
|
|
1703
|
-
}, {
|
|
1704
|
-
key: "getFullUrl",
|
|
1705
|
-
value: function getFullUrl(path) {
|
|
1706
|
-
var _this$options$baseUrl = this.options.baseUrl,
|
|
1707
|
-
baseUrl = _this$options$baseUrl === void 0 ? null : _this$options$baseUrl;
|
|
1708
|
-
var hasProtocol = path.indexOf('://') !== -1;
|
|
1709
|
-
return baseUrl !== null && !hasProtocol ? "".concat(baseUrl.replace(/\/$/, ''), "/").concat(path.replace(/^\//, '')) : path;
|
|
1710
|
-
}
|
|
1711
|
-
}, {
|
|
1712
|
-
key: "onError",
|
|
1713
|
-
value: function onError(err) {
|
|
1714
|
-
var onUnauthorized = this.options.onUnauthorized;
|
|
1715
|
-
var _ref = err || {},
|
|
1716
|
-
_ref$status = _ref.status,
|
|
1717
|
-
status = _ref$status === void 0 ? null : _ref$status;
|
|
1718
|
-
var statusCode = parseInt(status, 10);
|
|
1719
|
-
// If status is refused and callback exists
|
|
1720
|
-
if ((statusCode === 401 || statusCode === 419) && onUnauthorized !== null) {
|
|
1721
|
-
return onUnauthorized();
|
|
1722
|
-
}
|
|
1723
|
-
throw err;
|
|
1724
|
-
}
|
|
1725
|
-
}]);
|
|
1726
|
-
}();
|
|
1727
|
-
var Base$1 = Base;
|
|
1728
|
-
|
|
1729
|
-
var MediasApi = /*#__PURE__*/function (_Base) {
|
|
1730
|
-
function MediasApi() {
|
|
1731
|
-
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1732
|
-
_classCallCheck(this, MediasApi);
|
|
1733
|
-
return _callSuper(this, MediasApi, [{
|
|
1734
|
-
baseUrl: opts.baseUrl ? opts.baseUrl : null,
|
|
1735
|
-
routes: _objectSpread({
|
|
1736
|
-
index: 'medias',
|
|
1737
|
-
trashed: 'medias/trash',
|
|
1738
|
-
tags: 'medias/tags',
|
|
1739
|
-
show: 'medias/:media',
|
|
1740
|
-
store: 'medias',
|
|
1741
|
-
update: 'medias/:media',
|
|
1742
|
-
trash: 'medias/trash/:media',
|
|
1743
|
-
"delete": 'medias/:media'
|
|
1744
|
-
}, opts.routes || null)
|
|
1745
|
-
}]);
|
|
1746
|
-
}
|
|
1747
|
-
_inherits(MediasApi, _Base);
|
|
1748
|
-
return _createClass(MediasApi, [{
|
|
1749
|
-
key: "find",
|
|
1750
|
-
value: function find(id) {
|
|
1751
|
-
return this.requestGet(this.route('show', {
|
|
1752
|
-
media: id
|
|
1753
|
-
}));
|
|
1754
|
-
}
|
|
1755
|
-
}, {
|
|
1756
|
-
key: "get",
|
|
1757
|
-
value: function get() {
|
|
1758
|
-
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1759
|
-
var page = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
|
1760
|
-
var count = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10;
|
|
1761
|
-
var finalQuery = _objectSpread({}, query);
|
|
1762
|
-
if (page !== null) {
|
|
1763
|
-
finalQuery.page = page;
|
|
1764
|
-
}
|
|
1765
|
-
if (count !== null) {
|
|
1766
|
-
finalQuery.count = count;
|
|
1767
|
-
}
|
|
1768
|
-
return this.requestGet(this.route('index'), finalQuery);
|
|
1769
|
-
}
|
|
1770
|
-
}, {
|
|
1771
|
-
key: "getTrashed",
|
|
1772
|
-
value: function getTrashed() {
|
|
1773
|
-
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1774
|
-
var page = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
|
1775
|
-
var count = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10;
|
|
1776
|
-
var finalQuery = _objectSpread({}, query);
|
|
1777
|
-
if (page !== null) {
|
|
1778
|
-
finalQuery.page = page;
|
|
1779
|
-
}
|
|
1780
|
-
if (count !== null) {
|
|
1781
|
-
finalQuery.count = count;
|
|
1782
|
-
}
|
|
1783
|
-
return this.requestGet(this.route('trashed'), finalQuery);
|
|
1784
|
-
}
|
|
1785
|
-
}, {
|
|
1786
|
-
key: "getTags",
|
|
1787
|
-
value: function getTags() {
|
|
1788
|
-
var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1789
|
-
var count = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
|
|
1790
|
-
var finalQuery = _objectSpread({}, query);
|
|
1791
|
-
if (count !== null) {
|
|
1792
|
-
finalQuery.count = count;
|
|
1793
|
-
}
|
|
1794
|
-
return this.requestGet(this.route('tags'), finalQuery);
|
|
1795
|
-
}
|
|
1796
|
-
}, {
|
|
1797
|
-
key: "create",
|
|
1798
|
-
value: function create(data) {
|
|
1799
|
-
return this.requestPost(this.route('store'), data);
|
|
1800
|
-
}
|
|
1801
|
-
}, {
|
|
1802
|
-
key: "update",
|
|
1803
|
-
value: function update(id, data) {
|
|
1804
|
-
return this.requestPut(this.route('update', {
|
|
1805
|
-
media: id
|
|
1806
|
-
}), data);
|
|
1807
|
-
}
|
|
1808
|
-
}, {
|
|
1809
|
-
key: "trash",
|
|
1810
|
-
value: function trash(id) {
|
|
1811
|
-
return this.requestDelete(this.route('trash', {
|
|
1812
|
-
media: id
|
|
1813
|
-
}));
|
|
1814
|
-
}
|
|
1815
|
-
}, {
|
|
1816
|
-
key: "delete",
|
|
1817
|
-
value: function _delete(id) {
|
|
1818
|
-
return this.requestDelete(this.route('delete', {
|
|
1819
|
-
media: id
|
|
1820
|
-
}));
|
|
1821
|
-
}
|
|
1822
|
-
}]);
|
|
1823
|
-
}(Base$1);
|
|
1824
|
-
|
|
1825
|
-
var ResourcesApi = /*#__PURE__*/function () {
|
|
1826
|
-
function ResourcesApi(api) {
|
|
1827
|
-
_classCallCheck(this, ResourcesApi);
|
|
1828
|
-
this.api = api;
|
|
1829
|
-
}
|
|
1830
|
-
return _createClass(ResourcesApi, [{
|
|
1831
|
-
key: "resourceRoute",
|
|
1832
|
-
value: function resourceRoute(_ref, route, params) {
|
|
1833
|
-
var id = _ref.id,
|
|
1834
|
-
_ref$has_routes = _ref.has_routes,
|
|
1835
|
-
hasRoutes = _ref$has_routes === void 0 ? false : _ref$has_routes;
|
|
1836
|
-
var routePrefix = hasRoutes ? "resources.".concat(id) : 'resources';
|
|
1837
|
-
return this.api.route("".concat(routePrefix, ".").concat(route), hasRoutes ? params : _objectSpread({
|
|
1838
|
-
resource: id
|
|
1839
|
-
}, params));
|
|
1840
|
-
}
|
|
1841
|
-
}, {
|
|
1842
|
-
key: "find",
|
|
1843
|
-
value: function find(resource, id) {
|
|
1844
|
-
return this.api.requestGet(this.resourceRoute(resource, 'show', {
|
|
1845
|
-
id: id
|
|
1846
|
-
}), null, {
|
|
1847
|
-
withCredentials: true
|
|
1848
|
-
});
|
|
1849
|
-
}
|
|
1850
|
-
}, {
|
|
1851
|
-
key: "get",
|
|
1852
|
-
value: function get(resource) {
|
|
1853
|
-
var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1854
|
-
var page = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
|
|
1855
|
-
var count = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 12;
|
|
1856
|
-
var finalQuery = _objectSpread({}, query);
|
|
1857
|
-
if (page !== null) {
|
|
1858
|
-
finalQuery.page = page;
|
|
1859
|
-
}
|
|
1860
|
-
if (count !== null) {
|
|
1861
|
-
finalQuery.count = count;
|
|
1862
|
-
}
|
|
1863
|
-
return this.api.requestGet(this.resourceRoute(resource, 'index'), finalQuery, {
|
|
1864
|
-
withCredentials: true
|
|
1865
|
-
});
|
|
1866
|
-
}
|
|
1867
|
-
}, {
|
|
1868
|
-
key: "store",
|
|
1869
|
-
value: function store(resource, data) {
|
|
1870
|
-
return this.api.requestPost(this.resourceRoute(resource, 'store'), data, {
|
|
1871
|
-
withCredentials: true
|
|
1872
|
-
});
|
|
1873
|
-
}
|
|
1874
|
-
}, {
|
|
1875
|
-
key: "update",
|
|
1876
|
-
value: function update(resource, id, data) {
|
|
1877
|
-
return this.api.requestPatch(this.resourceRoute(resource, 'update', {
|
|
1878
|
-
id: id
|
|
1879
|
-
}), data, {
|
|
1880
|
-
withCredentials: true
|
|
1881
|
-
});
|
|
1882
|
-
}
|
|
1883
|
-
}, {
|
|
1884
|
-
key: "destroy",
|
|
1885
|
-
value: function destroy(resource, id) {
|
|
1886
|
-
return this.api.requestDelete(this.resourceRoute(resource, 'destroy', {
|
|
1887
|
-
id: id
|
|
1888
|
-
}), null, {
|
|
1889
|
-
withCredentials: true
|
|
1890
|
-
});
|
|
1891
|
-
}
|
|
1892
|
-
}]);
|
|
1893
|
-
}();
|
|
1894
|
-
|
|
1895
|
-
var Api = /*#__PURE__*/function (_Base) {
|
|
1896
|
-
function Api() {
|
|
1897
|
-
var _this;
|
|
1898
|
-
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1899
|
-
_classCallCheck(this, Api);
|
|
1900
|
-
_this = _callSuper(this, Api, [opts]);
|
|
1901
|
-
_this.auth = new AuthApi(_this, {
|
|
1902
|
-
withCredentials: true
|
|
1903
|
-
});
|
|
1904
|
-
_this.account = new AccountApi(_this, {
|
|
1905
|
-
withCredentials: true
|
|
1906
|
-
});
|
|
1907
|
-
_this.medias = new MediasApi(opts);
|
|
1908
|
-
_this.resources = new ResourcesApi(_this);
|
|
1909
|
-
return _this;
|
|
1910
|
-
}
|
|
1911
|
-
_inherits(Api, _Base);
|
|
1912
|
-
return _createClass(Api);
|
|
1913
|
-
}(Base$1);
|
|
1914
|
-
var Api$1 = Api;
|
|
1915
|
-
|
|
1916
|
-
/* eslint-disable react/jsx-props-no-spreading */
|
|
1917
|
-
var ApiContext = /*#__PURE__*/React.createContext(null);
|
|
1918
|
-
var useApi = function useApi() {
|
|
1919
|
-
return React.useContext(ApiContext);
|
|
1920
|
-
};
|
|
1921
|
-
var propTypes$2 = {
|
|
1922
|
-
api: PropTypes.instanceOf(Api$1),
|
|
1923
|
-
baseUrl: PropTypes.string,
|
|
1924
|
-
onUnauthorized: PropTypes.func,
|
|
1925
|
-
children: PropTypes.node.isRequired
|
|
1926
|
-
};
|
|
1927
|
-
var defaultProps$2 = {
|
|
1928
|
-
api: null,
|
|
1929
|
-
onUnauthorized: null,
|
|
1930
|
-
baseUrl: undefined
|
|
1931
|
-
};
|
|
1932
|
-
var ApiProvider = function ApiProvider(_ref) {
|
|
1933
|
-
var initialApi = _ref.api,
|
|
1934
|
-
baseUrl = _ref.baseUrl,
|
|
1935
|
-
onUnauthorized = _ref.onUnauthorized,
|
|
1936
|
-
children = _ref.children;
|
|
1937
|
-
var generateUrl = contexts.useUrlGenerator();
|
|
1938
|
-
var previousApi = useApi();
|
|
1939
|
-
var api = React.useMemo(function () {
|
|
1940
|
-
return initialApi || previousApi || new Api$1({
|
|
1941
|
-
baseUrl: baseUrl,
|
|
1942
|
-
generateUrl: generateUrl,
|
|
1943
|
-
onUnauthorized: onUnauthorized
|
|
1944
|
-
});
|
|
1945
|
-
}, [previousApi, initialApi, baseUrl]);
|
|
1946
|
-
return /*#__PURE__*/React.createElement(ApiContext.Provider, {
|
|
1947
|
-
value: api
|
|
1948
|
-
}, children);
|
|
1949
|
-
};
|
|
1950
|
-
ApiProvider.propTypes = propTypes$2;
|
|
1951
|
-
ApiProvider.defaultProps = defaultProps$2;
|
|
1952
|
-
|
|
1953
|
-
var useResourceDestroy = function useResourceDestroy(resource) {
|
|
1954
|
-
var id = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
1955
|
-
var _useState = React.useState(false),
|
|
1956
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
1957
|
-
loading = _useState2[0],
|
|
1958
|
-
setLoading = _useState2[1];
|
|
1959
|
-
var api = useApi();
|
|
1960
|
-
var destroy = React.useCallback(function (providedId) {
|
|
1961
|
-
setLoading(true);
|
|
1962
|
-
return api.resources.destroy(resource, providedId || id).then(function (response) {
|
|
1963
|
-
setLoading(false);
|
|
1964
|
-
return response;
|
|
1965
|
-
})["catch"](function (e) {
|
|
1966
|
-
setLoading(false);
|
|
1967
|
-
throw e;
|
|
1968
|
-
});
|
|
1969
|
-
}, [api, resource, id, setLoading]);
|
|
1970
|
-
return {
|
|
1971
|
-
destroy: destroy,
|
|
1972
|
-
loading: loading
|
|
1973
|
-
};
|
|
1974
|
-
};
|
|
1975
|
-
var useResourceDestroy$1 = useResourceDestroy;
|
|
1976
|
-
|
|
1977
|
-
var _excluded$1 = ["data"];
|
|
1978
|
-
var useResourceItem = function useResourceItem(resource, id, opts) {
|
|
1979
|
-
var api = useApi();
|
|
1980
|
-
var loader = React.useCallback(function () {
|
|
1981
|
-
return api.resources.find(resource, id);
|
|
1982
|
-
}, [api, resource, id]);
|
|
1983
|
-
var _useData = useData$1(loader, opts),
|
|
1984
|
-
data = _useData.data,
|
|
1985
|
-
request = _objectWithoutProperties(_useData, _excluded$1);
|
|
1986
|
-
return _objectSpread({
|
|
1987
|
-
item: data
|
|
1988
|
-
}, request);
|
|
1989
|
-
};
|
|
1990
|
-
var useResourceItem$1 = useResourceItem;
|
|
1991
|
-
|
|
1992
|
-
var _excluded = ["items"];
|
|
1993
|
-
var useResourceItems = function useResourceItems(resource) {
|
|
1994
|
-
var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
1995
|
-
var page = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
1996
|
-
var count = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
|
|
1997
|
-
var opts = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null;
|
|
1998
|
-
var api = useApi();
|
|
1999
|
-
var _ref = resource || {},
|
|
2000
|
-
_ref$id = _ref.id,
|
|
2001
|
-
id = _ref$id === void 0 ? null : _ref$id;
|
|
2002
|
-
var lastResourceRef = React.useRef(id);
|
|
2003
|
-
var resourceChanging = lastResourceRef.current !== id;
|
|
2004
|
-
var getPage = React.useCallback(function () {
|
|
2005
|
-
var requestedPage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
2006
|
-
return api.resources.get(resource, query, requestedPage, count).then(function (response) {
|
|
2007
|
-
var _ref2 = resource || {},
|
|
2008
|
-
resourceId = _ref2.id;
|
|
2009
|
-
lastResourceRef.current = resourceId;
|
|
2010
|
-
return response;
|
|
2011
|
-
})["catch"](function (err) {
|
|
2012
|
-
var _ref3 = resource || {},
|
|
2013
|
-
resourceId = _ref3.id;
|
|
2014
|
-
lastResourceRef.current = resourceId;
|
|
2015
|
-
throw err;
|
|
2016
|
-
});
|
|
2017
|
-
}, [api, resource, query, count]);
|
|
2018
|
-
var getItems = React.useCallback(function () {
|
|
2019
|
-
return api.resources.get(resource, query, null, null).then(function (response) {
|
|
2020
|
-
var _ref4 = resource || {},
|
|
2021
|
-
resourceId = _ref4.id;
|
|
2022
|
-
lastResourceRef.current = resourceId;
|
|
2023
|
-
return response;
|
|
2024
|
-
})["catch"](function (err) {
|
|
2025
|
-
var _ref5 = resource || {},
|
|
2026
|
-
resourceId = _ref5.id;
|
|
2027
|
-
lastResourceRef.current = resourceId;
|
|
2028
|
-
throw err;
|
|
2029
|
-
});
|
|
2030
|
-
}, [api, resource, query, count]);
|
|
2031
|
-
var _useItemsStore = useItemsStore$1(id, _objectSpread({
|
|
2032
|
-
getPage: page !== null ? getPage : null,
|
|
2033
|
-
getItems: page === null ? getItems : null,
|
|
2034
|
-
query: query,
|
|
2035
|
-
page: page,
|
|
2036
|
-
count: count
|
|
2037
|
-
}, opts)),
|
|
2038
|
-
items = _useItemsStore.items,
|
|
2039
|
-
request = _objectWithoutProperties(_useItemsStore, _excluded);
|
|
2040
|
-
return _objectSpread({
|
|
2041
|
-
items: resourceChanging ? null : items
|
|
2042
|
-
}, request);
|
|
2043
|
-
};
|
|
2044
|
-
var useResourceItems$1 = useResourceItems;
|
|
2045
|
-
|
|
2046
|
-
var useResourceCreate = function useResourceCreate(resource) {
|
|
2047
|
-
var _useState = React.useState(false),
|
|
2048
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
2049
|
-
loading = _useState2[0],
|
|
2050
|
-
setLoading = _useState2[1];
|
|
2051
|
-
var api = useApi();
|
|
2052
|
-
var store = React.useCallback(function (data) {
|
|
2053
|
-
setLoading(true);
|
|
2054
|
-
return api.resources.store(resource, data).then(function (response) {
|
|
2055
|
-
setLoading(false);
|
|
2056
|
-
return response;
|
|
2057
|
-
})["catch"](function (e) {
|
|
2058
|
-
setLoading(false);
|
|
2059
|
-
throw e;
|
|
2060
|
-
});
|
|
2061
|
-
}, [api, resource, setLoading]);
|
|
2062
|
-
return {
|
|
2063
|
-
store: store,
|
|
2064
|
-
loading: loading
|
|
2065
|
-
};
|
|
2066
|
-
};
|
|
2067
|
-
var useResourceCreate$1 = useResourceCreate;
|
|
2068
|
-
|
|
2069
|
-
var useResourceUpdate = function useResourceUpdate(resource, id) {
|
|
2070
|
-
var _useState = React.useState(false),
|
|
2071
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
2072
|
-
loading = _useState2[0],
|
|
2073
|
-
setLoading = _useState2[1];
|
|
2074
|
-
var api = useApi();
|
|
2075
|
-
var update = React.useCallback(function (data) {
|
|
2076
|
-
setLoading(true);
|
|
2077
|
-
return api.resources.update(resource, id, data).then(function (response) {
|
|
2078
|
-
setLoading(false);
|
|
2079
|
-
return response;
|
|
2080
|
-
})["catch"](function (e) {
|
|
2081
|
-
setLoading(false);
|
|
2082
|
-
throw e;
|
|
2083
|
-
});
|
|
2084
|
-
}, [api, resource, id, setLoading]);
|
|
2085
|
-
return {
|
|
2086
|
-
update: update,
|
|
2087
|
-
loading: loading
|
|
2088
|
-
};
|
|
2089
|
-
};
|
|
2090
|
-
var useResourceUpdate$1 = useResourceUpdate;
|
|
2091
|
-
|
|
2092
|
-
var QueryContext = /*#__PURE__*/React.createContext(null);
|
|
2093
|
-
var useQueryContext = function useQueryContext() {
|
|
2094
|
-
return React.useContext(QueryContext);
|
|
2095
|
-
};
|
|
2096
|
-
var propTypes$1 = {
|
|
2097
|
-
config: PropTypes.shape({}),
|
|
2098
|
-
initialKey: PropTypes.arrayOf(PropTypes.string),
|
|
2099
|
-
initialData: PropTypes.oneOfType([PropTypes.shape({}), PropTypes.arrayOf(PropTypes.shape({}))]),
|
|
2100
|
-
children: PropTypes.node.isRequired
|
|
2101
|
-
};
|
|
2102
|
-
var defaultProps$1 = {
|
|
2103
|
-
config: null,
|
|
2104
|
-
initialKey: null,
|
|
2105
|
-
initialData: null
|
|
2106
|
-
};
|
|
2107
|
-
var QueryProvider = function QueryProvider(_ref) {
|
|
2108
|
-
var initialConfig = _ref.config,
|
|
2109
|
-
initialKey = _ref.initialKey,
|
|
2110
|
-
initialData = _ref.initialData,
|
|
2111
|
-
children = _ref.children;
|
|
2112
|
-
var queryClient = React.useMemo(function () {
|
|
2113
|
-
var client = new reactQuery.QueryClient(_objectSpread({
|
|
2114
|
-
defaultOptions: {
|
|
2115
|
-
queries: {
|
|
2116
|
-
staleTime: Infinity
|
|
2117
|
-
}
|
|
2118
|
-
}
|
|
2119
|
-
}, initialConfig));
|
|
2120
|
-
if (initialKey !== null && initialData !== null) {
|
|
2121
|
-
client.setQueryData(initialKey, initialData);
|
|
2122
|
-
}
|
|
2123
|
-
return client;
|
|
2124
|
-
}, [initialConfig]);
|
|
2125
|
-
return /*#__PURE__*/React.createElement(reactQuery.QueryClientProvider, {
|
|
2126
|
-
client: queryClient
|
|
2127
|
-
}, children);
|
|
2128
|
-
};
|
|
2129
|
-
QueryProvider.propTypes = propTypes$1;
|
|
2130
|
-
QueryProvider.defaultProps = defaultProps$1;
|
|
2131
|
-
|
|
2132
|
-
/* eslint-disable react/jsx-props-no-spreading */
|
|
2133
|
-
var propTypes = {
|
|
2134
|
-
api: PropTypes.instanceOf(Api$1),
|
|
2135
|
-
apiBaseUrl: PropTypes.string,
|
|
2136
|
-
children: PropTypes.node
|
|
2137
|
-
};
|
|
2138
|
-
var defaultProps = {
|
|
2139
|
-
api: null,
|
|
2140
|
-
apiBaseUrl: undefined,
|
|
2141
|
-
children: null
|
|
2142
|
-
};
|
|
2143
|
-
var DataProvider = function DataProvider(_ref) {
|
|
2144
|
-
var api = _ref.api,
|
|
2145
|
-
apiBaseUrl = _ref.apiBaseUrl,
|
|
2146
|
-
children = _ref.children;
|
|
2147
|
-
return /*#__PURE__*/React.createElement(ApiProvider, {
|
|
2148
|
-
api: api,
|
|
2149
|
-
baseUrl: apiBaseUrl
|
|
2150
|
-
}, children);
|
|
2151
|
-
};
|
|
2152
|
-
DataProvider.propTypes = propTypes;
|
|
2153
|
-
DataProvider.defaultProps = defaultProps;
|
|
2154
|
-
var DataProvider$1 = DataProvider;
|
|
2155
|
-
|
|
2156
|
-
exports.Api = Api$1;
|
|
2157
|
-
exports.ApiProvider = ApiProvider;
|
|
2158
|
-
exports.BaseApi = Base$1;
|
|
2159
|
-
exports.DataProvider = DataProvider$1;
|
|
2160
|
-
exports.QueryProvider = QueryProvider;
|
|
2161
|
-
exports.useApi = useApi;
|
|
2162
|
-
exports.useData = useData$1;
|
|
2163
|
-
exports.useItems = useItems$3;
|
|
2164
|
-
exports.useItemsLegacy = useItems$1;
|
|
2165
|
-
exports.useItemsStore = useItemsStore$1;
|
|
2166
|
-
exports.useQueryContext = useQueryContext;
|
|
2167
|
-
exports.useResourceDestroy = useResourceDestroy$1;
|
|
2168
|
-
exports.useResourceItem = useResourceItem$1;
|
|
2169
|
-
exports.useResourceItems = useResourceItems$1;
|
|
2170
|
-
exports.useResourceStore = useResourceCreate$1;
|
|
2171
|
-
exports.useResourceUpdate = useResourceUpdate$1;
|