@omniumretail/component-library 1.0.70 → 1.0.72
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/.storybook/main.js +16 -16
- package/.storybook/preview-head.html +20 -20
- package/.storybook/preview.js +27 -27
- package/README.md +54 -54
- package/bitbucket-pipelines.yml +94 -94
- package/dist/bundle.js +246 -290
- package/dist/types/components/AnalyticsBar/AnalyticsBar.stories.d.ts +5 -5
- package/dist/types/components/AnalyticsBar/helpers/codeMutation.d.ts +4 -4
- package/dist/types/components/AnalyticsBar/index.d.ts +2 -2
- package/dist/types/components/AnalyticsBar/interfaces/analyticsBar.d.ts +12 -12
- package/dist/types/components/Button/Button.stories.d.ts +6 -6
- package/dist/types/components/Button/index.d.ts +7 -7
- package/dist/types/components/Category/Category.stories.d.ts +4 -4
- package/dist/types/components/Category/CategoryContent/index.d.ts +7 -7
- package/dist/types/components/Category/CategorySidebar/index.d.ts +26 -26
- package/dist/types/components/Category/index.d.ts +7 -7
- package/dist/types/components/CategoryReadOnly/CategoryReadOnly.stories.d.ts +4 -4
- package/dist/types/components/CategoryReadOnly/evaluationOptions.d.ts +10 -10
- package/dist/types/components/CategoryReadOnly/index.d.ts +9 -9
- package/dist/types/components/CategoryResponse/CategoryResponse.stories.d.ts +4 -4
- package/dist/types/components/CategoryResponse/evaluationOptions.d.ts +10 -10
- package/dist/types/components/CategoryResponse/index.d.ts +9 -9
- package/dist/types/components/DatePicker/DatePicker.stories.d.ts +5 -5
- package/dist/types/components/DatePicker/index.d.ts +8 -8
- package/dist/types/components/DatePickerTag/DatePickerTag.stories.d.ts +5 -5
- package/dist/types/components/DatePickerTag/index.d.ts +6 -6
- package/dist/types/components/Footer/Footer.stories.d.ts +5 -5
- package/dist/types/components/Footer/index.d.ts +4 -4
- package/dist/types/components/Input/Input.stories.d.ts +5 -5
- package/dist/types/components/Input/index.d.ts +10 -10
- package/dist/types/components/InputCountryCode/index.d.ts +10 -10
- package/dist/types/components/InputCountryCode/inputCountryCode.stories.d.ts +5 -5
- package/dist/types/components/Label/Label.stories.d.ts +6 -6
- package/dist/types/components/Label/index.d.ts +7 -7
- package/dist/types/components/Link/Link.stories.d.ts +7 -7
- package/dist/types/components/Link/index.d.ts +7 -7
- package/dist/types/components/Menu/Menu.stories.d.ts +5 -5
- package/dist/types/components/Menu/helpers/codeMutation.d.ts +4 -4
- package/dist/types/components/Menu/index.d.ts +3 -2
- package/dist/types/components/ModalConfirmation/ModalConfirmation.stories.d.ts +5 -5
- package/dist/types/components/ModalConfirmation/ModalStatusList.d.ts +5 -5
- package/dist/types/components/ModalConfirmation/index.d.ts +9 -9
- package/dist/types/components/ModalWithTable/ModalWithTable.stories.d.ts +5 -5
- package/dist/types/components/ModalWithTable/index.d.ts +13 -13
- package/dist/types/components/Navigation/Navigation.stories.d.ts +5 -5
- package/dist/types/components/Navigation/index.d.ts +10 -10
- package/dist/types/components/Notification/Notification.stories.d.ts +5 -5
- package/dist/types/components/Notification/index.d.ts +9 -9
- package/dist/types/components/Questions/Questions.stories.d.ts +4 -4
- package/dist/types/components/Questions/SingleQuestion/index.d.ts +1 -1
- package/dist/types/components/Questions/index.d.ts +5 -5
- package/dist/types/components/Radio/Radio.stories.d.ts +5 -5
- package/dist/types/components/Radio/index.d.ts +10 -10
- package/dist/types/components/Select/Select.stories.d.ts +6 -6
- package/dist/types/components/Select/index.d.ts +5 -5
- package/dist/types/components/Separator/Separator.stories.d.ts +6 -6
- package/dist/types/components/Separator/index.d.ts +11 -11
- package/dist/types/components/Sidebar/Sidebar.stories.d.ts +6 -6
- package/dist/types/components/Sidebar/index.d.ts +15 -15
- package/dist/types/components/Switch/Switch.stories.d.ts +5 -5
- package/dist/types/components/Switch/index.d.ts +2 -2
- package/dist/types/components/Table/Table.stories.d.ts +9 -9
- package/dist/types/components/Table/index.d.ts +37 -36
- package/dist/types/components/Tag/Tag.stories.d.ts +5 -5
- package/dist/types/components/Tag/index.d.ts +8 -8
- package/dist/types/components/Upload/Upload.stories.d.ts +4 -4
- package/dist/types/components/Upload/index.d.ts +8 -8
- package/dist/types/components/UserInfo/UserInfo.stories.d.ts +4 -4
- package/dist/types/components/UserInfo/index.d.ts +8 -8
- package/dist/types/components/index.d.ts +25 -25
- package/dist/types/constants/i18n.d.ts +1 -1
- package/dist/types/constants/translationHelper.d.ts +2 -2
- package/dist/types/index.d.ts +2 -2
- package/package.json +198 -198
- package/src/assets/scss/_global.scss +89 -89
- package/src/assets/scss/index.scss +2 -2
- package/src/components/AnalyticsBar/AnalyticsBar.stories.tsx +236 -236
- package/src/components/AnalyticsBar/helpers/codeMutation.tsx +19 -19
- package/src/components/AnalyticsBar/index.tsx +76 -76
- package/src/components/AnalyticsBar/interfaces/analyticsBar.tsx +13 -13
- package/src/components/AnalyticsBar/styles.module.scss +108 -108
- package/src/components/Button/Button.stories.tsx +26 -26
- package/src/components/Button/index.tsx +24 -24
- package/src/components/Button/styles.module.scss +65 -65
- package/src/components/Category/Category.stories.tsx +88 -88
- package/src/components/Category/CategoryContent/index.tsx +188 -188
- package/src/components/Category/CategoryContent/styles.module.scss +51 -51
- package/src/components/Category/CategorySidebar/index.tsx +268 -268
- package/src/components/Category/CategorySidebar/styles.module.scss +28 -28
- package/src/components/Category/index.tsx +76 -76
- package/src/components/Category/styles.module.scss +13 -13
- package/src/components/CategoryReadOnly/CategoryReadOnly.stories.tsx +270 -270
- package/src/components/CategoryReadOnly/evaluationOptions.tsx +81 -81
- package/src/components/CategoryReadOnly/index.tsx +254 -254
- package/src/components/CategoryReadOnly/styles.module.scss +184 -184
- package/src/components/CategoryResponse/CategoryResponse.stories.tsx +251 -251
- package/src/components/CategoryResponse/evaluationOptions.tsx +81 -81
- package/src/components/CategoryResponse/index.tsx +277 -277
- package/src/components/CategoryResponse/styles.module.scss +180 -180
- package/src/components/DatePicker/DatePicker.stories.tsx +16 -16
- package/src/components/DatePicker/index.tsx +38 -38
- package/src/components/DatePicker/styles.module.scss +3 -3
- package/src/components/DatePickerTag/DatePickerTag.stories.tsx +19 -19
- package/src/components/DatePickerTag/index.tsx +89 -89
- package/src/components/DatePickerTag/styles.module.scss +31 -31
- package/src/components/Footer/Footer.stories.tsx +14 -14
- package/src/components/Footer/index.tsx +38 -38
- package/src/components/Footer/styles.module.scss +38 -38
- package/src/components/Input/Input.stories.tsx +13 -13
- package/src/components/Input/index.tsx +31 -31
- package/src/components/Input/styles.module.scss +8 -8
- package/src/components/InputCountryCode/index.tsx +75 -75
- package/src/components/InputCountryCode/inputCountryCode.stories.tsx +55 -55
- package/src/components/InputCountryCode/styles.module.scss +3 -3
- package/src/components/Label/Label.stories.tsx +21 -21
- package/src/components/Label/index.tsx +19 -19
- package/src/components/Label/styles.module.scss +16 -16
- package/src/components/Link/Link.stories.tsx +30 -30
- package/src/components/Link/index.tsx +21 -21
- package/src/components/Link/styles.module.scss +24 -24
- package/src/components/Menu/Menu.stories.tsx +178 -178
- package/src/components/Menu/helpers/codeMutation.tsx +19 -19
- package/src/components/Menu/index.tsx +23 -23
- package/src/components/ModalConfirmation/ModalConfirmation.stories.tsx +40 -40
- package/src/components/ModalConfirmation/ModalStatusList.tsx +5 -5
- package/src/components/ModalConfirmation/index.tsx +70 -70
- package/src/components/ModalConfirmation/styles.module.scss +62 -62
- package/src/components/ModalWithTable/ModalWithTable.stories.tsx +96 -96
- package/src/components/ModalWithTable/index.tsx +232 -232
- package/src/components/ModalWithTable/styles.module.scss +77 -77
- package/src/components/Navigation/Navigation.stories.tsx +17 -17
- package/src/components/Navigation/index.tsx +33 -33
- package/src/components/Navigation/styles.module.scss +58 -58
- package/src/components/Notification/Notification.stories.tsx +29 -29
- package/src/components/Notification/index.tsx +21 -21
- package/src/components/Questions/Questions.stories.tsx +37 -37
- package/src/components/Questions/SingleQuestion/index.tsx +84 -84
- package/src/components/Questions/SingleQuestion/styles.module.scss +81 -81
- package/src/components/Questions/index.tsx +78 -78
- package/src/components/Radio/Radio.stories.tsx +43 -43
- package/src/components/Radio/index.tsx +26 -26
- package/src/components/Radio/styles.module.scss +23 -23
- package/src/components/Select/Select.stories.tsx +39 -39
- package/src/components/Select/index.tsx +27 -27
- package/src/components/Select/styles.module.scss +13 -13
- package/src/components/Separator/Separator.stories.tsx +22 -22
- package/src/components/Separator/index.tsx +27 -27
- package/src/components/Separator/styles.module.scss +25 -25
- package/src/components/Sidebar/Sidebar.stories.tsx +85 -85
- package/src/components/Sidebar/index.tsx +103 -103
- package/src/components/Sidebar/styles.module.scss +85 -85
- package/src/components/Switch/Switch.stories.tsx +14 -14
- package/src/components/Switch/index.tsx +8 -8
- package/src/components/Switch/styles.module.scss +7 -7
- package/src/components/Table/Table.stories.tsx +222 -221
- package/src/components/Table/index.tsx +275 -271
- package/src/components/Table/styles.module.scss +75 -75
- package/src/components/Tag/Tag.stories.tsx +22 -22
- package/src/components/Tag/index.tsx +189 -189
- package/src/components/Tag/styles.module.scss +60 -60
- package/src/components/Upload/Upload.stories.tsx +45 -45
- package/src/components/Upload/index.tsx +91 -91
- package/src/components/UserInfo/UserInfo.stories.tsx +37 -37
- package/src/components/UserInfo/index.tsx +62 -62
- package/src/components/UserInfo/styles.module.scss +29 -29
- package/src/components/index.tsx +25 -25
- package/src/constants/i18n.ts +25 -25
- package/src/constants/translationHelper.ts +7 -7
- package/src/index.ts +2 -2
- package/src/locales/en.json +86 -86
- package/src/locales/es.json +86 -86
- package/src/locales/pt.json +86 -86
- package/src/types/Global.d.ts +4 -4
- package/tsconfig.json +29 -29
- package/webpack.config.js +51 -51
|
@@ -1,268 +1,268 @@
|
|
|
1
|
-
import React, { useEffect, useState } from 'react';
|
|
2
|
-
import { Tree } from 'antd';
|
|
3
|
-
import type { TreeProps } from 'antd/es/tree';
|
|
4
|
-
import styles from './styles.module.scss';
|
|
5
|
-
import { MinusOutlined, PlusOutlined } from '@ant-design/icons';
|
|
6
|
-
import { Button } from '../../Button';
|
|
7
|
-
import { t } from 'i18next';
|
|
8
|
-
|
|
9
|
-
export interface Questions {
|
|
10
|
-
grade: string;
|
|
11
|
-
questions: string;
|
|
12
|
-
info: string;
|
|
13
|
-
}
|
|
14
|
-
export interface Data {
|
|
15
|
-
categoryName: string;
|
|
16
|
-
generalEvaluationLevel: string;
|
|
17
|
-
grade: string;
|
|
18
|
-
openAnswer: boolean;
|
|
19
|
-
questions: Questions[];
|
|
20
|
-
}
|
|
21
|
-
export interface DataNode {
|
|
22
|
-
title: string;
|
|
23
|
-
key: string;
|
|
24
|
-
data?: Data;
|
|
25
|
-
children?: DataNode[];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
interface SidebarProps {
|
|
29
|
-
categoryContentData: any;
|
|
30
|
-
categorySidebarInfo: any;
|
|
31
|
-
categoryContentFinalData: any;
|
|
32
|
-
data: DataNode[];
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function get_element_by_index(data: DataNode[], index: string | number): DataNode | null {
|
|
36
|
-
for (const item of data) {
|
|
37
|
-
if (item.key === index) {
|
|
38
|
-
return item;
|
|
39
|
-
} else if (item.children) {
|
|
40
|
-
const result = get_element_by_index(item.children, index);
|
|
41
|
-
if (result !== null) {
|
|
42
|
-
return result;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function addPropertyByIndex(data: DataNode[], index: string | number, property: string, value: any): DataNode[] {
|
|
50
|
-
return data.map((item: DataNode) => {
|
|
51
|
-
if (item.key === index) {
|
|
52
|
-
return { ...item, [property]: value };
|
|
53
|
-
}
|
|
54
|
-
else if (item.children) {
|
|
55
|
-
return { ...item, children: addPropertyByIndex(item.children, index, property, value) };
|
|
56
|
-
}
|
|
57
|
-
return item;
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function remove_element_by_index(data: DataNode[], index: string | number): DataNode[] {
|
|
62
|
-
return data.filter((item) => item.key !== index);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function remove_nested_element(data: DataNode[], index: string | number): DataNode[] {
|
|
66
|
-
let newData = [...data] as DataNode[];
|
|
67
|
-
|
|
68
|
-
for (let i = 0; i < newData.length; i++) {
|
|
69
|
-
if (newData[i].key === index) {
|
|
70
|
-
newData = remove_element_by_index(newData, index);
|
|
71
|
-
break;
|
|
72
|
-
} else if (newData[i].children && (newData[i] as any).children.length > 0) {
|
|
73
|
-
newData[i].children = remove_nested_element((newData[i].children as any), index);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return newData;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export const CategorySidebar = (props: SidebarProps) => {
|
|
81
|
-
const {
|
|
82
|
-
categoryContentData,
|
|
83
|
-
categoryContentFinalData,
|
|
84
|
-
data
|
|
85
|
-
} = props;
|
|
86
|
-
|
|
87
|
-
const [gData, setGData] = useState(data);
|
|
88
|
-
const [expandedKeys] = useState([]);
|
|
89
|
-
const [sidebarInfo, setSidebarInfo] = useState<any>(undefined);
|
|
90
|
-
|
|
91
|
-
const onDragEnter: TreeProps['onDragEnter'] = (info) => {
|
|
92
|
-
// drag entered
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const onDrop: TreeProps['onDrop'] = (info) => {
|
|
96
|
-
const dropKey = info.node.key;
|
|
97
|
-
const dragKey = info.dragNode.key;
|
|
98
|
-
const dropPos = info.node.pos.split('-');
|
|
99
|
-
const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
|
|
100
|
-
const { data: nodeData } = info.node as DataNode;
|
|
101
|
-
const questions: Questions[] | undefined = nodeData?.questions;
|
|
102
|
-
|
|
103
|
-
if (questions && questions.length > 0) {
|
|
104
|
-
// console.log('you cant add a subcategory to a category with questions');
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (dropPos.length > 4) {
|
|
109
|
-
// console.log('cant have more than 3 levels');
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const loop = (
|
|
114
|
-
data: DataNode[],
|
|
115
|
-
key: React.Key,
|
|
116
|
-
callback: (node: DataNode, i: number, data: DataNode[]) => void,
|
|
117
|
-
) => {
|
|
118
|
-
for (let i = 0; i < data.length; i++) {
|
|
119
|
-
if (data[i].key === key) {
|
|
120
|
-
return callback(data[i], i, data);
|
|
121
|
-
}
|
|
122
|
-
if (data[i].children) {
|
|
123
|
-
loop(data[i].children!, key, callback);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
const data = [...gData];
|
|
128
|
-
|
|
129
|
-
// Find dragObject
|
|
130
|
-
let dragObj: DataNode;
|
|
131
|
-
loop(data, dragKey, (item, index, arr) => {
|
|
132
|
-
arr.splice(index, 1);
|
|
133
|
-
dragObj = item;
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
if (!info.dropToGap) {
|
|
137
|
-
// Drop on the content
|
|
138
|
-
loop(data, dropKey, (item) => {
|
|
139
|
-
item.children = item.children || [];
|
|
140
|
-
// where to insert
|
|
141
|
-
item.children.unshift(dragObj);
|
|
142
|
-
});
|
|
143
|
-
} else if (
|
|
144
|
-
((info.node as any).props.children || []).length > 0 && // Has children
|
|
145
|
-
(info.node as any).props.expanded && // Is expanded
|
|
146
|
-
dropPosition === 1 // On the bottom gap
|
|
147
|
-
) {
|
|
148
|
-
loop(data, dropKey, (item) => {
|
|
149
|
-
item.children = item.children || [];
|
|
150
|
-
// where to insert
|
|
151
|
-
item.children.unshift(dragObj);
|
|
152
|
-
// in previous version, we use item.children.push(dragObj) to insert the
|
|
153
|
-
// item to the tail of the children
|
|
154
|
-
});
|
|
155
|
-
} else {
|
|
156
|
-
let ar: DataNode[] = [];
|
|
157
|
-
let i: number;
|
|
158
|
-
loop(data, dropKey, (_item, index, arr) => {
|
|
159
|
-
ar = arr;
|
|
160
|
-
i = index;
|
|
161
|
-
});
|
|
162
|
-
if (dropPosition === -1) {
|
|
163
|
-
ar.splice(i!, 0, dragObj!);
|
|
164
|
-
} else {
|
|
165
|
-
ar.splice(i! + 1, 0, dragObj!);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function generateCategoryKeys(categories: any[], parentKey: string = ''): any[] {
|
|
170
|
-
let keyCounter = 0;
|
|
171
|
-
return categories.map((category: any, index: number) => {
|
|
172
|
-
const categoryKey = `${parentKey}${parentKey ? '.' : ''}${keyCounter++}`;
|
|
173
|
-
let children: any = [];
|
|
174
|
-
if (category.children) {
|
|
175
|
-
children = generateCategoryKeys(category.children, categoryKey);
|
|
176
|
-
}
|
|
177
|
-
return { ...category, key: categoryKey, children };
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
setGData(generateCategoryKeys(data));
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
const onSelect: TreeProps['onSelect'] = (selectedKeys, info) => {
|
|
185
|
-
setSidebarInfo(selectedKeys);
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
const addCategory = () => {
|
|
189
|
-
setGData((prevCategories: DataNode[]) => [
|
|
190
|
-
...prevCategories,
|
|
191
|
-
{ title: t('components.category.newCategory'), key: `${gData.length}` },
|
|
192
|
-
])
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const removeCategory = () => {
|
|
196
|
-
if (sidebarInfo) {
|
|
197
|
-
// Update the state with the new array
|
|
198
|
-
setGData(remove_nested_element(gData, sidebarInfo[0]));
|
|
199
|
-
setSidebarInfo(undefined);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
useEffect(() => {
|
|
204
|
-
props.categorySidebarInfo({
|
|
205
|
-
selectKey: sidebarInfo,
|
|
206
|
-
data: get_element_by_index(gData, sidebarInfo && sidebarInfo[0]),
|
|
207
|
-
});
|
|
208
|
-
}, [sidebarInfo]);
|
|
209
|
-
|
|
210
|
-
useEffect(() => {
|
|
211
|
-
|
|
212
|
-
const newGData = addPropertyByIndex(gData, sidebarInfo && sidebarInfo[0], "data", categoryContentData);
|
|
213
|
-
|
|
214
|
-
function updateCategoryTitles(categories: any[]): any[] {
|
|
215
|
-
return categories.map((category: any, index: number) => {
|
|
216
|
-
if (category?.data) {
|
|
217
|
-
const updatedTitle = `${category.data.categoryName} ${category.data.questions ? '(Q: ' + category.data.questions.length + ')' : ''} ${category.data.grade ? '(G: ' + category.data.grade + ')' : ''}`;
|
|
218
|
-
const updatedChildren = category.children ? updateCategoryTitles(category.children) : null;
|
|
219
|
-
return { ...category, title: updatedTitle, children: updatedChildren };
|
|
220
|
-
}
|
|
221
|
-
return category;
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const updatedGData: any[] = updateCategoryTitles(newGData);
|
|
226
|
-
|
|
227
|
-
setGData(updatedGData);
|
|
228
|
-
}, [categoryContentData]);
|
|
229
|
-
|
|
230
|
-
useEffect(() => {
|
|
231
|
-
props.categorySidebarInfo({
|
|
232
|
-
selectKey: sidebarInfo,
|
|
233
|
-
data: get_element_by_index(gData, sidebarInfo && sidebarInfo[0])
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
props.categoryContentFinalData(gData);
|
|
237
|
-
}, [gData]);
|
|
238
|
-
|
|
239
|
-
return (
|
|
240
|
-
<div className={styles.categorySidebar}>
|
|
241
|
-
<div className={styles.title}>Categorias</div>
|
|
242
|
-
<Button icon={<PlusOutlined />} onClick={addCategory}>
|
|
243
|
-
{ t('components.categorySidBar.addCategory') }
|
|
244
|
-
</Button>
|
|
245
|
-
|
|
246
|
-
{
|
|
247
|
-
sidebarInfo &&
|
|
248
|
-
<Button icon={<MinusOutlined />} onClick={removeCategory}>
|
|
249
|
-
{ t('components.categorySidBar.removeCategory') }
|
|
250
|
-
</Button>
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
<div className={styles.categoryViewer}>
|
|
255
|
-
<Tree
|
|
256
|
-
className="draggable-tree"
|
|
257
|
-
defaultExpandedKeys={expandedKeys}
|
|
258
|
-
draggable
|
|
259
|
-
blockNode
|
|
260
|
-
onDragEnter={onDragEnter}
|
|
261
|
-
onDrop={onDrop}
|
|
262
|
-
treeData={gData}
|
|
263
|
-
onSelect={onSelect}
|
|
264
|
-
/>
|
|
265
|
-
</div>
|
|
266
|
-
</div>
|
|
267
|
-
);
|
|
268
|
-
};
|
|
1
|
+
import React, { useEffect, useState } from 'react';
|
|
2
|
+
import { Tree } from 'antd';
|
|
3
|
+
import type { TreeProps } from 'antd/es/tree';
|
|
4
|
+
import styles from './styles.module.scss';
|
|
5
|
+
import { MinusOutlined, PlusOutlined } from '@ant-design/icons';
|
|
6
|
+
import { Button } from '../../Button';
|
|
7
|
+
import { t } from 'i18next';
|
|
8
|
+
|
|
9
|
+
export interface Questions {
|
|
10
|
+
grade: string;
|
|
11
|
+
questions: string;
|
|
12
|
+
info: string;
|
|
13
|
+
}
|
|
14
|
+
export interface Data {
|
|
15
|
+
categoryName: string;
|
|
16
|
+
generalEvaluationLevel: string;
|
|
17
|
+
grade: string;
|
|
18
|
+
openAnswer: boolean;
|
|
19
|
+
questions: Questions[];
|
|
20
|
+
}
|
|
21
|
+
export interface DataNode {
|
|
22
|
+
title: string;
|
|
23
|
+
key: string;
|
|
24
|
+
data?: Data;
|
|
25
|
+
children?: DataNode[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface SidebarProps {
|
|
29
|
+
categoryContentData: any;
|
|
30
|
+
categorySidebarInfo: any;
|
|
31
|
+
categoryContentFinalData: any;
|
|
32
|
+
data: DataNode[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function get_element_by_index(data: DataNode[], index: string | number): DataNode | null {
|
|
36
|
+
for (const item of data) {
|
|
37
|
+
if (item.key === index) {
|
|
38
|
+
return item;
|
|
39
|
+
} else if (item.children) {
|
|
40
|
+
const result = get_element_by_index(item.children, index);
|
|
41
|
+
if (result !== null) {
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function addPropertyByIndex(data: DataNode[], index: string | number, property: string, value: any): DataNode[] {
|
|
50
|
+
return data.map((item: DataNode) => {
|
|
51
|
+
if (item.key === index) {
|
|
52
|
+
return { ...item, [property]: value };
|
|
53
|
+
}
|
|
54
|
+
else if (item.children) {
|
|
55
|
+
return { ...item, children: addPropertyByIndex(item.children, index, property, value) };
|
|
56
|
+
}
|
|
57
|
+
return item;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function remove_element_by_index(data: DataNode[], index: string | number): DataNode[] {
|
|
62
|
+
return data.filter((item) => item.key !== index);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function remove_nested_element(data: DataNode[], index: string | number): DataNode[] {
|
|
66
|
+
let newData = [...data] as DataNode[];
|
|
67
|
+
|
|
68
|
+
for (let i = 0; i < newData.length; i++) {
|
|
69
|
+
if (newData[i].key === index) {
|
|
70
|
+
newData = remove_element_by_index(newData, index);
|
|
71
|
+
break;
|
|
72
|
+
} else if (newData[i].children && (newData[i] as any).children.length > 0) {
|
|
73
|
+
newData[i].children = remove_nested_element((newData[i].children as any), index);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return newData;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const CategorySidebar = (props: SidebarProps) => {
|
|
81
|
+
const {
|
|
82
|
+
categoryContentData,
|
|
83
|
+
categoryContentFinalData,
|
|
84
|
+
data
|
|
85
|
+
} = props;
|
|
86
|
+
|
|
87
|
+
const [gData, setGData] = useState(data);
|
|
88
|
+
const [expandedKeys] = useState([]);
|
|
89
|
+
const [sidebarInfo, setSidebarInfo] = useState<any>(undefined);
|
|
90
|
+
|
|
91
|
+
const onDragEnter: TreeProps['onDragEnter'] = (info) => {
|
|
92
|
+
// drag entered
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const onDrop: TreeProps['onDrop'] = (info) => {
|
|
96
|
+
const dropKey = info.node.key;
|
|
97
|
+
const dragKey = info.dragNode.key;
|
|
98
|
+
const dropPos = info.node.pos.split('-');
|
|
99
|
+
const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
|
|
100
|
+
const { data: nodeData } = info.node as DataNode;
|
|
101
|
+
const questions: Questions[] | undefined = nodeData?.questions;
|
|
102
|
+
|
|
103
|
+
if (questions && questions.length > 0) {
|
|
104
|
+
// console.log('you cant add a subcategory to a category with questions');
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (dropPos.length > 4) {
|
|
109
|
+
// console.log('cant have more than 3 levels');
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const loop = (
|
|
114
|
+
data: DataNode[],
|
|
115
|
+
key: React.Key,
|
|
116
|
+
callback: (node: DataNode, i: number, data: DataNode[]) => void,
|
|
117
|
+
) => {
|
|
118
|
+
for (let i = 0; i < data.length; i++) {
|
|
119
|
+
if (data[i].key === key) {
|
|
120
|
+
return callback(data[i], i, data);
|
|
121
|
+
}
|
|
122
|
+
if (data[i].children) {
|
|
123
|
+
loop(data[i].children!, key, callback);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
const data = [...gData];
|
|
128
|
+
|
|
129
|
+
// Find dragObject
|
|
130
|
+
let dragObj: DataNode;
|
|
131
|
+
loop(data, dragKey, (item, index, arr) => {
|
|
132
|
+
arr.splice(index, 1);
|
|
133
|
+
dragObj = item;
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
if (!info.dropToGap) {
|
|
137
|
+
// Drop on the content
|
|
138
|
+
loop(data, dropKey, (item) => {
|
|
139
|
+
item.children = item.children || [];
|
|
140
|
+
// where to insert
|
|
141
|
+
item.children.unshift(dragObj);
|
|
142
|
+
});
|
|
143
|
+
} else if (
|
|
144
|
+
((info.node as any).props.children || []).length > 0 && // Has children
|
|
145
|
+
(info.node as any).props.expanded && // Is expanded
|
|
146
|
+
dropPosition === 1 // On the bottom gap
|
|
147
|
+
) {
|
|
148
|
+
loop(data, dropKey, (item) => {
|
|
149
|
+
item.children = item.children || [];
|
|
150
|
+
// where to insert
|
|
151
|
+
item.children.unshift(dragObj);
|
|
152
|
+
// in previous version, we use item.children.push(dragObj) to insert the
|
|
153
|
+
// item to the tail of the children
|
|
154
|
+
});
|
|
155
|
+
} else {
|
|
156
|
+
let ar: DataNode[] = [];
|
|
157
|
+
let i: number;
|
|
158
|
+
loop(data, dropKey, (_item, index, arr) => {
|
|
159
|
+
ar = arr;
|
|
160
|
+
i = index;
|
|
161
|
+
});
|
|
162
|
+
if (dropPosition === -1) {
|
|
163
|
+
ar.splice(i!, 0, dragObj!);
|
|
164
|
+
} else {
|
|
165
|
+
ar.splice(i! + 1, 0, dragObj!);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function generateCategoryKeys(categories: any[], parentKey: string = ''): any[] {
|
|
170
|
+
let keyCounter = 0;
|
|
171
|
+
return categories.map((category: any, index: number) => {
|
|
172
|
+
const categoryKey = `${parentKey}${parentKey ? '.' : ''}${keyCounter++}`;
|
|
173
|
+
let children: any = [];
|
|
174
|
+
if (category.children) {
|
|
175
|
+
children = generateCategoryKeys(category.children, categoryKey);
|
|
176
|
+
}
|
|
177
|
+
return { ...category, key: categoryKey, children };
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
setGData(generateCategoryKeys(data));
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const onSelect: TreeProps['onSelect'] = (selectedKeys, info) => {
|
|
185
|
+
setSidebarInfo(selectedKeys);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const addCategory = () => {
|
|
189
|
+
setGData((prevCategories: DataNode[]) => [
|
|
190
|
+
...prevCategories,
|
|
191
|
+
{ title: t('components.category.newCategory'), key: `${gData.length}` },
|
|
192
|
+
])
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const removeCategory = () => {
|
|
196
|
+
if (sidebarInfo) {
|
|
197
|
+
// Update the state with the new array
|
|
198
|
+
setGData(remove_nested_element(gData, sidebarInfo[0]));
|
|
199
|
+
setSidebarInfo(undefined);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
useEffect(() => {
|
|
204
|
+
props.categorySidebarInfo({
|
|
205
|
+
selectKey: sidebarInfo,
|
|
206
|
+
data: get_element_by_index(gData, sidebarInfo && sidebarInfo[0]),
|
|
207
|
+
});
|
|
208
|
+
}, [sidebarInfo]);
|
|
209
|
+
|
|
210
|
+
useEffect(() => {
|
|
211
|
+
|
|
212
|
+
const newGData = addPropertyByIndex(gData, sidebarInfo && sidebarInfo[0], "data", categoryContentData);
|
|
213
|
+
|
|
214
|
+
function updateCategoryTitles(categories: any[]): any[] {
|
|
215
|
+
return categories.map((category: any, index: number) => {
|
|
216
|
+
if (category?.data) {
|
|
217
|
+
const updatedTitle = `${category.data.categoryName} ${category.data.questions ? '(Q: ' + category.data.questions.length + ')' : ''} ${category.data.grade ? '(G: ' + category.data.grade + ')' : ''}`;
|
|
218
|
+
const updatedChildren = category.children ? updateCategoryTitles(category.children) : null;
|
|
219
|
+
return { ...category, title: updatedTitle, children: updatedChildren };
|
|
220
|
+
}
|
|
221
|
+
return category;
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const updatedGData: any[] = updateCategoryTitles(newGData);
|
|
226
|
+
|
|
227
|
+
setGData(updatedGData);
|
|
228
|
+
}, [categoryContentData]);
|
|
229
|
+
|
|
230
|
+
useEffect(() => {
|
|
231
|
+
props.categorySidebarInfo({
|
|
232
|
+
selectKey: sidebarInfo,
|
|
233
|
+
data: get_element_by_index(gData, sidebarInfo && sidebarInfo[0])
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
props.categoryContentFinalData(gData);
|
|
237
|
+
}, [gData]);
|
|
238
|
+
|
|
239
|
+
return (
|
|
240
|
+
<div className={styles.categorySidebar}>
|
|
241
|
+
<div className={styles.title}>Categorias</div>
|
|
242
|
+
<Button icon={<PlusOutlined />} onClick={addCategory}>
|
|
243
|
+
{ t('components.categorySidBar.addCategory') }
|
|
244
|
+
</Button>
|
|
245
|
+
|
|
246
|
+
{
|
|
247
|
+
sidebarInfo &&
|
|
248
|
+
<Button icon={<MinusOutlined />} onClick={removeCategory}>
|
|
249
|
+
{ t('components.categorySidBar.removeCategory') }
|
|
250
|
+
</Button>
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
<div className={styles.categoryViewer}>
|
|
255
|
+
<Tree
|
|
256
|
+
className="draggable-tree"
|
|
257
|
+
defaultExpandedKeys={expandedKeys}
|
|
258
|
+
draggable
|
|
259
|
+
blockNode
|
|
260
|
+
onDragEnter={onDragEnter}
|
|
261
|
+
onDrop={onDrop}
|
|
262
|
+
treeData={gData}
|
|
263
|
+
onSelect={onSelect}
|
|
264
|
+
/>
|
|
265
|
+
</div>
|
|
266
|
+
</div>
|
|
267
|
+
);
|
|
268
|
+
};
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
.categorySidebar {
|
|
2
|
-
:global {
|
|
3
|
-
.ant-tree-title {
|
|
4
|
-
font-weight: var(--font-weight-semibold);
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
button {
|
|
9
|
-
margin-top: 32px;
|
|
10
|
-
width: 100%;
|
|
11
|
-
|
|
12
|
-
&:nth-child(3) {
|
|
13
|
-
margin-top: 12px;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
.title {
|
|
19
|
-
font-size: var(--font-size-body-4);
|
|
20
|
-
font-weight: 600;
|
|
21
|
-
line-height: 100%;
|
|
22
|
-
color: var(--color-blue);
|
|
23
|
-
text-transform: uppercase;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
.categoryViewer {
|
|
27
|
-
margin-top: 20px;
|
|
28
|
-
}
|
|
1
|
+
.categorySidebar {
|
|
2
|
+
:global {
|
|
3
|
+
.ant-tree-title {
|
|
4
|
+
font-weight: var(--font-weight-semibold);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
button {
|
|
9
|
+
margin-top: 32px;
|
|
10
|
+
width: 100%;
|
|
11
|
+
|
|
12
|
+
&:nth-child(3) {
|
|
13
|
+
margin-top: 12px;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.title {
|
|
19
|
+
font-size: var(--font-size-body-4);
|
|
20
|
+
font-weight: 600;
|
|
21
|
+
line-height: 100%;
|
|
22
|
+
color: var(--color-blue);
|
|
23
|
+
text-transform: uppercase;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.categoryViewer {
|
|
27
|
+
margin-top: 20px;
|
|
28
|
+
}
|