1 | import { useState } from 'react';
|
---|
2 | import { useHistory } from 'react-router-dom';
|
---|
3 | import useForm from '../../../hooks/useForm';
|
---|
4 |
|
---|
5 | import {
|
---|
6 | EmployeeEditWrapper,
|
---|
7 | Title,
|
---|
8 | RowWrapper,
|
---|
9 | LabelAndInputWrapper,
|
---|
10 | Label,
|
---|
11 | StandardInputField,
|
---|
12 | Dropdown,
|
---|
13 | DropdownOption,
|
---|
14 | SwitchRowWrapper,
|
---|
15 | SwitchTitle,
|
---|
16 | SwitchLabelAndInputWrapper,
|
---|
17 | AccountSwitch,
|
---|
18 | BackButton,
|
---|
19 | SaveChangesButton,
|
---|
20 | VisibilityIcon,
|
---|
21 | VisibilityOffIcon,
|
---|
22 | } from './styles';
|
---|
23 |
|
---|
24 | import IconButton from '@mui/material/IconButton';
|
---|
25 | import Checkbox from '@mui/material/Checkbox';
|
---|
26 | import ListItemText from '@mui/material/ListItemText';
|
---|
27 | import AbsoluteLoader from '../../Loaders/AbsoluteLoader';
|
---|
28 |
|
---|
29 | import { employeeStatus } from '../../../config/enums';
|
---|
30 | import useGetData from '../../../hooks/useGetData';
|
---|
31 | import useCreateEmployee from '../../../hooks/useCreateEmployee';
|
---|
32 |
|
---|
33 | import { defaultUser } from '../../../config/defaultUser';
|
---|
34 |
|
---|
35 | const MenuProps = {
|
---|
36 | PaperProps: {
|
---|
37 | style: {
|
---|
38 | maxHeight: 220,
|
---|
39 | },
|
---|
40 | },
|
---|
41 | };
|
---|
42 |
|
---|
43 | const EmployeeCreate = () => {
|
---|
44 | const [isPasswordVisible, setIsPasswordVisible] = useState(false);
|
---|
45 | let history = useHistory();
|
---|
46 | const { data: zoneOptions, isLoading: isLoadingZonesData } = useGetData({
|
---|
47 | url: `/parkingZone/parkingZoneNames`,
|
---|
48 | });
|
---|
49 | const { createEmployee } = useCreateEmployee();
|
---|
50 | const {
|
---|
51 | data: employeeEditableData,
|
---|
52 | onFormChange: setEmployeeEditableData,
|
---|
53 | } = useForm({ ...defaultUser });
|
---|
54 | const [accStatus, setAccStatus] = useState(defaultUser.locked);
|
---|
55 | const {
|
---|
56 | data: { confirmPassword },
|
---|
57 | onFormChange: setConfirmPassword,
|
---|
58 | } = useForm({
|
---|
59 | confirmPassword: defaultUser.password,
|
---|
60 | });
|
---|
61 | const [zones, setZones] = useState(defaultUser.parkingZones); // TODO RENAME ZONE TO ZONES
|
---|
62 | console.log(zones);
|
---|
63 | const statusOptions = Object.keys(employeeStatus).map((key) => {
|
---|
64 | return {
|
---|
65 | text: employeeStatus[key],
|
---|
66 | value: key,
|
---|
67 | };
|
---|
68 | });
|
---|
69 |
|
---|
70 | const onCreateEmployee = () => {
|
---|
71 | if (zones.length === 1 && zones[0] === 'NONE') {
|
---|
72 | zones.shift();
|
---|
73 | }
|
---|
74 | console.log(`Confirm password: ${confirmPassword}`);
|
---|
75 | const changedEmployee = {
|
---|
76 | ...employeeEditableData,
|
---|
77 | locked: accStatus,
|
---|
78 | parkingZones: zones,
|
---|
79 | confirmPass: confirmPassword,
|
---|
80 | };
|
---|
81 | createEmployee({ employee: changedEmployee });
|
---|
82 | };
|
---|
83 | const handleZonesChange = (event) => {
|
---|
84 | const {
|
---|
85 | target: { value },
|
---|
86 | } = event;
|
---|
87 | if (value.length > 1 && value[0] === 'NONE') {
|
---|
88 | value.shift();
|
---|
89 | }
|
---|
90 | setZones(
|
---|
91 | typeof value === 'string'
|
---|
92 | ? value.split(', ')
|
---|
93 | : value.length === 0
|
---|
94 | ? ['NONE']
|
---|
95 | : value
|
---|
96 | );
|
---|
97 | };
|
---|
98 | return (
|
---|
99 | <EmployeeEditWrapper>
|
---|
100 | <Title variant='h5'>Создади вработен</Title>
|
---|
101 | <RowWrapper>
|
---|
102 | <LabelAndInputWrapper>
|
---|
103 | <Label>Име</Label>
|
---|
104 | <StandardInputField
|
---|
105 | value={employeeEditableData.firstName}
|
---|
106 | name='firstName'
|
---|
107 | onChange={setEmployeeEditableData}
|
---|
108 | />
|
---|
109 | </LabelAndInputWrapper>
|
---|
110 | <LabelAndInputWrapper>
|
---|
111 | <Label>Презиме</Label>
|
---|
112 | <StandardInputField
|
---|
113 | value={employeeEditableData.lastName}
|
---|
114 | name='lastName'
|
---|
115 | onChange={setEmployeeEditableData}
|
---|
116 | />
|
---|
117 | </LabelAndInputWrapper>
|
---|
118 | </RowWrapper>
|
---|
119 | <RowWrapper>
|
---|
120 | <LabelAndInputWrapper>
|
---|
121 | <Label>Емаил</Label>
|
---|
122 | <StandardInputField
|
---|
123 | value={employeeEditableData.email}
|
---|
124 | name='email'
|
---|
125 | type='email'
|
---|
126 | onChange={setEmployeeEditableData}
|
---|
127 | />
|
---|
128 | </LabelAndInputWrapper>
|
---|
129 | <LabelAndInputWrapper>
|
---|
130 | <Label>Телефон</Label>
|
---|
131 | <StandardInputField
|
---|
132 | value={employeeEditableData.mobile}
|
---|
133 | name='mobile'
|
---|
134 | onChange={setEmployeeEditableData}
|
---|
135 | />
|
---|
136 | </LabelAndInputWrapper>
|
---|
137 | </RowWrapper>
|
---|
138 | <RowWrapper>
|
---|
139 | <LabelAndInputWrapper>
|
---|
140 | <Label>Лозинка</Label>
|
---|
141 | <StandardInputField
|
---|
142 | value={employeeEditableData.password}
|
---|
143 | name='password'
|
---|
144 | type={isPasswordVisible ? 'text' : 'password'}
|
---|
145 | $autoComplete={true}
|
---|
146 | onChange={setEmployeeEditableData}
|
---|
147 | />
|
---|
148 | </LabelAndInputWrapper>
|
---|
149 | <IconButton
|
---|
150 | sx={{ marginTop: '23px' }}
|
---|
151 | onClick={() => setIsPasswordVisible(!isPasswordVisible)}
|
---|
152 | >
|
---|
153 | {isPasswordVisible ? (
|
---|
154 | <VisibilityIcon />
|
---|
155 | ) : (
|
---|
156 | <VisibilityOffIcon />
|
---|
157 | )}
|
---|
158 | </IconButton>
|
---|
159 | <LabelAndInputWrapper>
|
---|
160 | <Label>Потврди лозинка</Label>
|
---|
161 | <StandardInputField
|
---|
162 | value={confirmPassword}
|
---|
163 | name='confirmPassword'
|
---|
164 | type={isPasswordVisible ? 'text' : 'password'}
|
---|
165 | onChange={setConfirmPassword}
|
---|
166 | />
|
---|
167 | </LabelAndInputWrapper>
|
---|
168 | </RowWrapper>
|
---|
169 | <RowWrapper>
|
---|
170 | {isLoadingZonesData ? (
|
---|
171 | <AbsoluteLoader
|
---|
172 | containerStyle={{
|
---|
173 | position: 'absolute',
|
---|
174 | left: '310px',
|
---|
175 | bottom: '5px',
|
---|
176 | width: '40px',
|
---|
177 | height: '40px',
|
---|
178 | }}
|
---|
179 | />
|
---|
180 | ) : null}
|
---|
181 | <LabelAndInputWrapper>
|
---|
182 | <Label>Одговорен за</Label>
|
---|
183 | <Dropdown
|
---|
184 | multiple
|
---|
185 | value={zones}
|
---|
186 | onChange={handleZonesChange}
|
---|
187 | renderValue={(selected) => {
|
---|
188 | return selected.join(', ');
|
---|
189 | }}
|
---|
190 | MenuProps={MenuProps}
|
---|
191 | >
|
---|
192 | {!isLoadingZonesData &&
|
---|
193 | zoneOptions.map((zone) => (
|
---|
194 | <DropdownOption value={zone} key={zone}>
|
---|
195 | <Checkbox
|
---|
196 | checked={zones.indexOf(zone) > -1}
|
---|
197 | />
|
---|
198 | <ListItemText primary={zone} />
|
---|
199 | </DropdownOption>
|
---|
200 | ))}
|
---|
201 | </Dropdown>
|
---|
202 | </LabelAndInputWrapper>
|
---|
203 | <LabelAndInputWrapper>
|
---|
204 | <Label>Статус</Label>
|
---|
205 | <Dropdown
|
---|
206 | value={employeeEditableData.status}
|
---|
207 | name='status'
|
---|
208 | onChange={setEmployeeEditableData}
|
---|
209 | MenuProps={MenuProps}
|
---|
210 | >
|
---|
211 | {statusOptions.map((option) => (
|
---|
212 | <DropdownOption
|
---|
213 | value={option.value}
|
---|
214 | key={option.value}
|
---|
215 | >
|
---|
216 | {option.text}
|
---|
217 | </DropdownOption>
|
---|
218 | ))}
|
---|
219 | </Dropdown>
|
---|
220 | </LabelAndInputWrapper>
|
---|
221 | </RowWrapper>
|
---|
222 | <SwitchRowWrapper>
|
---|
223 | <SwitchTitle>Акаунт</SwitchTitle>
|
---|
224 | <SwitchLabelAndInputWrapper>
|
---|
225 | <Label>Активен:</Label>
|
---|
226 | <AccountSwitch
|
---|
227 | checked={!accStatus}
|
---|
228 | value={accStatus}
|
---|
229 | name='locked'
|
---|
230 | onClick={() => setAccStatus(!accStatus)}
|
---|
231 | />
|
---|
232 | </SwitchLabelAndInputWrapper>
|
---|
233 | </SwitchRowWrapper>
|
---|
234 | <RowWrapper>
|
---|
235 | <BackButton onClick={() => history.push('/employees')}>
|
---|
236 | Врати се назад
|
---|
237 | </BackButton>
|
---|
238 | <SaveChangesButton onClick={onCreateEmployee}>
|
---|
239 | Создади вработен
|
---|
240 | </SaveChangesButton>
|
---|
241 | </RowWrapper>
|
---|
242 | </EmployeeEditWrapper>
|
---|
243 | );
|
---|
244 | };
|
---|
245 |
|
---|
246 | export default EmployeeCreate;
|
---|