@tap-payments/os-micro-frontend-shared 0.1.438-test.2 → 0.1.438-test.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.
|
@@ -7,51 +7,136 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { useCallback, useMemo, useState
|
|
10
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
11
11
|
import { updateColumnSelection } from '../../../../utils/index.js';
|
|
12
|
-
import { handleTemplateUpdate,
|
|
12
|
+
import { handleTemplateUpdate, mapColumnsToLayoutSection, convertTemplateToColumnsView, convertTemplatesToColumnsView, isTemplateMatchingId, replaceTemplateInList, updateCurrentTemplate, } from '../utils';
|
|
13
13
|
export function useCustomTableViews({ templates, setTemplates, createTemplate, updateTemplate, deleteTemplate, tableMode, lang = 'en', columnModifiers, }) {
|
|
14
|
-
const [
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
const [selectedTemplateId, setSelectedTemplateId] = useState(undefined);
|
|
15
|
+
const defaultTemplate = useMemo(() => { var _a; return (_a = templates.find((t) => t.default)) !== null && _a !== void 0 ? _a : templates[0]; }, [templates]);
|
|
16
|
+
const createRequiredColumn = useCallback((code, sortOrder) => {
|
|
17
|
+
const label = code === 'date' ? 'Date' : 'Index';
|
|
18
|
+
return {
|
|
19
|
+
code,
|
|
20
|
+
name: [{ text: label, lang: 'en' }],
|
|
21
|
+
selected: true,
|
|
22
|
+
sort_order: sortOrder,
|
|
23
|
+
fields: [
|
|
24
|
+
{
|
|
25
|
+
code,
|
|
26
|
+
type: 'single',
|
|
27
|
+
parameters: [{ parameter: code, code, selected: true }],
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
}, []);
|
|
32
|
+
const normalizeMandatoryColumns = useCallback((columns, mode) => {
|
|
33
|
+
var _a, _b, _c, _d, _e;
|
|
34
|
+
const normalized = [...columns];
|
|
35
|
+
const findColumnIndex = (code) => normalized.findIndex((column) => {
|
|
36
|
+
var _a, _b, _c, _d;
|
|
37
|
+
const columnCode = (_a = column.code) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
38
|
+
const columnName = (_d = (_c = (_b = column.name) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.text) === null || _d === void 0 ? void 0 : _d.toLowerCase();
|
|
39
|
+
return columnCode === code || columnName === code;
|
|
40
|
+
});
|
|
41
|
+
const dateIdx = findColumnIndex('date');
|
|
42
|
+
if (dateIdx >= 0) {
|
|
43
|
+
const currentDate = normalized[dateIdx];
|
|
44
|
+
normalized[dateIdx] = Object.assign(Object.assign({}, currentDate), { code: (_a = currentDate.code) !== null && _a !== void 0 ? _a : 'date', selected: true, fields: ((_b = currentDate.fields) === null || _b === void 0 ? void 0 : _b.length) > 0
|
|
45
|
+
? currentDate.fields.map((field) => {
|
|
46
|
+
var _a;
|
|
47
|
+
return (Object.assign(Object.assign({}, field), { parameters: ((_a = field.parameters) === null || _a === void 0 ? void 0 : _a.length) > 0
|
|
48
|
+
? field.parameters.map((parameter) => (Object.assign(Object.assign({}, parameter), { selected: true })))
|
|
49
|
+
: [{ parameter: 'date', code: 'date', selected: true }] }));
|
|
50
|
+
})
|
|
51
|
+
: createRequiredColumn('date', (_c = currentDate.sort_order) !== null && _c !== void 0 ? _c : 1).fields });
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
normalized.push(createRequiredColumn('date', 1));
|
|
55
|
+
}
|
|
56
|
+
if (mode === 'sheet') {
|
|
57
|
+
const indexIdx = findColumnIndex('index');
|
|
58
|
+
if (indexIdx >= 0) {
|
|
59
|
+
const currentIndex = normalized[indexIdx];
|
|
60
|
+
normalized[indexIdx] = Object.assign(Object.assign({}, currentIndex), { code: (_d = currentIndex.code) !== null && _d !== void 0 ? _d : 'index', selected: true, fields: ((_e = currentIndex.fields) === null || _e === void 0 ? void 0 : _e.length) > 0
|
|
61
|
+
? currentIndex.fields.map((field) => {
|
|
62
|
+
var _a;
|
|
63
|
+
return (Object.assign(Object.assign({}, field), { parameters: ((_a = field.parameters) === null || _a === void 0 ? void 0 : _a.length) > 0
|
|
64
|
+
? field.parameters.map((parameter) => (Object.assign(Object.assign({}, parameter), { selected: true })))
|
|
65
|
+
: [{ parameter: 'index', code: 'index', selected: true }] }));
|
|
66
|
+
})
|
|
67
|
+
: createRequiredColumn('index', 0).fields });
|
|
21
68
|
}
|
|
22
69
|
else {
|
|
23
|
-
|
|
24
|
-
const defaultTemplate = templates.find((t) => t.default);
|
|
25
|
-
if (defaultTemplate) {
|
|
26
|
-
setSelectedView(templateToViewMenuItem(defaultTemplate, tableMode));
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
setSelectedView(undefined);
|
|
30
|
-
}
|
|
70
|
+
normalized.push(createRequiredColumn('index', 0));
|
|
31
71
|
}
|
|
32
72
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
73
|
+
const indexColumn = normalized.find((column) => { var _a, _b, _c, _d; return ((_d = ((_a = column.code) !== null && _a !== void 0 ? _a : (_c = (_b = column.name) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.text)) === null || _d === void 0 ? void 0 : _d.toLowerCase()) === 'index'; });
|
|
74
|
+
const dateColumn = normalized.find((column) => { var _a, _b, _c, _d; return ((_d = ((_a = column.code) !== null && _a !== void 0 ? _a : (_c = (_b = column.name) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.text)) === null || _d === void 0 ? void 0 : _d.toLowerCase()) === 'date'; });
|
|
75
|
+
const restColumns = normalized.filter((column) => {
|
|
76
|
+
var _a, _b, _c, _d;
|
|
77
|
+
const key = (_d = ((_a = column.code) !== null && _a !== void 0 ? _a : (_c = (_b = column.name) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.text)) === null || _d === void 0 ? void 0 : _d.toLowerCase();
|
|
78
|
+
return key !== 'index' && key !== 'date';
|
|
79
|
+
});
|
|
80
|
+
const orderedColumns = mode === 'sheet' ? [indexColumn, dateColumn, ...restColumns] : [dateColumn, ...restColumns];
|
|
81
|
+
return orderedColumns.filter((column) => Boolean(column));
|
|
82
|
+
}, [createRequiredColumn]);
|
|
83
|
+
const alignCreatedTemplateToCurrentMode = useCallback((createdTemplate) => {
|
|
84
|
+
var _a, _b, _c, _d;
|
|
85
|
+
const normalized = handleTemplateUpdate(createdTemplate);
|
|
86
|
+
const selectedModeCode = tableMode === 'sheet' ? 'sheet' : 'advanced';
|
|
87
|
+
const otherModeCode = tableMode === 'sheet' ? 'advanced' : 'sheet';
|
|
88
|
+
const selectedModeLayout = (_a = normalized.layout) === null || _a === void 0 ? void 0 : _a.find((section) => {
|
|
89
|
+
var _a;
|
|
90
|
+
const code = (_a = section.code) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
91
|
+
if (selectedModeCode === 'advanced')
|
|
92
|
+
return code === 'advanced' || code === 'default';
|
|
93
|
+
return code === 'sheet' || code === 'sheets';
|
|
94
|
+
});
|
|
95
|
+
const defaultOtherModeLayout = (_b = defaultTemplate === null || defaultTemplate === void 0 ? void 0 : defaultTemplate.layout) === null || _b === void 0 ? void 0 : _b.find((section) => {
|
|
96
|
+
var _a;
|
|
97
|
+
const code = (_a = section.code) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
98
|
+
if (otherModeCode === 'advanced')
|
|
99
|
+
return code === 'advanced' || code === 'default';
|
|
100
|
+
return code === 'sheet' || code === 'sheets';
|
|
101
|
+
});
|
|
102
|
+
const nextLayout = [];
|
|
103
|
+
if (selectedModeLayout) {
|
|
104
|
+
nextLayout.push(Object.assign(Object.assign({}, selectedModeLayout), { columns: [...((_c = selectedModeLayout.columns) !== null && _c !== void 0 ? _c : [])] }));
|
|
105
|
+
}
|
|
106
|
+
if (defaultOtherModeLayout) {
|
|
107
|
+
nextLayout.push(Object.assign(Object.assign({}, defaultOtherModeLayout), { columns: [...((_d = defaultOtherModeLayout.columns) !== null && _d !== void 0 ? _d : [])] }));
|
|
108
|
+
}
|
|
109
|
+
return nextLayout.length > 0 ? Object.assign(Object.assign({}, normalized), { layout: nextLayout }) : normalized;
|
|
110
|
+
}, [tableMode, defaultTemplate]);
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
if (!defaultTemplate) {
|
|
113
|
+
setSelectedTemplateId(undefined);
|
|
114
|
+
return;
|
|
38
115
|
}
|
|
39
|
-
|
|
116
|
+
if (!selectedTemplateId) {
|
|
117
|
+
setSelectedTemplateId(defaultTemplate.id);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const isSelectedTemplateExists = templates.some((t) => isTemplateMatchingId(t, selectedTemplateId));
|
|
121
|
+
if (!isSelectedTemplateExists) {
|
|
122
|
+
setSelectedTemplateId(defaultTemplate.id);
|
|
123
|
+
}
|
|
124
|
+
}, [templates, defaultTemplate, selectedTemplateId]);
|
|
40
125
|
const onCreateCustomView = useCallback(({ name, layout }) => __awaiter(this, void 0, void 0, function* () {
|
|
41
126
|
var _a;
|
|
42
127
|
const created = yield createTemplate({ name, layout: [layout] });
|
|
43
128
|
if (created) {
|
|
44
|
-
const normalized =
|
|
129
|
+
const normalized = alignCreatedTemplateToCurrentMode(created);
|
|
45
130
|
setTemplates((prev) => [...prev, normalized]);
|
|
46
|
-
|
|
131
|
+
setSelectedTemplateId(normalized.id);
|
|
47
132
|
return { id: created.id, name: (_a = created.name) !== null && _a !== void 0 ? _a : '' };
|
|
48
133
|
}
|
|
49
|
-
}), [createTemplate, setTemplates,
|
|
134
|
+
}), [createTemplate, setTemplates, alignCreatedTemplateToCurrentMode]);
|
|
50
135
|
const onDeleteCustomView = useCallback((templateId) => __awaiter(this, void 0, void 0, function* () {
|
|
51
136
|
yield deleteTemplate(templateId);
|
|
52
137
|
setTemplates((prev) => prev.filter((t) => !isTemplateMatchingId(t, templateId)));
|
|
53
|
-
|
|
54
|
-
}), [deleteTemplate, setTemplates]);
|
|
138
|
+
setSelectedTemplateId((current) => (current === templateId ? defaultTemplate === null || defaultTemplate === void 0 ? void 0 : defaultTemplate.id : current));
|
|
139
|
+
}), [deleteTemplate, setTemplates, defaultTemplate]);
|
|
55
140
|
const onUpdateDefaultView = useCallback((columns) => {
|
|
56
141
|
const defaultTemplate = templates.find((t) => t.default);
|
|
57
142
|
if (!defaultTemplate)
|
|
@@ -64,13 +149,19 @@ export function useCustomTableViews({ templates, setTemplates, createTemplate, u
|
|
|
64
149
|
const onEditCustomView = useCallback((viewId, data) => __awaiter(this, void 0, void 0, function* () {
|
|
65
150
|
const template = templates.find((t) => isTemplateMatchingId(t, viewId));
|
|
66
151
|
const { name, layout } = updateCurrentTemplate(template, data);
|
|
152
|
+
const optimisticTemplate = template ? Object.assign(Object.assign({}, template), { name, layout }) : undefined;
|
|
153
|
+
if (optimisticTemplate) {
|
|
154
|
+
const optimisticNormalized = handleTemplateUpdate(optimisticTemplate);
|
|
155
|
+
setTemplates((prev) => replaceTemplateInList(prev, (t) => isTemplateMatchingId(t, viewId), optimisticNormalized));
|
|
156
|
+
}
|
|
67
157
|
const updated = yield updateTemplate(viewId, { name, layout });
|
|
68
|
-
|
|
69
|
-
|
|
158
|
+
const candidateTemplate = updated !== null && updated !== void 0 ? updated : optimisticTemplate;
|
|
159
|
+
if (candidateTemplate) {
|
|
160
|
+
const normalized = handleTemplateUpdate(candidateTemplate);
|
|
70
161
|
setTemplates((prev) => replaceTemplateInList(prev, (t) => isTemplateMatchingId(t, viewId), normalized));
|
|
71
|
-
|
|
162
|
+
setSelectedTemplateId(normalized.id);
|
|
72
163
|
}
|
|
73
|
-
}), [templates,
|
|
164
|
+
}), [templates, updateTemplate, setTemplates]);
|
|
74
165
|
const customViews = useMemo(() => ({
|
|
75
166
|
templates,
|
|
76
167
|
onCreateCustomView,
|
|
@@ -79,56 +170,32 @@ export function useCustomTableViews({ templates, setTemplates, createTemplate, u
|
|
|
79
170
|
onUpdateDefaultView,
|
|
80
171
|
}), [templates, onCreateCustomView, onEditCustomView, onDeleteCustomView, onUpdateDefaultView]);
|
|
81
172
|
const onViewChange = useCallback((view) => {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
name: menuItem.name,
|
|
95
|
-
selected: menuItem.selected,
|
|
96
|
-
})),
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
const columnsSignature = (_b = selectedView.columns) !== null && _b !== void 0 ? _b : [];
|
|
100
|
-
const viewWithLayout = selectedView;
|
|
101
|
-
const layoutSignature = ((_c = viewWithLayout.layout) !== null && _c !== void 0 ? _c : []).map((layout) => {
|
|
102
|
-
var _a;
|
|
103
|
-
return ({
|
|
104
|
-
code: layout.code,
|
|
105
|
-
columns: ((_a = layout.columns) !== null && _a !== void 0 ? _a : []).map((col) => col.code),
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
return JSON.stringify({
|
|
109
|
-
id: selectedView.id,
|
|
110
|
-
columns: columnsSignature,
|
|
111
|
-
submenu: submenuSignature,
|
|
112
|
-
layout: layoutSignature,
|
|
113
|
-
});
|
|
114
|
-
}, [selectedView]);
|
|
173
|
+
var _a;
|
|
174
|
+
if (!defaultTemplate) {
|
|
175
|
+
setSelectedTemplateId(undefined);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (!(view === null || view === void 0 ? void 0 : view.id) || view.id === 'default') {
|
|
179
|
+
setSelectedTemplateId(defaultTemplate.id);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const matchingTemplate = templates.find((t) => isTemplateMatchingId(t, view.id));
|
|
183
|
+
setSelectedTemplateId((_a = matchingTemplate === null || matchingTemplate === void 0 ? void 0 : matchingTemplate.id) !== null && _a !== void 0 ? _a : defaultTemplate.id);
|
|
184
|
+
}, [templates, defaultTemplate]);
|
|
115
185
|
const columnsViewData = useMemo(() => {
|
|
116
|
-
|
|
186
|
+
var _a;
|
|
187
|
+
const selectedTemplate = (_a = templates.find((t) => (selectedTemplateId ? isTemplateMatchingId(t, selectedTemplateId) : false))) !== null && _a !== void 0 ? _a : defaultTemplate;
|
|
117
188
|
let templateColumnsView = [];
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
const hasLayout = !!viewWithLayout.layout;
|
|
121
|
-
templateColumnsView = convertTemplateToColumnsView(viewWithLayout, tableMode);
|
|
122
|
-
if (templateColumnsView.length === 0 && !hasLayout && (templates === null || templates === void 0 ? void 0 : templates.length) > 0) {
|
|
123
|
-
templateColumnsView = convertTemplatesToColumnsView(templates, tableMode);
|
|
124
|
-
}
|
|
189
|
+
if (selectedTemplate) {
|
|
190
|
+
templateColumnsView = convertTemplateToColumnsView(selectedTemplate, tableMode);
|
|
125
191
|
}
|
|
126
|
-
if (templateColumnsView.length === 0 &&
|
|
192
|
+
if (templateColumnsView.length === 0 && (templates === null || templates === void 0 ? void 0 : templates.length) > 0) {
|
|
127
193
|
templateColumnsView = convertTemplatesToColumnsView(templates, tableMode);
|
|
128
194
|
}
|
|
129
195
|
if (templateColumnsView.length === 0) {
|
|
130
196
|
return [];
|
|
131
197
|
}
|
|
198
|
+
templateColumnsView = normalizeMandatoryColumns(templateColumnsView, tableMode);
|
|
132
199
|
if (columnModifiers === null || columnModifiers === void 0 ? void 0 : columnModifiers.length) {
|
|
133
200
|
let result = templateColumnsView;
|
|
134
201
|
for (const mod of columnModifiers) {
|
|
@@ -141,11 +208,11 @@ export function useCustomTableViews({ templates, setTemplates, createTemplate, u
|
|
|
141
208
|
return result;
|
|
142
209
|
}
|
|
143
210
|
return templateColumnsView;
|
|
144
|
-
}, [tableMode, templates,
|
|
211
|
+
}, [tableMode, templates, selectedTemplateId, defaultTemplate, columnModifiers, normalizeMandatoryColumns]);
|
|
145
212
|
return {
|
|
146
213
|
customViews,
|
|
147
214
|
onViewChange,
|
|
148
215
|
columnsViewData,
|
|
149
|
-
selectedViewId:
|
|
216
|
+
selectedViewId: selectedTemplateId,
|
|
150
217
|
};
|
|
151
218
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tap-payments/os-micro-frontend-shared",
|
|
3
3
|
"description": "Shared components and utilities for Tap Payments micro frontends",
|
|
4
|
-
"version": "0.1.438-test.
|
|
5
|
-
"testVersion":
|
|
4
|
+
"version": "0.1.438-test.4",
|
|
5
|
+
"testVersion": 4,
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "build/index.js",
|
|
8
8
|
"module": "build/index.js",
|