quasar-ui-danx 0.0.39 → 0.0.40

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quasar-ui-danx",
3
- "version": "0.0.39",
3
+ "version": "0.0.40",
4
4
  "author": "Dan <dan@flytedesk.com>",
5
5
  "description": "DanX Vue / Quasar component library",
6
6
  "license": "MIT",
@@ -84,6 +84,9 @@
84
84
  </component>
85
85
  </q-td>
86
86
  </template>
87
+ <template #bottom>
88
+ <ActionInputComponent />
89
+ </template>
87
90
  </q-table>
88
91
  </template>
89
92
 
@@ -92,7 +95,7 @@ import { ref } from 'vue';
92
95
  import { getItem, setItem } from '../../helpers';
93
96
  import { DragHandleIcon as RowResizeIcon } from '../../svg';
94
97
  import { HandleDraggable } from '../DragAndDrop';
95
- import { mapSortBy, RenderComponent } from '../index';
98
+ import { ActionInputComponent, mapSortBy, RenderComponent } from '../index';
96
99
  import { ActionMenu, EmptyTableState, registerStickyScrolling, TableSummaryRow } from './index';
97
100
 
98
101
  defineEmits(['action', 'filter', 'update:quasar-pagination', 'update:selected-rows']);
@@ -4,18 +4,20 @@
4
4
  v-if="activeActionInput"
5
5
  :component="activeActionInput.component"
6
6
  :is-saving="isSaving"
7
- @confirm="activeActionInput.confirm"
7
+ @confirm="onConfirm"
8
8
  @close="activeActionInput.cancel"
9
9
  />
10
10
  </div>
11
11
  </template>
12
12
  <script setup>
13
- import RenderComponent from 'src/components/Utility/Tools/RenderComponent';
13
+ import { ref } from 'vue';
14
14
  import { activeActionInput } from '../../../helpers';
15
+ import RenderComponent from './RenderComponent';
15
16
 
16
- defineProps({
17
- isSaving: Boolean
18
-
19
- });
20
- const emit = defineEmits(['result']);
17
+ const isSaving = ref(false);
18
+ async function onConfirm(input) {
19
+ isSaving.value = true;
20
+ await activeActionInput.value.confirm(input);
21
+ isSaving.value = false;
22
+ }
21
23
  </script>
@@ -1,4 +1,4 @@
1
- import { ref, shallowRef } from "vue";
1
+ import { shallowRef } from "vue";
2
2
  import { FlashMessages } from "./index";
3
3
 
4
4
  interface ActionOptions {
@@ -8,9 +8,11 @@ interface ActionOptions {
8
8
  batch?: boolean;
9
9
  inputComponent?: (target: object[] | object) => any;
10
10
  enabled?: (target: object) => boolean;
11
- onAction?: (action: string | null, target: object, input: any) => any;
12
- onBatchAction?: (action: string | null, targets: object[], input: any) => any;
13
- onFinish?: (action: string | null, targets: object, input: any) => void;
11
+ onAction?: (action: string | null, target: object, input: any) => Promise<any>;
12
+ onBatchAction?: (action: string | null, targets: object[], input: any) => Promise<any>;
13
+ onSuccess?: (action: string | null, targets: object, input: any) => any;
14
+ onError?: (action: string | null, targets: object, input: any) => any;
15
+ onFinish?: (action: string | null, targets: object, input: any) => any;
14
16
  }
15
17
 
16
18
  export const activeActionInput = shallowRef(null);
@@ -23,8 +25,7 @@ export const activeActionInput = shallowRef(null);
23
25
  * @param {ActionOptions} globalOptions
24
26
  */
25
27
  export function useActions(actions: ActionOptions[], globalOptions: ActionOptions = null) {
26
- const activeAction = ref(null);
27
- const activeTarget = ref(null);
28
+ const isSavingTarget = shallowRef(null);
28
29
 
29
30
  /**
30
31
  * Resolves an action by name or object, adds globalOptions and overrides any passes options
@@ -44,8 +45,7 @@ export function useActions(actions: ActionOptions[], globalOptions: ActionOption
44
45
 
45
46
  return {
46
47
  actions,
47
- activeAction,
48
- activeTarget,
48
+ isSavingTarget,
49
49
  resolveAction,
50
50
 
51
51
  /**
@@ -66,7 +66,7 @@ export function useActions(actions: ActionOptions[], globalOptions: ActionOption
66
66
  },
67
67
 
68
68
  /**
69
- * Applies an action to an item.
69
+ * TODO: HOW TO INTEGRATE optimistic updates and single item updates?
70
70
  */
71
71
  async applyAction(item, input, itemData = {}) {
72
72
  isSavingItem.value = item;
@@ -99,24 +99,35 @@ export function useActions(actions: ActionOptions[], globalOptions: ActionOption
99
99
  async performAction(name: string | object, target: object[] | object, input: any = null) {
100
100
  const action = resolveAction(name);
101
101
  const component = action.inputComponent && action.inputComponent(target);
102
+ let result = null;
102
103
 
104
+ isSavingTarget.value = target;
105
+
106
+ // If no component input is required, we can directly perform the action
103
107
  if (component) {
104
- input = await new Promise((resolve, reject) => {
108
+ // If the action requires an input, we set the activeActionInput to the input component.
109
+ // This will tell the ActionInputComponent to render the input component, and confirm or cancel the
110
+ // action The confirm function has the input from the component passed and will resolve the promise
111
+ // with the result of the action
112
+ result = await new Promise((resolve, reject) => {
105
113
  activeActionInput.value = {
106
114
  component,
107
- confirm: resolve,
115
+ confirm: async () => resolve(await onConfirmAction(action, target, input)),
108
116
  cancel: reject
109
117
  };
110
118
  });
111
119
  activeActionInput.value = null;
120
+ } else {
121
+ result = await onConfirmAction(action, target, input);
112
122
  }
113
123
 
114
- return await onConfirmAction(action, target, input);
124
+ isSavingTarget.value = null;
125
+ return result;
115
126
  }
116
127
  };
117
128
  }
118
129
 
119
- async function onConfirmAction(action, target, input) {
130
+ async function onConfirmAction(action: ActionOptions, target: object[] | object, input: any = null) {
120
131
  if (!action.onAction) {
121
132
  throw new Error("No onAction handler found for the selected action:" + action.name);
122
133
  }
@@ -141,7 +152,7 @@ async function onConfirmAction(action, target, input) {
141
152
  }
142
153
 
143
154
  if (action.onSuccess) {
144
- await action.onSuccess(result, target, input);
155
+ action.onSuccess(result, target, input);
145
156
  }
146
157
 
147
158
  } else {
@@ -157,12 +168,12 @@ async function onConfirmAction(action, target, input) {
157
168
  FlashMessages.combine("error", errors);
158
169
 
159
170
  if (action.onError) {
160
- await action.onError(result, target, input);
171
+ action.onError(result, target, input);
161
172
  }
162
173
  }
163
174
 
164
175
  if (action.onFinish) {
165
- await action.onFinish(result, target, input);
176
+ action.onFinish(result, target, input);
166
177
  }
167
178
 
168
179
  return result;