forked from suitenumerique/projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabelEditStep.jsx
More file actions
executable file
·110 lines (94 loc) · 2.95 KB
/
Copy pathLabelEditStep.jsx
File metadata and controls
executable file
·110 lines (94 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { dequal } from 'dequal';
import React, { useCallback, useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import { useTranslation } from 'react-i18next';
import { Button, Input } from '@openfun/cunningham-react';
import PopoverHeader from '../../ui/Popover/PopoverHeader';
import { useForm, useSteps } from '../../hooks';
import LabelColors from '../../constants/LabelColors';
import DeleteStep from '../DeleteStep';
import ColorPicker from '../../ui/ColorPicker';
import styles from './LabelEditStep.module.scss';
const StepTypes = {
DELETE: 'DELETE',
};
const LabelEditStep = React.memo(({ defaultData, onUpdate, onDelete, onBack }) => {
const [t] = useTranslation();
const nameField = useRef(null);
useEffect(() => {
nameField.current.select();
}, []);
const [data, handleFieldChange] = useForm(() => ({
color: LabelColors[0],
...defaultData,
name: defaultData.name || '',
}));
const [step, openStep, handleBack] = useSteps();
const handleSubmit = useCallback(() => {
const cleanData = {
...data,
name: data.name.trim() || null,
};
if (!dequal(cleanData, defaultData)) {
onUpdate(cleanData);
}
onBack();
}, [defaultData, data, onUpdate, onBack]);
const handleDeleteClick = useCallback(() => {
openStep(StepTypes.DELETE);
}, [openStep]);
if (step && step.type === StepTypes.DELETE) {
return (
<DeleteStep
title="common.deleteLabel"
content="common.areYouSureYouWantToDeleteThisLabel"
buttonContent="action.deleteLabel"
onConfirm={onDelete}
onBack={handleBack}
/>
);
}
return (
<div style={{ width: '290px' }}>
<PopoverHeader
onBack={onBack}
title={t('common.editLabel', {
context: 'title',
})}
/>
<form onSubmit={handleSubmit}>
<div className={styles.fieldLabel}>{t('common.title')}</div>
<Input
label="Nom de l'étiquette"
ref={nameField}
name="name"
value={data.name}
className={styles.field}
onChange={handleFieldChange}
/>
<div className={styles.fieldLabel}>{t('common.color')}</div>
<ColorPicker colors={LabelColors} current={data.color} onChange={handleFieldChange} />
<div className={styles.controls}>
<Button color="brand" variant="primary" type="submit">
{t('action.save')}
</Button>
<Button
color="error"
variant="bordered"
className={styles.deleteButton}
onClick={handleDeleteClick}
>
{t('action.delete')}
</Button>
</div>
</form>
</div>
);
});
LabelEditStep.propTypes = {
defaultData: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
onUpdate: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired,
onBack: PropTypes.func.isRequired,
};
export default LabelEditStep;