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