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