@omniumretail/component-library 1.0.49 → 1.0.51

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.
@@ -0,0 +1,270 @@
1
+ import { Meta, Story } from "@storybook/react";
2
+ import { Button } from "components/Button";
3
+ import { useEffect, useRef, useState } from "react";
4
+ import { CategoryReadOnly } from '.';
5
+
6
+ export default {
7
+ title: 'CategoryReadOnly',
8
+ component: CategoryReadOnly,
9
+ } as Meta;
10
+
11
+ const Template: Story<any> = (args) => {
12
+ const [serverData, setServerData] = useState<any>();
13
+ const categoryResponseRef = useRef(null);
14
+ const [hasNext, setHasNext] = useState(true);
15
+ const [hasPrevious, setHasPrevious] = useState(false);
16
+
17
+ const handleNextCategoryAvailabilityChange = (hasNext: boolean) => {
18
+ setHasNext(hasNext);
19
+ };
20
+
21
+ const handlePreviousCategoryAvailabilityChange = (hasPrevious: boolean) => {
22
+ setHasPrevious(hasPrevious);
23
+ };
24
+
25
+ const handleNextClickOutside = () => {
26
+ (categoryResponseRef.current as any).handleNextClick();
27
+ }
28
+
29
+ const handlePreviousClickOutside = () => {
30
+ (categoryResponseRef.current as any).handlePreviousClick();
31
+ }
32
+
33
+ useEffect(() => {
34
+ console.log(serverData);
35
+ }, [serverData])
36
+
37
+ return (
38
+ <div style={{ height: '600px' }}>
39
+ <div>
40
+ <CategoryReadOnly
41
+ {...args}
42
+ serverReadyData={setServerData}
43
+ ref={categoryResponseRef}
44
+ onNextCategoryAvailabilityChange={handleNextCategoryAvailabilityChange}
45
+ onPreviousCategoryAvailabilityChange={handlePreviousCategoryAvailabilityChange}
46
+ >
47
+ </CategoryReadOnly>
48
+ </div>
49
+ <div>
50
+
51
+ {hasPrevious && (
52
+ <Button onClick={handlePreviousClickOutside}>Previous on Parent</Button>
53
+ )}
54
+
55
+ {hasNext && (
56
+ <Button onClick={handleNextClickOutside}>Next on Parent</Button>
57
+ )}
58
+ </div>
59
+ </div>
60
+ )
61
+ };
62
+
63
+ export const Primary = Template.bind({});
64
+ Primary.args = {
65
+ data: {
66
+ "Id": "E985B66B-5EA4-4A6F-81B9-0B12CAA99343",
67
+ "EvaluationCycleId": "A3FE0C73-E771-40BB-B87D-1E40597841A6",
68
+ "QuestionnaireId": "4C15A0A0-7DDF-4952-810C-B46105807CB7",
69
+ "UserId": "A200F4C5-8E41-41D4-A90C-B15EEC448517",
70
+ "TargetUserId": "A200F4C5-8E41-41D4-A90C-B15EEC448517",
71
+ "StartDate": 1678707055,
72
+ "EndDate": 1679707055,
73
+ "Status": "A",
74
+ "UserFinalScore": "4",
75
+ "SupervisorFinalScore": "3.6",
76
+ "UserColor": "#09168B",
77
+ "SupervisorColor": "#FF674C",
78
+ "CategoryAnswers": [
79
+ {
80
+ "Title": "Competências",
81
+ "Key": "1",
82
+ "Data": {
83
+ "Id": "57A06838-67F9-4726-BC36-719A0C797484",
84
+ "CategoryId": "CATEGORYID1",
85
+ "CategoryName": "Competências",
86
+ "OpenAnswer": false,
87
+ "GeneralEvaluationLevel": 2,
88
+ "Grade": "0",
89
+ "Description": "Inserir resposta numa escala numérica entre 1 e 5, sendo que 1 corresponde a “Mau” e 5 corresponde a “Excelente”",
90
+ "Score": "3.4",
91
+ "SupervisorScore": "3.4",
92
+ "Questions": [
93
+ {
94
+ "Id": "65562A64-A24B-425A-A345-560238CA2468",
95
+ "QuestionId": "7D17A8A5-E299-47D3-AB49-684135DB23A3",
96
+ "Subject": "Lorem ipsum dolor sit amet, consectetur adipiscing elit Lorem ipsum dolor sit amet, consectetur adipiscing elit",
97
+ "Description": "This is a description",
98
+ "Grade": "0",
99
+ "UserAnswer": "3.4",
100
+ "SupervisorAnswer": "3.4",
101
+ },
102
+ {
103
+ "Id": "85562A64-A24B-425A-A345-560238CA2468",
104
+ "QuestionId": "8D17A8A5-E299-47D3-AB49-684135DB23A3",
105
+ "Subject": "Lorem ipsum dolor sit amet, consectetur adipiscing elit Lorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elit",
106
+ "Description": "This is a description ",
107
+ "Grade": "0",
108
+ "UserAnswer": "3.4",
109
+ "SupervisorAnswer": "3.4",
110
+ }
111
+ ]
112
+ }
113
+ },
114
+ {
115
+ "Title": "Category 2",
116
+ "Key": "2",
117
+ "Data": {
118
+ "Id": "57A06838-67F9-4726-BC36-719A0C797484",
119
+ "CategoryId": "CATEGORYID2",
120
+ "CategoryName": "Category 2",
121
+ "OpenAnswer": false,
122
+ "GeneralEvaluationLevel": 0,
123
+ "Grade": "100",
124
+ "Description": "This is a description",
125
+ "Score": "3.4",
126
+ "SupervisorScore": "3.4",
127
+ "Questions": []
128
+ },
129
+ "Children": [
130
+ {
131
+ "Title": "Category 2.1",
132
+ "Key": "2.1",
133
+ "Data": {
134
+ "Id": "57A06838-67F9-4726-BC36-719A0C797484",
135
+ "CategoryId": "1CDED10D-0984-46D4-A9B3-7EC518D74A63",
136
+ "CategoryName": "Category 2.1",
137
+ "OpenAnswer": false,
138
+ "GeneralEvaluationLevel": 0,
139
+ "Grade": "50",
140
+ "Description": "This is a description",
141
+ "Score": "0.000",
142
+ "SupervisorScore": "0.000",
143
+ "Questions": []
144
+ },
145
+ "Children": [
146
+ {
147
+ "Title": "Category 2.1.1",
148
+ "Key": "2.1.1",
149
+ "Data": {
150
+ "Id": "37A06838-67F9-4726-BC36-719A0C797484",
151
+ "CategoryId": "3CDED10D-0984-46D4-A9B3-7EC518D74A63",
152
+ "CategoryName": "Category 2.1.1",
153
+ "OpenAnswer": false,
154
+ "GeneralEvaluationLevel": 0,
155
+ "Grade": "50",
156
+ "Description": "This is a description",
157
+ "Score": "3.4",
158
+ "SupervisorScore": "3.4",
159
+ "Questions": [
160
+ {
161
+ "Id": "365562A64-A24B-425A-A345-560238CA2468",
162
+ "QuestionId": "3D17A8A5-E299-47D3-AB49-684135DB23A3",
163
+ "Subject": "This is a Subject",
164
+ "Description": "This is a description",
165
+ "Grade": "50",
166
+ "UserAnswer": "3.4",
167
+ "SupervisorAnswer": "3.4",
168
+ },
169
+ {
170
+ "Id": "35562A64-A24B-425A-A345-560238CA2468",
171
+ "QuestionId": "3D17A8A5-E299-47D3-AB49-684135DB23A3",
172
+ "Subject": "This is a Subject",
173
+ "Description": "This is a description",
174
+ "Grade": "25",
175
+ "UserAnswer": "3.4",
176
+ "SupervisorAnswer": "3.4",
177
+ },
178
+ {
179
+ "Id": "35562A64-A24B-425A-A345-560238CA2468",
180
+ "QuestionId": "3D17A8A5-E299-47D3-AB49-684135DB23A3",
181
+ "Subject": "This is a Subject",
182
+ "Description": "This is a description",
183
+ "Grade": "25",
184
+ "UserAnswer": "3.4",
185
+ "SupervisorAnswer": "3.4",
186
+ }
187
+ ]
188
+ }
189
+ }
190
+ ]
191
+ },
192
+ {
193
+ "Title": "Category 2.2",
194
+ "Key": "2.2",
195
+ "Data": {
196
+ "Id": "57A06838-67F9-4726-BC36-719A0C797484",
197
+ "CategoryId": "PCDED10D-0984-46D4-A9B3-7EC518D74A63",
198
+ "CategoryName": "Category 2.2",
199
+ "OpenAnswer": false,
200
+ "GeneralEvaluationLevel": 0,
201
+ "Grade": "50",
202
+ "Description": "This is a description",
203
+ "Score": "3.4",
204
+ "SupervisorScore": "3.4",
205
+ "Questions": [
206
+ {
207
+ "Id": "P5562A64-A24B-425A-A345-560238CA2468",
208
+ "QuestionId": "PD17A8A5-E299-47D3-AB49-684135DB23A3",
209
+ "Subject": "This is a Subject",
210
+ "Description": "This is a description",
211
+ "Grade": "50",
212
+ "UserAnswer": "3.4",
213
+ "SupervisorAnswer": "3.4",
214
+ },
215
+ {
216
+ "Id": "P5562A64-A24B-425A-A345-560238CA2468",
217
+ "QuestionId": "PD17A8A5-E299-47D3-AB49-684135DB23A3",
218
+ "Subject": "This is a Subject",
219
+ "Description": "This is a description",
220
+ "Grade": "50",
221
+ "UserAnswer": "3.4",
222
+ "SupervisorAnswer": "3.4",
223
+ }
224
+ ]
225
+ },
226
+ "Children": []
227
+ }
228
+ ]
229
+ },
230
+ {
231
+ "Title": "Category 3",
232
+ "Key": "3",
233
+ "Data": {
234
+ "Id": "97A06838-67F9-4726-BC36-719A0C797484",
235
+ "CategoryId": "CATEGORYID3",
236
+ "CategoryName": "Category 3",
237
+ "OpenAnswer": true,
238
+ "GeneralEvaluationLevel": 0,
239
+ "Grade": "0",
240
+ "Description": "This is a description",
241
+ "Score": "3.4",
242
+ "SupervisorScore": "3.4",
243
+ "Questions": [
244
+ {
245
+ "Id": "95562A64-A24B-425A-A345-560238CA2468",
246
+ "QuestionId": "9D17A8A5-E299-47D3-AB49-684135DB23A3",
247
+ "Subject": "This is a Subject",
248
+ "Description": "This is a description",
249
+ "Grade": "0",
250
+ "UserAnswer": "3.4",
251
+ "SupervisorAnswer": "3.4",
252
+ },
253
+ {
254
+ "Id": "93862A64-A24B-425A-A345-560238CA2468",
255
+ "QuestionId": "9D38A8A5-E299-47D3-AB49-684135DB23A3",
256
+ "Subject": "This is a Subject",
257
+ "Description": "This is a description",
258
+ "Grade": "0",
259
+ "UserAnswer": "3.4",
260
+ "SupervisorAnswer": "3.4",
261
+ }
262
+ ]
263
+ }
264
+ },
265
+ ]
266
+ }
267
+ };
268
+
269
+
270
+
@@ -0,0 +1,81 @@
1
+ import translate from '../../constants/translationHelper';
2
+
3
+ export type EvaluationOption = {
4
+ value: string;
5
+ label: string;
6
+ };
7
+
8
+ export type EvaluationOptions = {
9
+ scale_0: EvaluationOption[];
10
+ scale_1: EvaluationOption[];
11
+ scale_2: EvaluationOption[];
12
+ };
13
+
14
+ export const evaluationOptions: EvaluationOptions = {
15
+ scale_0: [
16
+ {
17
+ value: '1',
18
+ label: '1',
19
+ },
20
+ {
21
+ value: '2',
22
+ label: '2',
23
+ },
24
+ {
25
+ value: '3',
26
+ label: '3',
27
+ },
28
+ {
29
+ value: '4',
30
+ label: '4',
31
+ },
32
+ {
33
+ value: '5',
34
+ label: '5',
35
+ }
36
+ ],
37
+ scale_1: [
38
+ {
39
+ value: '1',
40
+ label: '1',
41
+ },
42
+ {
43
+ value: '2',
44
+ label: '2',
45
+ },
46
+ {
47
+ value: '3',
48
+ label: '3',
49
+ },
50
+ {
51
+ value: '4',
52
+ label: '4',
53
+ },
54
+ ],
55
+ scale_2: [
56
+ {
57
+ value: '1',
58
+ label: '1',
59
+ },
60
+ {
61
+ value: '2',
62
+ label: '2',
63
+ },
64
+ {
65
+ value: '3',
66
+ label: '3',
67
+ },
68
+ {
69
+ value: '4',
70
+ label: '4',
71
+ },
72
+ {
73
+ value: '5',
74
+ label: '5',
75
+ },
76
+ {
77
+ value: 'na',
78
+ label: translate('components.categoryResponse.notApplicable')
79
+ }
80
+ ]
81
+ }
@@ -0,0 +1,254 @@
1
+ import styles from './styles.module.scss';
2
+ import { useEffect, useImperativeHandle, useState } from 'react';
3
+ import { useForm } from 'antd/es/form/Form';
4
+ import classNames from 'classnames';
5
+ import React from 'react';
6
+ import { t } from 'i18next';
7
+
8
+ interface CategoryResponse {
9
+ data: any;
10
+ serverReadyData: any;
11
+ onNextCategoryAvailabilityChange: (hasNext: boolean) => void;
12
+ onPreviousCategoryAvailabilityChange: (hasPrevious: boolean) => void;
13
+ };
14
+
15
+ type Category = {
16
+ Title: string;
17
+ Key: string;
18
+ Data: any;
19
+ Children?: Category[];
20
+ };
21
+
22
+ const updateCategoryAnswers = (CategoryAnswers: any[], categoryToUpdate: any, updatedQuestions: any) => {
23
+ CategoryAnswers.forEach((category: any) => {
24
+ if (category.Data.CategoryId === categoryToUpdate.CategoryId) {
25
+ category.Data.Questions = category.Data.Questions.map((question: any, index: any) => {
26
+ return {
27
+ ...question,
28
+ Answer: updatedQuestions[index].Answer
29
+ };
30
+ });
31
+ }
32
+ else if (category.Children && category.Children.length > 0) {
33
+ updateCategoryAnswers(category.Children, categoryToUpdate, updatedQuestions);
34
+ }
35
+ });
36
+ return CategoryAnswers;
37
+ };
38
+
39
+ const findCategoryWithQuestions = (currentKey: string, categories: Category[], direction: 'next' | 'prev'): Category | null => {
40
+ let foundCurrent = false;
41
+
42
+ const searchCategories = (categoryList: Category[], direction: 'next' | 'prev'): Category | null => {
43
+ const iterable = direction === 'next' ? categoryList : [...categoryList].reverse();
44
+
45
+ for (const category of iterable) {
46
+ if (foundCurrent) {
47
+ if (category.Data.Questions.length > 0) {
48
+ return category;
49
+ }
50
+ } else if (category.Key === currentKey) {
51
+ foundCurrent = true;
52
+ }
53
+
54
+ if (category.Children) {
55
+ const result = searchCategories(category.Children, direction);
56
+ if (result) {
57
+ return result;
58
+ }
59
+ }
60
+ }
61
+ return null;
62
+ };
63
+
64
+ return searchCategories(categories, direction);
65
+ };
66
+
67
+ const getCategoryObject = (currentKey: string, categories: Category[], direction: 'next' | 'prev'): any => {
68
+ const category = findCategoryWithQuestions(currentKey, categories, direction);
69
+ return category ? category.Data : null;
70
+ };
71
+
72
+ const hasCategory = (currentKey: string, categories: Category[], direction: 'next' | 'prev'): boolean => {
73
+ const category = findCategoryWithQuestions(currentKey, categories, direction);
74
+ return !!category;
75
+ };
76
+
77
+ export const CategoryReadOnly = React.forwardRef((props: CategoryResponse, ref) => {
78
+ const { data } = props;
79
+
80
+ const [currentKey, setCurrentKey] = useState(data.CategoryAnswers[0].Key);
81
+ const [localData, setLocalData] = useState<any>(data);
82
+ // Setting first set of questions as default open
83
+ const [selectedCategory, setSelectedCategory] = useState<any>(data.CategoryAnswers[0]);
84
+ const [initialValues, setInitialValues] = useState<any>(data.CategoryAnswers[0].Data);
85
+ const [form] = useForm();
86
+
87
+
88
+ const updateInitialValues = (data: any) => {
89
+ const {
90
+ Questions,
91
+ OpenAnswer
92
+ } = data;
93
+
94
+ const initial = {
95
+ Questions: Questions.map((question: any) => ({
96
+ Subject: question.Subject,
97
+ Description: question.Description,
98
+ Answer: question.Answer || ""
99
+ })),
100
+ OpenAnswer: OpenAnswer
101
+ };
102
+
103
+ setInitialValues(initial);
104
+ form.setFieldsValue(initial);
105
+ };
106
+
107
+ const handleLabelClick = (category: any, index: number) => {
108
+ setSelectedCategory(category);
109
+ updateInitialValues(category.Data);
110
+ setCurrentKey(index);
111
+ };
112
+
113
+ const onFinish = (values: any) => {
114
+ const updatedQuestions = initialValues.Questions.map((question: any, index: number) => {
115
+ return {
116
+ ...question,
117
+ Answer: values.Questions[index].Answer
118
+ };
119
+ });
120
+
121
+ const updatedCategory = updateCategoryAnswers(localData.CategoryAnswers, selectedCategory.Data, updatedQuestions);
122
+
123
+ const updatedLocalData = { ...localData, CategoryAnswers: updatedCategory };
124
+
125
+ setLocalData(updatedLocalData);
126
+ };
127
+
128
+ const renderCategories = (categories: any) => {
129
+ return categories.map((category: any, index: number) => {
130
+ const labelClasses = classNames({
131
+ [styles.cursorPointer]: category.Data.Questions.length > 0
132
+ }, [styles.label]);
133
+
134
+ const indexCheck = category.Data.Questions.length > 0 && category.Key;
135
+
136
+ return (
137
+ <div
138
+ className={`${styles.labelWrapper} ${category.Data.CategoryId === selectedCategory.Data.CategoryId ? styles.active : ""}`}
139
+ key={index}
140
+ >
141
+ <div
142
+ className={labelClasses}
143
+ data-index={indexCheck}
144
+ onClick={() => category.Data.Questions.length > 0 && handleLabelClick(category, indexCheck)}
145
+ >
146
+ {category.Title}
147
+ </div>
148
+ {category.Children && (
149
+ <div className={styles.subCategory}>
150
+ {renderCategories(category.Children)}
151
+ </div>
152
+ )}
153
+ </div>
154
+ )
155
+ });
156
+ };
157
+
158
+ const handleNextClick = () => {
159
+ const nextCategory = findCategoryWithQuestions(currentKey, localData.CategoryAnswers, 'next');
160
+ if (nextCategory) {
161
+ setCurrentKey(nextCategory.Key);
162
+ setSelectedCategory(nextCategory);
163
+ }
164
+
165
+ const nextCategoryData = getCategoryObject(currentKey, localData.CategoryAnswers, 'next');
166
+ setSelectedCategory(nextCategoryData ? { ...nextCategory, data: nextCategoryData } : null);
167
+ updateInitialValues(nextCategoryData);
168
+ };
169
+
170
+ const handlePreviousClick = () => {
171
+ const prevCategory = findCategoryWithQuestions(currentKey, localData.CategoryAnswers, 'prev');
172
+ if (prevCategory) {
173
+ setCurrentKey(prevCategory.Key);
174
+ setSelectedCategory(prevCategory);
175
+ }
176
+
177
+ const prevCategoryData = getCategoryObject(currentKey, localData.CategoryAnswers, 'prev');
178
+ setSelectedCategory(prevCategoryData ? { ...prevCategory, data: prevCategoryData } : null);
179
+ updateInitialValues(prevCategoryData);
180
+ };
181
+
182
+ useImperativeHandle(ref, () => ({
183
+ handleNextClick,
184
+ handlePreviousClick,
185
+ }));
186
+
187
+ useEffect(() => {
188
+ props.serverReadyData(localData);
189
+ }, [localData]);
190
+
191
+ useEffect(() => {
192
+ const hasNext = hasCategory(currentKey, localData.CategoryAnswers, 'next');
193
+ props.onNextCategoryAvailabilityChange(hasNext);
194
+
195
+ const hasPrevious = hasCategory(currentKey, localData.CategoryAnswers, 'prev');
196
+ props.onPreviousCategoryAvailabilityChange(hasPrevious);
197
+ }, [currentKey, localData.CategoryAnswers, props]);
198
+
199
+ const questionWrapperClasses = classNames({
200
+ [styles.questionWrapperOpenAnswer]: selectedCategory.Data.OpenAnswer,
201
+ }, [styles.questionWrapper]);
202
+
203
+ const answerClasses = classNames({
204
+ [styles.answerOpenAnswer]: selectedCategory.Data.OpenAnswer,
205
+ }, [styles.answer]);
206
+
207
+ return (
208
+ <div className={styles.categoryResponse}>
209
+ <div className={styles.sidebarWrapper}>
210
+ <div className={styles.title}>{t('components.categoryReadOnly.categories')}</div>
211
+ {renderCategories(localData.CategoryAnswers)}
212
+ </div>
213
+ <div className={styles.contentWrapper}>
214
+
215
+ <div className={styles.details}>
216
+ <div className={styles.categoryName}>
217
+ {selectedCategory?.Data?.CategoryName}
218
+ </div>
219
+ <div className={styles.categoryGradesWrapper}>
220
+ <div className={styles.categoryGradesTitle}>
221
+ {t('components.categoryReadOnly.categoryAverage')}
222
+ </div>
223
+ <div className={styles.categoryGrades}>
224
+ <div className={styles.gradeBox} style={{ backgroundColor: localData.UserColor }}>
225
+ {selectedCategory?.Data.Score}
226
+ </div>
227
+ <div className={styles.gradeBox} style={{ backgroundColor: localData.SupervisorColor }}>
228
+ {selectedCategory?.Data.SupervisorScore}
229
+ </div>
230
+ </div>
231
+ </div>
232
+ </div>
233
+
234
+ {selectedCategory?.Data?.Questions.map((question: any, index: number) => (
235
+ <div className={questionWrapperClasses} key={index}>
236
+ <div className={styles.question}>
237
+ <div className={styles.subject}>
238
+ {question.Subject}
239
+ </div>
240
+ </div>
241
+ <div className={answerClasses}>
242
+ <div className={styles.gradeBox} style={{ backgroundColor: localData.UserColor }}>
243
+ {question.UserAnswer}
244
+ </div>
245
+ <div className={styles.gradeBox} style={{ backgroundColor: localData.SupervisorColor }}>
246
+ {question.SupervisorAnswer}
247
+ </div>
248
+ </div>
249
+ </div>
250
+ ))}
251
+ </div>
252
+ </div>
253
+ );
254
+ });