@karta.io/utils 0.34.1 → 0.35.0
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.cjs +191 -95
- package/dist/index.d.ts +35 -22
- package/dist/index.mjs +193 -97
- package/package.json +9 -3
package/dist/index.cjs
CHANGED
|
@@ -5,6 +5,7 @@ const commonData = require('@karta.io/common-data');
|
|
|
5
5
|
const libphonenumberJs = require('libphonenumber-js');
|
|
6
6
|
const snakeCase = require('lodash/snakeCase.js');
|
|
7
7
|
const vue = require('vue');
|
|
8
|
+
const vueQuery = require('@tanstack/vue-query');
|
|
8
9
|
const core = require('@vueuse/core');
|
|
9
10
|
const dayjs = require('dayjs');
|
|
10
11
|
const utc = require('dayjs/plugin/utc.js');
|
|
@@ -270,126 +271,221 @@ function useAbortController(callback) {
|
|
|
270
271
|
|
|
271
272
|
function useAsyncMultiselect({
|
|
272
273
|
apiMethod,
|
|
273
|
-
|
|
274
|
-
selectedValues,
|
|
275
|
-
params = void 0
|
|
274
|
+
mapOptionCallback = void 0,
|
|
275
|
+
selectedValues = void 0,
|
|
276
|
+
params = void 0,
|
|
277
|
+
queryKey,
|
|
278
|
+
dataItemsKey = "results",
|
|
279
|
+
dataItemIdKey = "id",
|
|
280
|
+
dataCountKey = "count",
|
|
281
|
+
delay = 300,
|
|
282
|
+
onUpdated = void 0
|
|
276
283
|
}) {
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
284
|
+
const reactiveData = vue.reactive({
|
|
285
|
+
search: "",
|
|
286
|
+
page: 1,
|
|
287
|
+
selectedValues: core.toValue(selectedValues),
|
|
288
|
+
params: JSON.stringify(core.toValue(params))
|
|
289
|
+
});
|
|
290
|
+
const { call, abort } = useAbortController(apiMethod);
|
|
291
|
+
const loadWAC = () => call({
|
|
292
|
+
page: reactiveData.page,
|
|
293
|
+
search: reactiveData.search,
|
|
294
|
+
...reactiveData.selectedValues?.length && { includedIds: reactiveData.selectedValues },
|
|
295
|
+
...reactiveData.params && { ...JSON.parse(reactiveData.params) }
|
|
296
|
+
});
|
|
297
|
+
const { data, isFetching, isLoading } = vueQuery.useQuery({
|
|
298
|
+
queryKey: [queryKey, reactiveData],
|
|
299
|
+
queryFn: loadWAC
|
|
300
|
+
});
|
|
301
|
+
const itemsByPages = vue.ref(null);
|
|
280
302
|
const count = vue.ref(0);
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
if (search.value === value)
|
|
287
|
-
return;
|
|
288
|
-
search.value = value;
|
|
289
|
-
await loadItemsWithDebounce();
|
|
290
|
-
};
|
|
291
|
-
const loadItems = async (isSearch) => {
|
|
292
|
-
loading.value = true;
|
|
293
|
-
try {
|
|
294
|
-
const { results, count: itemsCount } = await apiMethodWAC({
|
|
295
|
-
page: page.value,
|
|
296
|
-
search: search.value,
|
|
297
|
-
...selectedValues.value?.length && !search.value && { includedIds: selectedValues.value },
|
|
298
|
-
...params
|
|
299
|
-
});
|
|
300
|
-
count.value = itemsCount;
|
|
301
|
-
if (isSearch) {
|
|
302
|
-
const cashedMapItems = selectedValues.value.map((selectedValue) => [
|
|
303
|
-
selectedValue,
|
|
304
|
-
items.value.get(selectedValue)
|
|
305
|
-
]);
|
|
306
|
-
items.value = new Map([...cashedMapItems]);
|
|
307
|
-
}
|
|
308
|
-
const resultsForMap = results.map(mapResultCallback);
|
|
309
|
-
items.value = new Map([...items.value, ...resultsForMap]);
|
|
310
|
-
page.value += 1;
|
|
311
|
-
loading.value = false;
|
|
312
|
-
} catch (error) {
|
|
313
|
-
if (isAborted(error))
|
|
303
|
+
const selectedItemsCached = vue.ref([]);
|
|
304
|
+
vue.watch(
|
|
305
|
+
data,
|
|
306
|
+
(newValue) => {
|
|
307
|
+
if (!newValue?.[dataItemsKey] || newValue[dataCountKey] === void 0)
|
|
314
308
|
return;
|
|
315
|
-
|
|
316
|
-
|
|
309
|
+
count.value = newValue[dataCountKey] || 0;
|
|
310
|
+
const newItems = newValue[dataItemsKey];
|
|
311
|
+
itemsByPages.value = {
|
|
312
|
+
...reactiveData.page !== 1 && itemsByPages.value,
|
|
313
|
+
[reactiveData.page]: newItems
|
|
314
|
+
};
|
|
315
|
+
if (reactiveData.page === 1 && reactiveData.selectedValues?.length && !reactiveData.search) {
|
|
316
|
+
selectedItemsCached.value = newItems.filter(
|
|
317
|
+
(item) => reactiveData.selectedValues.includes(item[dataItemIdKey])
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
onUpdated?.();
|
|
321
|
+
},
|
|
322
|
+
{ immediate: true }
|
|
323
|
+
);
|
|
324
|
+
const items = vue.computed(() => {
|
|
325
|
+
if (!itemsByPages.value)
|
|
326
|
+
return selectedItemsCached.value;
|
|
327
|
+
if (!Object.keys(itemsByPages.value).length)
|
|
328
|
+
return selectedItemsCached.value;
|
|
329
|
+
const itemsByPagesAsArray = Object.values(itemsByPages.value).reduce(
|
|
330
|
+
(acc, item) => [...acc, ...item],
|
|
331
|
+
[]
|
|
332
|
+
);
|
|
333
|
+
const itemsAsArray = [...selectedItemsCached.value, ...itemsByPagesAsArray];
|
|
334
|
+
const itemsAsMap = itemsAsArray.reduce((acc, item) => {
|
|
335
|
+
const key = item[dataItemIdKey] || JSON.stringify(item);
|
|
336
|
+
return acc.set(key, item);
|
|
337
|
+
}, /* @__PURE__ */ new Map());
|
|
338
|
+
return [...itemsAsMap.values()];
|
|
339
|
+
});
|
|
340
|
+
const options = vue.computed(() => {
|
|
341
|
+
if (!items.value.length)
|
|
342
|
+
return [];
|
|
343
|
+
if (!mapOptionCallback)
|
|
344
|
+
return this.items.value;
|
|
345
|
+
return items.value.map(mapOptionCallback);
|
|
346
|
+
});
|
|
347
|
+
const hasNextPage = vue.computed(() => count.value > items.value.length);
|
|
348
|
+
const loadPage = (newPage = 1) => {
|
|
349
|
+
reactiveData.page = newPage;
|
|
350
|
+
};
|
|
351
|
+
const updateSearchWithDebounce = core.useDebounceFn((value) => {
|
|
352
|
+
reactiveData.search = value;
|
|
353
|
+
reactiveData.page = 1;
|
|
354
|
+
}, delay);
|
|
355
|
+
const updateSearch = (value) => {
|
|
356
|
+
updateSearchWithDebounce(value);
|
|
317
357
|
};
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
358
|
+
if (vue.isRef(selectedValues)) {
|
|
359
|
+
vue.watch(selectedValues, async (newSelectedValues) => {
|
|
360
|
+
if (!newSelectedValues.length) {
|
|
361
|
+
selectedItemsCached.value = [];
|
|
362
|
+
} else {
|
|
363
|
+
selectedItemsCached.value = items.value.filter(
|
|
364
|
+
(item) => newSelectedValues.includes(item[dataItemIdKey])
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
await vue.nextTick();
|
|
368
|
+
reactiveData.page = 1;
|
|
369
|
+
reactiveData.selectedValues = newSelectedValues;
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
if (vue.isRef(params)) {
|
|
373
|
+
vue.watch(params, (newParams) => {
|
|
374
|
+
reactiveData.page = 1;
|
|
375
|
+
reactiveData.params = JSON.stringify(newParams);
|
|
376
|
+
});
|
|
377
|
+
}
|
|
322
378
|
vue.onBeforeUnmount(() => {
|
|
323
379
|
abort?.();
|
|
324
380
|
});
|
|
325
381
|
return {
|
|
326
|
-
|
|
382
|
+
items,
|
|
327
383
|
options,
|
|
328
|
-
|
|
384
|
+
search: vue.computed(() => reactiveData.search),
|
|
329
385
|
hasNextPage,
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
386
|
+
isFetching,
|
|
387
|
+
isLoading,
|
|
388
|
+
loadPage,
|
|
389
|
+
updateSearch
|
|
333
390
|
};
|
|
334
391
|
}
|
|
335
392
|
|
|
336
393
|
function useAsyncSelect({
|
|
337
394
|
apiMethod,
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
params = void 0
|
|
395
|
+
mapOptionCallback = void 0,
|
|
396
|
+
selectedValue = void 0,
|
|
397
|
+
params = void 0,
|
|
398
|
+
queryKey,
|
|
399
|
+
dataItemsKey = "results",
|
|
400
|
+
dataCountKey = "count",
|
|
401
|
+
delay = 300,
|
|
402
|
+
onUpdated = void 0
|
|
341
403
|
}) {
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
404
|
+
const reactiveData = vue.reactive({
|
|
405
|
+
search: "",
|
|
406
|
+
page: 1,
|
|
407
|
+
selectedValue: core.toValue(selectedValue),
|
|
408
|
+
params: JSON.stringify(core.toValue(params))
|
|
409
|
+
});
|
|
410
|
+
const { call, abort } = useAbortController(apiMethod);
|
|
411
|
+
const loadWAC = () => call({
|
|
412
|
+
page: reactiveData.page,
|
|
413
|
+
search: reactiveData.search,
|
|
414
|
+
...reactiveData.selectedValue && { includedIds: [reactiveData.selectedValue] },
|
|
415
|
+
...reactiveData.params && { ...JSON.parse(reactiveData.params) }
|
|
416
|
+
});
|
|
417
|
+
const { data, isFetching, isLoading } = vueQuery.useQuery({
|
|
418
|
+
queryKey: [queryKey, reactiveData],
|
|
419
|
+
queryFn: loadWAC
|
|
420
|
+
});
|
|
421
|
+
const itemsByPages = vue.ref(null);
|
|
345
422
|
const count = vue.ref(0);
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
const handleSearch = async (value) => {
|
|
351
|
-
if (search.value === value)
|
|
352
|
-
return;
|
|
353
|
-
search.value = value;
|
|
354
|
-
await loadItemsWithDebounce();
|
|
355
|
-
};
|
|
356
|
-
const loadItems = async (isSearch) => {
|
|
357
|
-
loading.value = true;
|
|
358
|
-
try {
|
|
359
|
-
const { results, count: itemsCount } = await apiMethodWAC({
|
|
360
|
-
page: page.value,
|
|
361
|
-
search: search.value,
|
|
362
|
-
...includedIds?.length && includedIds,
|
|
363
|
-
...params
|
|
364
|
-
});
|
|
365
|
-
count.value = itemsCount;
|
|
366
|
-
if (isSearch)
|
|
367
|
-
items.value = /* @__PURE__ */ new Map();
|
|
368
|
-
const resultsForMap = results.map(mapResultCallback);
|
|
369
|
-
items.value = new Map([...items.value, ...resultsForMap]);
|
|
370
|
-
page.value += 1;
|
|
371
|
-
loading.value = false;
|
|
372
|
-
} catch (error) {
|
|
373
|
-
if (isAborted(error))
|
|
423
|
+
vue.watch(
|
|
424
|
+
data,
|
|
425
|
+
(newValue) => {
|
|
426
|
+
if (!newValue?.[dataItemsKey] || newValue[dataCountKey] === void 0)
|
|
374
427
|
return;
|
|
375
|
-
|
|
376
|
-
|
|
428
|
+
count.value = newValue[dataCountKey] || 0;
|
|
429
|
+
itemsByPages.value = {
|
|
430
|
+
...reactiveData.page !== 1 && itemsByPages.value,
|
|
431
|
+
[reactiveData.page]: newValue[dataItemsKey]
|
|
432
|
+
};
|
|
433
|
+
onUpdated?.();
|
|
434
|
+
},
|
|
435
|
+
{ immediate: true }
|
|
436
|
+
);
|
|
437
|
+
const items = vue.computed(() => {
|
|
438
|
+
if (!itemsByPages.value)
|
|
439
|
+
return [];
|
|
440
|
+
if (!Object.keys(itemsByPages.value).length)
|
|
441
|
+
return [];
|
|
442
|
+
return Object.values(itemsByPages.value).reduce(
|
|
443
|
+
(acc, item) => [...acc, ...item],
|
|
444
|
+
[]
|
|
445
|
+
);
|
|
446
|
+
});
|
|
447
|
+
const options = vue.computed(() => {
|
|
448
|
+
if (!items.value.length)
|
|
449
|
+
return [];
|
|
450
|
+
if (!mapOptionCallback)
|
|
451
|
+
return this.items.value;
|
|
452
|
+
return items.value.map(mapOptionCallback);
|
|
453
|
+
});
|
|
454
|
+
const hasNextPage = vue.computed(() => count.value > items.value.length);
|
|
455
|
+
const loadPage = (newPage = 1) => {
|
|
456
|
+
reactiveData.page = newPage;
|
|
457
|
+
};
|
|
458
|
+
const updateSearchWithDebounce = core.useDebounceFn((value) => {
|
|
459
|
+
reactiveData.search = value;
|
|
460
|
+
reactiveData.page = 1;
|
|
461
|
+
}, delay);
|
|
462
|
+
const updateSearch = (value) => {
|
|
463
|
+
updateSearchWithDebounce(value);
|
|
377
464
|
};
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
465
|
+
if (vue.isRef(selectedValue)) {
|
|
466
|
+
vue.watch(selectedValue, (newSelectedValue) => {
|
|
467
|
+
reactiveData.page = 1;
|
|
468
|
+
reactiveData.selectedValue = newSelectedValue;
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
if (vue.isRef(params)) {
|
|
472
|
+
vue.watch(params, (newParams) => {
|
|
473
|
+
reactiveData.page = 1;
|
|
474
|
+
reactiveData.params = JSON.stringify(newParams);
|
|
475
|
+
});
|
|
476
|
+
}
|
|
382
477
|
vue.onBeforeUnmount(() => {
|
|
383
478
|
abort?.();
|
|
384
479
|
});
|
|
385
480
|
return {
|
|
386
|
-
|
|
481
|
+
items,
|
|
387
482
|
options,
|
|
388
|
-
|
|
483
|
+
search: vue.computed(() => reactiveData.search),
|
|
389
484
|
hasNextPage,
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
485
|
+
isFetching,
|
|
486
|
+
isLoading,
|
|
487
|
+
loadPage,
|
|
488
|
+
updateSearch
|
|
393
489
|
};
|
|
394
490
|
}
|
|
395
491
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as vue from 'vue';
|
|
2
|
-
import {
|
|
2
|
+
import { MaybeRef } from '@vueuse/core';
|
|
3
3
|
import dayjs from 'dayjs';
|
|
4
4
|
import { Browser } from 'detect-browser';
|
|
5
5
|
import logdown from 'logdown';
|
|
@@ -106,34 +106,47 @@ declare function useAbortController<T extends (...args: any[]) => any>(callback:
|
|
|
106
106
|
isAborted(error: ResponseError): boolean;
|
|
107
107
|
};
|
|
108
108
|
|
|
109
|
-
declare function useAsyncMultiselect<T extends (...args: any[]) => any, K extends (params:
|
|
109
|
+
declare function useAsyncMultiselect<T extends (...args: any[]) => any, K extends (params: any) => any>({ apiMethod, mapOptionCallback, selectedValues, params, queryKey, dataItemsKey, dataItemIdKey, dataCountKey, delay, onUpdated, }: {
|
|
110
110
|
apiMethod: T;
|
|
111
|
-
|
|
112
|
-
selectedValues
|
|
113
|
-
params?: Parameters<T>[0]
|
|
111
|
+
mapOptionCallback?: K;
|
|
112
|
+
selectedValues?: MaybeRef<Parameters<T>[0]['includedIds']>;
|
|
113
|
+
params?: MaybeRef<Partial<Parameters<T>[0]>>;
|
|
114
|
+
queryKey: string;
|
|
115
|
+
dataItemsKey?: string;
|
|
116
|
+
dataItemIdKey?: string;
|
|
117
|
+
dataCountKey?: string;
|
|
118
|
+
delay?: number;
|
|
119
|
+
onUpdated?: () => void;
|
|
114
120
|
}): {
|
|
115
|
-
|
|
116
|
-
options: ComputedRef<ReturnType<K>[
|
|
117
|
-
|
|
118
|
-
hasNextPage: ComputedRef<boolean>;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
121
|
+
items: vue.ComputedRef<any[]>;
|
|
122
|
+
options: vue.ComputedRef<ReturnType<K>[]>;
|
|
123
|
+
search: vue.ComputedRef<string>;
|
|
124
|
+
hasNextPage: vue.ComputedRef<boolean>;
|
|
125
|
+
isFetching: vue.Ref<boolean>;
|
|
126
|
+
isLoading: vue.Ref<false> | vue.Ref<true>;
|
|
127
|
+
loadPage: (newPage?: number) => void;
|
|
128
|
+
updateSearch: (value: string) => void;
|
|
122
129
|
};
|
|
123
130
|
|
|
124
|
-
declare function useAsyncSelect<T extends (...args: any[]) => any, K extends (params:
|
|
131
|
+
declare function useAsyncSelect<T extends (...args: any[]) => any, K extends (params: any) => any>({ apiMethod, mapOptionCallback, selectedValue, params, queryKey, dataItemsKey, dataCountKey, delay, onUpdated, }: {
|
|
125
132
|
apiMethod: T;
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
params?: Parameters<T>[0]
|
|
133
|
+
mapOptionCallback?: K;
|
|
134
|
+
selectedValue?: MaybeRef<Parameters<T>[0]['includedIds']>;
|
|
135
|
+
params?: MaybeRef<Partial<Parameters<T>[0]>>;
|
|
136
|
+
queryKey: string;
|
|
137
|
+
dataItemsKey?: string;
|
|
138
|
+
dataCountKey?: string;
|
|
139
|
+
delay?: number;
|
|
140
|
+
onUpdated?: () => void;
|
|
129
141
|
}): {
|
|
130
|
-
|
|
131
|
-
options: vue.ComputedRef<ReturnType<K>[
|
|
132
|
-
|
|
142
|
+
items: vue.ComputedRef<any[]>;
|
|
143
|
+
options: vue.ComputedRef<ReturnType<K>[]>;
|
|
144
|
+
search: vue.ComputedRef<string>;
|
|
133
145
|
hasNextPage: vue.ComputedRef<boolean>;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
146
|
+
isFetching: vue.Ref<boolean>;
|
|
147
|
+
isLoading: vue.Ref<false> | vue.Ref<true>;
|
|
148
|
+
loadPage: (newPage?: number) => void;
|
|
149
|
+
updateSearch: (value: string) => void;
|
|
137
150
|
};
|
|
138
151
|
|
|
139
152
|
declare function useClassName(block: string): {
|
package/dist/index.mjs
CHANGED
|
@@ -2,8 +2,9 @@ import camelCase from 'lodash/camelCase.js';
|
|
|
2
2
|
import { CURRENCY_DEFAULT } from '@karta.io/common-data';
|
|
3
3
|
import { parsePhoneNumberFromString } from 'libphonenumber-js';
|
|
4
4
|
import snakeCase from 'lodash/snakeCase.js';
|
|
5
|
-
import { ref, computed, onBeforeUnmount } from 'vue';
|
|
6
|
-
import {
|
|
5
|
+
import { reactive, ref, watch, computed, isRef, nextTick, onBeforeUnmount } from 'vue';
|
|
6
|
+
import { useQuery } from '@tanstack/vue-query';
|
|
7
|
+
import { toValue, useDebounceFn } from '@vueuse/core';
|
|
7
8
|
import dayjs from 'dayjs';
|
|
8
9
|
import utc from 'dayjs/plugin/utc.js';
|
|
9
10
|
import tz from 'dayjs/plugin/timezone.js';
|
|
@@ -256,126 +257,221 @@ function useAbortController(callback) {
|
|
|
256
257
|
|
|
257
258
|
function useAsyncMultiselect({
|
|
258
259
|
apiMethod,
|
|
259
|
-
|
|
260
|
-
selectedValues,
|
|
261
|
-
params = void 0
|
|
260
|
+
mapOptionCallback = void 0,
|
|
261
|
+
selectedValues = void 0,
|
|
262
|
+
params = void 0,
|
|
263
|
+
queryKey,
|
|
264
|
+
dataItemsKey = "results",
|
|
265
|
+
dataItemIdKey = "id",
|
|
266
|
+
dataCountKey = "count",
|
|
267
|
+
delay = 300,
|
|
268
|
+
onUpdated = void 0
|
|
262
269
|
}) {
|
|
263
|
-
const
|
|
264
|
-
|
|
265
|
-
|
|
270
|
+
const reactiveData = reactive({
|
|
271
|
+
search: "",
|
|
272
|
+
page: 1,
|
|
273
|
+
selectedValues: toValue(selectedValues),
|
|
274
|
+
params: JSON.stringify(toValue(params))
|
|
275
|
+
});
|
|
276
|
+
const { call, abort } = useAbortController(apiMethod);
|
|
277
|
+
const loadWAC = () => call({
|
|
278
|
+
page: reactiveData.page,
|
|
279
|
+
search: reactiveData.search,
|
|
280
|
+
...reactiveData.selectedValues?.length && { includedIds: reactiveData.selectedValues },
|
|
281
|
+
...reactiveData.params && { ...JSON.parse(reactiveData.params) }
|
|
282
|
+
});
|
|
283
|
+
const { data, isFetching, isLoading } = useQuery({
|
|
284
|
+
queryKey: [queryKey, reactiveData],
|
|
285
|
+
queryFn: loadWAC
|
|
286
|
+
});
|
|
287
|
+
const itemsByPages = ref(null);
|
|
266
288
|
const count = ref(0);
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
if (search.value === value)
|
|
273
|
-
return;
|
|
274
|
-
search.value = value;
|
|
275
|
-
await loadItemsWithDebounce();
|
|
276
|
-
};
|
|
277
|
-
const loadItems = async (isSearch) => {
|
|
278
|
-
loading.value = true;
|
|
279
|
-
try {
|
|
280
|
-
const { results, count: itemsCount } = await apiMethodWAC({
|
|
281
|
-
page: page.value,
|
|
282
|
-
search: search.value,
|
|
283
|
-
...selectedValues.value?.length && !search.value && { includedIds: selectedValues.value },
|
|
284
|
-
...params
|
|
285
|
-
});
|
|
286
|
-
count.value = itemsCount;
|
|
287
|
-
if (isSearch) {
|
|
288
|
-
const cashedMapItems = selectedValues.value.map((selectedValue) => [
|
|
289
|
-
selectedValue,
|
|
290
|
-
items.value.get(selectedValue)
|
|
291
|
-
]);
|
|
292
|
-
items.value = new Map([...cashedMapItems]);
|
|
293
|
-
}
|
|
294
|
-
const resultsForMap = results.map(mapResultCallback);
|
|
295
|
-
items.value = new Map([...items.value, ...resultsForMap]);
|
|
296
|
-
page.value += 1;
|
|
297
|
-
loading.value = false;
|
|
298
|
-
} catch (error) {
|
|
299
|
-
if (isAborted(error))
|
|
289
|
+
const selectedItemsCached = ref([]);
|
|
290
|
+
watch(
|
|
291
|
+
data,
|
|
292
|
+
(newValue) => {
|
|
293
|
+
if (!newValue?.[dataItemsKey] || newValue[dataCountKey] === void 0)
|
|
300
294
|
return;
|
|
301
|
-
|
|
302
|
-
|
|
295
|
+
count.value = newValue[dataCountKey] || 0;
|
|
296
|
+
const newItems = newValue[dataItemsKey];
|
|
297
|
+
itemsByPages.value = {
|
|
298
|
+
...reactiveData.page !== 1 && itemsByPages.value,
|
|
299
|
+
[reactiveData.page]: newItems
|
|
300
|
+
};
|
|
301
|
+
if (reactiveData.page === 1 && reactiveData.selectedValues?.length && !reactiveData.search) {
|
|
302
|
+
selectedItemsCached.value = newItems.filter(
|
|
303
|
+
(item) => reactiveData.selectedValues.includes(item[dataItemIdKey])
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
onUpdated?.();
|
|
307
|
+
},
|
|
308
|
+
{ immediate: true }
|
|
309
|
+
);
|
|
310
|
+
const items = computed(() => {
|
|
311
|
+
if (!itemsByPages.value)
|
|
312
|
+
return selectedItemsCached.value;
|
|
313
|
+
if (!Object.keys(itemsByPages.value).length)
|
|
314
|
+
return selectedItemsCached.value;
|
|
315
|
+
const itemsByPagesAsArray = Object.values(itemsByPages.value).reduce(
|
|
316
|
+
(acc, item) => [...acc, ...item],
|
|
317
|
+
[]
|
|
318
|
+
);
|
|
319
|
+
const itemsAsArray = [...selectedItemsCached.value, ...itemsByPagesAsArray];
|
|
320
|
+
const itemsAsMap = itemsAsArray.reduce((acc, item) => {
|
|
321
|
+
const key = item[dataItemIdKey] || JSON.stringify(item);
|
|
322
|
+
return acc.set(key, item);
|
|
323
|
+
}, /* @__PURE__ */ new Map());
|
|
324
|
+
return [...itemsAsMap.values()];
|
|
325
|
+
});
|
|
326
|
+
const options = computed(() => {
|
|
327
|
+
if (!items.value.length)
|
|
328
|
+
return [];
|
|
329
|
+
if (!mapOptionCallback)
|
|
330
|
+
return this.items.value;
|
|
331
|
+
return items.value.map(mapOptionCallback);
|
|
332
|
+
});
|
|
333
|
+
const hasNextPage = computed(() => count.value > items.value.length);
|
|
334
|
+
const loadPage = (newPage = 1) => {
|
|
335
|
+
reactiveData.page = newPage;
|
|
336
|
+
};
|
|
337
|
+
const updateSearchWithDebounce = useDebounceFn((value) => {
|
|
338
|
+
reactiveData.search = value;
|
|
339
|
+
reactiveData.page = 1;
|
|
340
|
+
}, delay);
|
|
341
|
+
const updateSearch = (value) => {
|
|
342
|
+
updateSearchWithDebounce(value);
|
|
303
343
|
};
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
344
|
+
if (isRef(selectedValues)) {
|
|
345
|
+
watch(selectedValues, async (newSelectedValues) => {
|
|
346
|
+
if (!newSelectedValues.length) {
|
|
347
|
+
selectedItemsCached.value = [];
|
|
348
|
+
} else {
|
|
349
|
+
selectedItemsCached.value = items.value.filter(
|
|
350
|
+
(item) => newSelectedValues.includes(item[dataItemIdKey])
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
await nextTick();
|
|
354
|
+
reactiveData.page = 1;
|
|
355
|
+
reactiveData.selectedValues = newSelectedValues;
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
if (isRef(params)) {
|
|
359
|
+
watch(params, (newParams) => {
|
|
360
|
+
reactiveData.page = 1;
|
|
361
|
+
reactiveData.params = JSON.stringify(newParams);
|
|
362
|
+
});
|
|
363
|
+
}
|
|
308
364
|
onBeforeUnmount(() => {
|
|
309
365
|
abort?.();
|
|
310
366
|
});
|
|
311
367
|
return {
|
|
312
|
-
|
|
368
|
+
items,
|
|
313
369
|
options,
|
|
314
|
-
|
|
370
|
+
search: computed(() => reactiveData.search),
|
|
315
371
|
hasNextPage,
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
372
|
+
isFetching,
|
|
373
|
+
isLoading,
|
|
374
|
+
loadPage,
|
|
375
|
+
updateSearch
|
|
319
376
|
};
|
|
320
377
|
}
|
|
321
378
|
|
|
322
379
|
function useAsyncSelect({
|
|
323
380
|
apiMethod,
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
params = void 0
|
|
381
|
+
mapOptionCallback = void 0,
|
|
382
|
+
selectedValue = void 0,
|
|
383
|
+
params = void 0,
|
|
384
|
+
queryKey,
|
|
385
|
+
dataItemsKey = "results",
|
|
386
|
+
dataCountKey = "count",
|
|
387
|
+
delay = 300,
|
|
388
|
+
onUpdated = void 0
|
|
327
389
|
}) {
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
|
|
390
|
+
const reactiveData = reactive({
|
|
391
|
+
search: "",
|
|
392
|
+
page: 1,
|
|
393
|
+
selectedValue: toValue(selectedValue),
|
|
394
|
+
params: JSON.stringify(toValue(params))
|
|
395
|
+
});
|
|
396
|
+
const { call, abort } = useAbortController(apiMethod);
|
|
397
|
+
const loadWAC = () => call({
|
|
398
|
+
page: reactiveData.page,
|
|
399
|
+
search: reactiveData.search,
|
|
400
|
+
...reactiveData.selectedValue && { includedIds: [reactiveData.selectedValue] },
|
|
401
|
+
...reactiveData.params && { ...JSON.parse(reactiveData.params) }
|
|
402
|
+
});
|
|
403
|
+
const { data, isFetching, isLoading } = useQuery({
|
|
404
|
+
queryKey: [queryKey, reactiveData],
|
|
405
|
+
queryFn: loadWAC
|
|
406
|
+
});
|
|
407
|
+
const itemsByPages = ref(null);
|
|
331
408
|
const count = ref(0);
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
const handleSearch = async (value) => {
|
|
337
|
-
if (search.value === value)
|
|
338
|
-
return;
|
|
339
|
-
search.value = value;
|
|
340
|
-
await loadItemsWithDebounce();
|
|
341
|
-
};
|
|
342
|
-
const loadItems = async (isSearch) => {
|
|
343
|
-
loading.value = true;
|
|
344
|
-
try {
|
|
345
|
-
const { results, count: itemsCount } = await apiMethodWAC({
|
|
346
|
-
page: page.value,
|
|
347
|
-
search: search.value,
|
|
348
|
-
...includedIds?.length && includedIds,
|
|
349
|
-
...params
|
|
350
|
-
});
|
|
351
|
-
count.value = itemsCount;
|
|
352
|
-
if (isSearch)
|
|
353
|
-
items.value = /* @__PURE__ */ new Map();
|
|
354
|
-
const resultsForMap = results.map(mapResultCallback);
|
|
355
|
-
items.value = new Map([...items.value, ...resultsForMap]);
|
|
356
|
-
page.value += 1;
|
|
357
|
-
loading.value = false;
|
|
358
|
-
} catch (error) {
|
|
359
|
-
if (isAborted(error))
|
|
409
|
+
watch(
|
|
410
|
+
data,
|
|
411
|
+
(newValue) => {
|
|
412
|
+
if (!newValue?.[dataItemsKey] || newValue[dataCountKey] === void 0)
|
|
360
413
|
return;
|
|
361
|
-
|
|
362
|
-
|
|
414
|
+
count.value = newValue[dataCountKey] || 0;
|
|
415
|
+
itemsByPages.value = {
|
|
416
|
+
...reactiveData.page !== 1 && itemsByPages.value,
|
|
417
|
+
[reactiveData.page]: newValue[dataItemsKey]
|
|
418
|
+
};
|
|
419
|
+
onUpdated?.();
|
|
420
|
+
},
|
|
421
|
+
{ immediate: true }
|
|
422
|
+
);
|
|
423
|
+
const items = computed(() => {
|
|
424
|
+
if (!itemsByPages.value)
|
|
425
|
+
return [];
|
|
426
|
+
if (!Object.keys(itemsByPages.value).length)
|
|
427
|
+
return [];
|
|
428
|
+
return Object.values(itemsByPages.value).reduce(
|
|
429
|
+
(acc, item) => [...acc, ...item],
|
|
430
|
+
[]
|
|
431
|
+
);
|
|
432
|
+
});
|
|
433
|
+
const options = computed(() => {
|
|
434
|
+
if (!items.value.length)
|
|
435
|
+
return [];
|
|
436
|
+
if (!mapOptionCallback)
|
|
437
|
+
return this.items.value;
|
|
438
|
+
return items.value.map(mapOptionCallback);
|
|
439
|
+
});
|
|
440
|
+
const hasNextPage = computed(() => count.value > items.value.length);
|
|
441
|
+
const loadPage = (newPage = 1) => {
|
|
442
|
+
reactiveData.page = newPage;
|
|
443
|
+
};
|
|
444
|
+
const updateSearchWithDebounce = useDebounceFn((value) => {
|
|
445
|
+
reactiveData.search = value;
|
|
446
|
+
reactiveData.page = 1;
|
|
447
|
+
}, delay);
|
|
448
|
+
const updateSearch = (value) => {
|
|
449
|
+
updateSearchWithDebounce(value);
|
|
363
450
|
};
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
451
|
+
if (isRef(selectedValue)) {
|
|
452
|
+
watch(selectedValue, (newSelectedValue) => {
|
|
453
|
+
reactiveData.page = 1;
|
|
454
|
+
reactiveData.selectedValue = newSelectedValue;
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
if (isRef(params)) {
|
|
458
|
+
watch(params, (newParams) => {
|
|
459
|
+
reactiveData.page = 1;
|
|
460
|
+
reactiveData.params = JSON.stringify(newParams);
|
|
461
|
+
});
|
|
462
|
+
}
|
|
368
463
|
onBeforeUnmount(() => {
|
|
369
464
|
abort?.();
|
|
370
465
|
});
|
|
371
466
|
return {
|
|
372
|
-
|
|
467
|
+
items,
|
|
373
468
|
options,
|
|
374
|
-
|
|
469
|
+
search: computed(() => reactiveData.search),
|
|
375
470
|
hasNextPage,
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
471
|
+
isFetching,
|
|
472
|
+
isLoading,
|
|
473
|
+
loadPage,
|
|
474
|
+
updateSearch
|
|
379
475
|
};
|
|
380
476
|
}
|
|
381
477
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karta.io/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.35.0",
|
|
4
4
|
"description": "karta.io utils for admin and application",
|
|
5
5
|
"author": "Vitaly Blokhin <karambafe@gmail.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -21,11 +21,17 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"peerDependencies": {
|
|
24
|
+
"@tanstack/vue-query": "4.29.20",
|
|
24
25
|
"vue": "3.3.x"
|
|
25
26
|
},
|
|
27
|
+
"peerDependenciesMeta": {
|
|
28
|
+
"@tanstack/vue-query": {
|
|
29
|
+
"optional": true
|
|
30
|
+
}
|
|
31
|
+
},
|
|
26
32
|
"dependencies": {
|
|
27
|
-
"@karta.io/common-data": "^0.
|
|
28
|
-
"@vueuse/core": "
|
|
33
|
+
"@karta.io/common-data": "^0.35.0",
|
|
34
|
+
"@vueuse/core": "10.2.1",
|
|
29
35
|
"async-validator": "4.2.5",
|
|
30
36
|
"dayjs": "1.11.8",
|
|
31
37
|
"detect-browser": "5.3.0",
|