@webitel/ui-sdk 24.8.12 → 24.8.13
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/ui-sdk.js +1 -1
- package/dist/ui-sdk.umd.cjs +1 -1
- package/package.json +15 -1
- package/src/store/new/helpers/createApiStoreModule.js +8 -0
- package/src/store/new/helpers/createBaseStoreModule.js +9 -0
- package/src/store/new/helpers/createCardStoreModule.js +8 -0
- package/src/store/new/helpers/createStoreModule.js +10 -0
- package/src/store/new/helpers/createTableStoreModule.js +8 -0
- package/src/store/new/index.js +16 -0
- package/src/store/new/modules/apiStoreModule/apiStoreModule.js +95 -0
- package/src/store/new/modules/baseStoreModule/baseStoreModule.js +21 -0
- package/src/store/new/modules/cardStoreModule/cardStoreModule.js +74 -0
- package/src/store/new/modules/cardStoreModule/useCardStore.js +54 -0
- package/src/store/new/modules/tableStoreModule/__tests__/tableStoreModule.spec.js +250 -0
- package/src/store/new/modules/tableStoreModule/tableStoreModule.js +232 -0
- package/src/store/new/modules/tableStoreModule/useTableStore.js +66 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import {
|
|
2
|
+
queryToSortAdapter,
|
|
3
|
+
sortToQueryAdapter,
|
|
4
|
+
} from '../../../../scripts/sortQueryAdapters.js';
|
|
5
|
+
import FilterEvent from '../../../../modules/Filters/enums/FilterEvent.enum.js';
|
|
6
|
+
|
|
7
|
+
const state = {
|
|
8
|
+
headers: [],
|
|
9
|
+
dataList: [],
|
|
10
|
+
selected: [],
|
|
11
|
+
error: {},
|
|
12
|
+
isLoading: false,
|
|
13
|
+
isNextPage: false,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const getters = {
|
|
17
|
+
PARENT_ID: () => null, // override me
|
|
18
|
+
|
|
19
|
+
// FIXME: maybe move to filters module?
|
|
20
|
+
FILTERS: (state, getters) => getters['filters/GET_FILTERS'],
|
|
21
|
+
|
|
22
|
+
FIELDS: (state) => {
|
|
23
|
+
const fields = state.headers.reduce((fields, { show, field }) => {
|
|
24
|
+
if (show) return [...fields, field];
|
|
25
|
+
return fields;
|
|
26
|
+
}, []);
|
|
27
|
+
|
|
28
|
+
return [...new Set(['id', ...fields])];
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
// main GET_LIST params collector
|
|
32
|
+
GET_LIST_PARAMS: (state, getters) => (overrides) => {
|
|
33
|
+
const filters = getters.FILTERS();
|
|
34
|
+
const fields = getters.FIELDS;
|
|
35
|
+
const parentId = getters.PARENT_ID;
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
parentId,
|
|
39
|
+
...filters,
|
|
40
|
+
fields,
|
|
41
|
+
...overrides,
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const actions = {
|
|
47
|
+
// FIXME: maybe move to filters module?
|
|
48
|
+
SET_FILTER: (
|
|
49
|
+
context,
|
|
50
|
+
payload,
|
|
51
|
+
) => context.dispatch('filters/SET_FILTER', payload),
|
|
52
|
+
|
|
53
|
+
// FIXME: maybe move to filters module?
|
|
54
|
+
ON_FILTER_EVENT: async (context, { event, payload }) => {
|
|
55
|
+
switch (event) {
|
|
56
|
+
case FilterEvent.RESTORED:
|
|
57
|
+
return context.dispatch('HANDLE_FILTERS_RESTORE', payload);
|
|
58
|
+
case FilterEvent.FILTER_SET:
|
|
59
|
+
return context.dispatch('HANDLE_FILTER_SET', payload);
|
|
60
|
+
default:
|
|
61
|
+
throw new Error(`Unknown filter event: ${event}`);
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
// FIXME: maybe move to filters module?
|
|
66
|
+
HANDLE_FILTERS_RESTORE: async (context, {
|
|
67
|
+
fields,
|
|
68
|
+
sort,
|
|
69
|
+
}) => {
|
|
70
|
+
if (sort) await context.dispatch('HANDLE_SORT_CHANGE', { value: sort });
|
|
71
|
+
if (fields?.length) await context.dispatch('HANDLE_FIELDS_CHANGE', { value: fields });
|
|
72
|
+
return context.dispatch('LOAD_DATA_LIST');
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
// FIXME: maybe move to filters module?
|
|
76
|
+
HANDLE_FILTER_SET: async (context, payload) => {
|
|
77
|
+
if (payload.name === 'fields') {
|
|
78
|
+
await context.dispatch('HANDLE_FIELDS_CHANGE', payload);
|
|
79
|
+
} else if (payload.name === 'sort') {
|
|
80
|
+
await context.dispatch('HANDLE_SORT_CHANGE', payload);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (context.getters.FILTERS().page && payload.name !== 'page') {
|
|
84
|
+
await context.dispatch('SET_FILTER', {
|
|
85
|
+
name: 'page',
|
|
86
|
+
value: 1,
|
|
87
|
+
silent: true,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return context.dispatch('LOAD_DATA_LIST');
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
// FIXME: maybe move to filters module?
|
|
95
|
+
HANDLE_FIELDS_CHANGE: (context, { value }) => {
|
|
96
|
+
const headers = context.state.headers.map((header) => ({
|
|
97
|
+
...header,
|
|
98
|
+
show: value.includes(header.value),
|
|
99
|
+
}));
|
|
100
|
+
|
|
101
|
+
context.commit('SET', { path: 'headers', value: headers });
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
// FIXME: maybe move to filters module?
|
|
105
|
+
HANDLE_SORT_CHANGE: (context, { value }) => {
|
|
106
|
+
const nextSort = queryToSortAdapter(value?.slice(0, 1) || '');
|
|
107
|
+
const field = nextSort ? value.slice(1) : value;
|
|
108
|
+
|
|
109
|
+
const headers = context.state.headers.map(({
|
|
110
|
+
sort: currentSort,
|
|
111
|
+
...header
|
|
112
|
+
}) => {
|
|
113
|
+
let sort;
|
|
114
|
+
|
|
115
|
+
if (field) {
|
|
116
|
+
sort = field === header.field ? nextSort : currentSort;
|
|
117
|
+
} else {
|
|
118
|
+
sort = nextSort; // null
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return { ...header, sort };
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
context.commit('SET', { path: 'headers', value: headers });
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
LOAD_DATA_LIST: async (context, query) => {
|
|
128
|
+
context.commit('SET', { path: 'isLoading', value: true });
|
|
129
|
+
context.commit('SET', { path: 'isNextPage', value: false }); // [WTEL-3560]
|
|
130
|
+
|
|
131
|
+
const params = context.getters.GET_LIST_PARAMS(query);
|
|
132
|
+
try {
|
|
133
|
+
let {
|
|
134
|
+
items = [],
|
|
135
|
+
next = false,
|
|
136
|
+
} = await context.dispatch('api/GET_LIST', { context, params });
|
|
137
|
+
|
|
138
|
+
context.commit('SET', { path: 'dataList', value: items });
|
|
139
|
+
context.commit('SET', { path: 'isNextPage', value: next });
|
|
140
|
+
} catch (err) {
|
|
141
|
+
context.commit('SET', { path: 'error', value: err });
|
|
142
|
+
throw err;
|
|
143
|
+
} finally {
|
|
144
|
+
context.commit('SET', { path: 'isLoading', value: false });
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
// FIXME: maybe move to filters module?
|
|
149
|
+
SORT: (context, { header, nextSortOrder }) => {
|
|
150
|
+
const sort = nextSortOrder
|
|
151
|
+
? `${sortToQueryAdapter(nextSortOrder)}${header.field}`
|
|
152
|
+
: nextSortOrder;
|
|
153
|
+
return context.dispatch('SET_FILTER', {
|
|
154
|
+
name: 'sort',
|
|
155
|
+
value: sort,
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
PATCH_ITEM_PROPERTY: async (context, {
|
|
160
|
+
item: _item, index, prop, value,
|
|
161
|
+
}) => {
|
|
162
|
+
const item = _item || context.state.dataList[index];
|
|
163
|
+
|
|
164
|
+
const { id, etag } = item;
|
|
165
|
+
|
|
166
|
+
context.commit('SET', {
|
|
167
|
+
path: `dataList[${index}].${prop}`,
|
|
168
|
+
value,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const changes = { [prop]: value };
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
await context.dispatch('api/PATCH_ITEM', {
|
|
175
|
+
context,
|
|
176
|
+
id,
|
|
177
|
+
etag,
|
|
178
|
+
changes,
|
|
179
|
+
});
|
|
180
|
+
} catch (err) {
|
|
181
|
+
await context.dispatch('LOAD_DATA_LIST');
|
|
182
|
+
throw err;
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
DELETE: async (context, deleted) => {
|
|
187
|
+
let action = 'DELETE_SINGLE';
|
|
188
|
+
if (Array.isArray(deleted)) {
|
|
189
|
+
if (deleted.length) action = 'DELETE_BULK';
|
|
190
|
+
else action = 'DELETE_ALL';
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
await context.dispatch(action, deleted);
|
|
194
|
+
} catch (err) {
|
|
195
|
+
throw err;
|
|
196
|
+
} finally {
|
|
197
|
+
await context.dispatch('LOAD_DATA_LIST');
|
|
198
|
+
|
|
199
|
+
/* if no items on current page after DELETE, move to prev page [WTEL-3793] */
|
|
200
|
+
if (!context.state.dataList.length && context.getters.FILTERS.page >
|
|
201
|
+
1) {
|
|
202
|
+
await context.dispatch('SET_FILTER', {
|
|
203
|
+
name: 'page',
|
|
204
|
+
value: context.getters.FILTERS.page - 1,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
|
|
210
|
+
DELETE_SINGLE: async (context, { id, etag }) => {
|
|
211
|
+
try {
|
|
212
|
+
await context.dispatch('api/DELETE_ITEM', { context, id, etag });
|
|
213
|
+
} catch (err) {
|
|
214
|
+
throw err;
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
|
|
218
|
+
DELETE_BULK: async (
|
|
219
|
+
context,
|
|
220
|
+
deleted,
|
|
221
|
+
) => Promise.allSettled(deleted.map((item) => context.dispatch('DELETE_SINGLE', item))),
|
|
222
|
+
|
|
223
|
+
SET_SELECTED: (context, selected) => {
|
|
224
|
+
context.commit('SET', { path: 'selected', value: selected });
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
export default {
|
|
229
|
+
state,
|
|
230
|
+
getters,
|
|
231
|
+
actions,
|
|
232
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { computed } from 'vue';
|
|
2
|
+
import { useStore } from 'vuex';
|
|
3
|
+
import getNamespacedState from '../../../store/helpers/getNamespacedState.js';
|
|
4
|
+
|
|
5
|
+
export const useTableStore = (namespace) => {
|
|
6
|
+
const store = useStore();
|
|
7
|
+
|
|
8
|
+
const tableNamespace = `${namespace}/table`;
|
|
9
|
+
|
|
10
|
+
const dataList = computed(() => getNamespacedState(store.state, tableNamespace).dataList);
|
|
11
|
+
|
|
12
|
+
const selected = computed(() => getNamespacedState(store.state, tableNamespace).selected);
|
|
13
|
+
|
|
14
|
+
const isLoading = computed(() => getNamespacedState(store.state, tableNamespace).isLoading);
|
|
15
|
+
|
|
16
|
+
const headers = computed(() => getNamespacedState(store.state, tableNamespace).headers);
|
|
17
|
+
|
|
18
|
+
const isNext = computed(() => getNamespacedState(store.state, tableNamespace).isNextPage);
|
|
19
|
+
|
|
20
|
+
const error = computed(() => getNamespacedState(store.state, tableNamespace).errors);
|
|
21
|
+
|
|
22
|
+
function loadData(payload) {
|
|
23
|
+
return store.dispatch(`${tableNamespace}/LOAD_DATA_LIST`, payload);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function onFilterEvent(payload) {
|
|
27
|
+
return store.dispatch(`${tableNamespace}/ON_FILTER_EVENT`, payload);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function patchProperty(payload) {
|
|
31
|
+
return store.dispatch(`${tableNamespace}/PATCH_ITEM_PROPERTY`, payload);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function deleteData(payload) {
|
|
35
|
+
return store.dispatch(`${tableNamespace}/DELETE`, payload);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function sort(header, nextSortOrder) {
|
|
39
|
+
return store.dispatch(`${tableNamespace}/SORT`, {
|
|
40
|
+
header,
|
|
41
|
+
nextSortOrder,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function setSelected(payload) {
|
|
46
|
+
return store.dispatch(`${tableNamespace}/SET_SELECTED`, payload);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
namespace: tableNamespace,
|
|
51
|
+
|
|
52
|
+
dataList,
|
|
53
|
+
selected,
|
|
54
|
+
isLoading,
|
|
55
|
+
headers,
|
|
56
|
+
isNext,
|
|
57
|
+
error,
|
|
58
|
+
|
|
59
|
+
loadData,
|
|
60
|
+
onFilterEvent,
|
|
61
|
+
patchProperty,
|
|
62
|
+
deleteData,
|
|
63
|
+
sort,
|
|
64
|
+
setSelected,
|
|
65
|
+
};
|
|
66
|
+
};
|