1
|
import React, { useState, useEffect } from 'react';
|
2
|
import './App.css';
|
3
|
import * as api_fetch from './api'
|
4
|
|
5
|
|
6
|
const OverviewAdmin = () => {
|
7
|
|
8
|
useEffect(() => {
|
9
|
api_fetch.getUsersOverview().then(usersOverview => {
|
10
|
setEmployees(usersOverview);
|
11
|
}).catch(reason => {
|
12
|
alert(reason)
|
13
|
});
|
14
|
}, []);
|
15
|
|
16
|
// states
|
17
|
const [employees, setEmployees] = useState([
|
18
|
{
|
19
|
name: 'Sadam',
|
20
|
id: 0,
|
21
|
sickday: 10,
|
22
|
holiday: 10
|
23
|
}
|
24
|
]);
|
25
|
|
26
|
const [isEdit, setEdit] = useState(false);
|
27
|
const [editedUserId, setEditedUserId] = useState();
|
28
|
const [prevEdit, setPrevEdit] = useState();
|
29
|
|
30
|
|
31
|
// functions
|
32
|
function changeSickdays(newValue) {
|
33
|
const newEmployees = employees.map(employee => {
|
34
|
if (editedUserId === employee.id) {
|
35
|
return {
|
36
|
...employee,
|
37
|
sickday: newValue,
|
38
|
};
|
39
|
} else {
|
40
|
return employee
|
41
|
}
|
42
|
})
|
43
|
setEmployees(newEmployees);
|
44
|
}
|
45
|
|
46
|
function changeHoliday(newValue) {
|
47
|
const newEmployees = employees.map(employee => {
|
48
|
if (editedUserId === employee.id) {
|
49
|
return {
|
50
|
...employee,
|
51
|
holiday: newValue,
|
52
|
};
|
53
|
} else {
|
54
|
return employee
|
55
|
}
|
56
|
})
|
57
|
setEmployees(newEmployees);
|
58
|
}
|
59
|
|
60
|
const submitEdit = async (e) => {
|
61
|
|
62
|
setEdit(isEdit === true ? false : true);
|
63
|
setPrevEdit(employees);
|
64
|
e.preventDefault();
|
65
|
|
66
|
const found = employees.find(employee => editedUserId === employee.id);
|
67
|
const foundPrevEdit = prevEdit.find(employee => editedUserId === employee.id);
|
68
|
|
69
|
const dataOverviewObject = {
|
70
|
id: found.id,
|
71
|
vacationCount: Number(found.holiday) - foundPrevEdit.holiday,
|
72
|
sickDayCount: Number(found.sickday),
|
73
|
role: found.role
|
74
|
}
|
75
|
|
76
|
api_fetch.saveDataOverview(dataOverviewObject).catch(reason => {
|
77
|
alert(reason)
|
78
|
});
|
79
|
}
|
80
|
|
81
|
const cancel = () => {
|
82
|
setEmployees(prevEdit)
|
83
|
setEdit(false)
|
84
|
}
|
85
|
|
86
|
// ***** overview button ******
|
87
|
|
88
|
const editEmployee = (employeeId, e) => {
|
89
|
setEdit(true)
|
90
|
setEditedUserId(employeeId)
|
91
|
setPrevEdit(employees)
|
92
|
|
93
|
e.preventDefault();
|
94
|
}
|
95
|
|
96
|
return (
|
97
|
<div>
|
98
|
<div className="side-content">
|
99
|
<div className="side-board column">
|
100
|
<form className="column side-board-items" onSubmit={(e) => submitEdit(e)}>
|
101
|
{/* <form className="column side-board-items" onSubmit={(e) => submitEdit(e)}> */}
|
102
|
<div className="side-board-heading row">
|
103
|
<h3>Overview</h3>
|
104
|
</div>
|
105
|
<div className="underline-1"></div>
|
106
|
<div className="side-board-items column">
|
107
|
<table border = "0">
|
108
|
<tbody>
|
109
|
<tr>
|
110
|
<th className="th-left">Name</th>
|
111
|
<th>Sick Days</th>
|
112
|
<th>Holiday</th>
|
113
|
<th></th>
|
114
|
</tr>
|
115
|
{employees.map(employee => (
|
116
|
<tr>
|
117
|
<td>{employee.name} {editedUserId}</td>
|
118
|
{/* SickDays Input */}
|
119
|
<td className="td-center">
|
120
|
{isEdit === true && editedUserId === employee.id ? (
|
121
|
<input className="offsInput" type="number" min="0" value={employee.sickday} onChange={(e) => changeSickdays(e.target.value)}/>
|
122
|
) : (
|
123
|
employee.sickday
|
124
|
)}
|
125
|
</td>
|
126
|
{/* Holiday Input */}
|
127
|
<td className="td-center">
|
128
|
{isEdit === true && editedUserId === employee.id ? (
|
129
|
<input className="offsInput" type="number" min="0" value={employee.holiday} onChange={(e) => changeHoliday(e.target.value)}/>
|
130
|
) : (
|
131
|
employee.holiday
|
132
|
)}</td>
|
133
|
{/* Edit Button */}
|
134
|
<button onClick={(e) => editEmployee(employee.id, e)} className="btn-edit">
|
135
|
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-pencil" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
|
136
|
<path fill-rule="evenodd" d="M11.293 1.293a1 1 0 0 1 1.414 0l2 2a1 1 0 0 1 0 1.414l-9 9a1 1 0 0 1-.39.242l-3 1a1 1 0 0 1-1.266-1.265l1-3a1 1 0 0 1 .242-.391l9-9zM12 2l2 2-9 9-3 1 1-3 9-9z"/>
|
137
|
<path fill-rule="evenodd" d="M12.146 6.354l-2.5-2.5.708-.708 2.5 2.5-.707.708zM3 10v.5a.5.5 0 0 0 .5.5H4v.5a.5.5 0 0 0 .5.5H5v.5a.5.5 0 0 0 .5.5H6v-1.5a.5.5 0 0 0-.5-.5H5v-.5a.5.5 0 0 0-.5-.5H3z"/>
|
138
|
</svg>
|
139
|
</button>
|
140
|
{/* End of Edit Button */}
|
141
|
</tr>
|
142
|
))}
|
143
|
</tbody>
|
144
|
</table>
|
145
|
{/* Submit & Reject Button */}
|
146
|
<div className="column">
|
147
|
{isEdit === true ? <button type="submit" className="btn btn-submit">
|
148
|
Save</button> : null}
|
149
|
{isEdit === true ? (
|
150
|
<button onClick={cancel} type="submit" className="btn btn-cancel">Cancel</button>
|
151
|
) : (null)}
|
152
|
</div>
|
153
|
</div>
|
154
|
</form>
|
155
|
</div>
|
156
|
</div>
|
157
|
</div>
|
158
|
);
|
159
|
}
|
160
|
|
161
|
|
162
|
export default OverviewAdmin;
|