@arrai-innovations/reactive-helpers 13.0.2 → 13.0.4

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.
@@ -51,7 +51,12 @@ export class ListInstanceError extends Error {
51
51
  * @typedef {object} ListInstanceOptions
52
52
  * @property {import('vue').UnwrapNestedRefs<ListInstanceProps>} props - The props for the list instance.
53
53
  * @property {object} [functions] - Default implementation are used as set by `setListCrud`.
54
- * @property {Function} [functions.list] - Provide the implementation for the list function.
54
+ * @property {import('../config/listCrud.js').ListFn} [functions.list] - Provide the implementation for the list
55
+ * function.
56
+ * @property {import('../config/listCrud.js').BulkDeleteFn} [functions.bulkDelete] - Provide the implementation for the bulkDelete
57
+ * function.
58
+ * @property {import('../config/listCrud.js').SubscribeFn} [functions.subscribe] - Provide the implementation for the
59
+ * subscribe function.
55
60
  * @property {boolean} [keepOldPages=false] - If true, pages will not be cleared when defaultPageCallback is called.
56
61
  */
57
62
 
@@ -251,6 +256,7 @@ export function useListInstance({ props, functions = {}, keepOldPages = false })
251
256
  crud: {
252
257
  args: {},
253
258
  list: undefined,
259
+ bulkDelete: undefined,
254
260
  },
255
261
  retrieveArgs: toRef(props, "retrieveArgs"),
256
262
  listArgs: toRef(props, "listArgs"),
@@ -289,6 +295,31 @@ export function useListInstance({ props, functions = {}, keepOldPages = false })
289
295
  list: null,
290
296
  };
291
297
 
298
+ async function bulkDeleteFn() {
299
+ if (state.loading) {
300
+ return Promise.reject(new ListInstanceError("already loading.", "already-loading"));
301
+ }
302
+ loadingError.setLoading();
303
+ loadingError.clearError();
304
+ return state.crud
305
+ .bulkDelete({
306
+ crudArgs: state.crud.args,
307
+ ids: Object.keys(state.objects).map(Number),
308
+ })
309
+ .then(() => {
310
+ assignReactiveObject(state.objects, {});
311
+ loadingError.clearError();
312
+ return Promise.resolve(true);
313
+ })
314
+ .catch((error) => {
315
+ loadingError.setError(error);
316
+ return Promise.resolve(false);
317
+ })
318
+ .finally(() => {
319
+ loadingError.clearLoading();
320
+ });
321
+ }
322
+
292
323
  function list() {
293
324
  // this function cannot be async, or the resulting promise will lose its .cancel() method
294
325
  if (promises.list) {
@@ -313,6 +344,7 @@ export function useListInstance({ props, functions = {}, keepOldPages = false })
313
344
  pageCallback: returnedObject.pageCallback,
314
345
  isCancelled: readonly(isCancelled),
315
346
  });
347
+
316
348
  let resolveState = false;
317
349
  if (listPromise.cancel) {
318
350
  promises.list.cancel = async () => {
@@ -419,6 +451,7 @@ export function useListInstance({ props, functions = {}, keepOldPages = false })
419
451
  const returnedObject = {
420
452
  state,
421
453
  list,
454
+ bulkDelete: bulkDeleteFn,
422
455
  addListObject,
423
456
  updateListObject,
424
457
  deleteListObject,
package/use/listKeys.js CHANGED
@@ -17,6 +17,7 @@ export const listInstanceStateKeys = [
17
17
  ];
18
18
  export const listInstanceFunctions = [
19
19
  "list",
20
+ "bulkDelete",
20
21
  "addListObject",
21
22
  "updateListObject",
22
23
  "deleteListObject",
@@ -5,20 +5,27 @@ import { readonly, ref } from "vue";
5
5
  */
6
6
 
7
7
  /**
8
- * A composable function for managing loading and error states.
9
- *
10
- * @returns {object} - An object containing reactive fields and actions for loading and error states.
11
- * @property {import("vue").Ref<boolean|undefined>} loading - Whether the component is loading.
12
- * @property {import("vue").Ref<Error|null>} error - The error that occurred.
13
- * @property {import("vue").Ref<boolean>} errored - Whether an error has occurred.
8
+ * @typedef {object} LoadingError
9
+ * @property {Readonly<import("vue").Ref<boolean|undefined>>} loading - Whether the component is loading.
10
+ * @property {Readonly<import("vue").Ref<Error|null>>} error - The error that occurred.
11
+ * @property {Readonly<import("vue").Ref<boolean>>} errored - Whether an error has occurred.
14
12
  * @property {() => void} setLoading - Set the loading state.
15
13
  * @property {() => void} clearLoading - Clear the loading state.
16
14
  * @property {(error) => void} setError - Set the error state.
17
15
  * @property {() => void} clearError - Clear the error state.
18
16
  */
17
+
18
+ /**
19
+ * A composable function for managing loading and error states.
20
+ *
21
+ * @returns {LoadingError} - An object containing reactive fields and actions for loading and error states.
22
+ */
19
23
  export function useLoadingError() {
24
+ /** @type {import("vue").Ref<boolean|undefined>} */
20
25
  const loading = ref(undefined);
26
+ /** @type {import("vue").Ref<Error|null>} */
21
27
  const error = ref(null);
28
+ /** @type {import("vue").Ref<boolean>} */
22
29
  const errored = ref(false);
23
30
 
24
31
  return {