Revize 1865a0be
Přidáno uživatelem Pavel Fidransky před více než 4 roky(ů)
client/src/OverviewAdmin.js | ||
---|---|---|
1 | 1 |
import React, { useState, useEffect } from 'react'; |
2 | 2 |
import './App.css'; |
3 |
import * as api_fetch from './api'
|
|
3 |
import * as api from './api';
|
|
4 | 4 |
|
5 |
export default function OverviewAdmin(props) { |
|
5 | 6 |
|
6 |
const OverviewAdmin = (props) => { |
|
7 |
useEffect(() => { |
|
8 |
api.getUsersOverview().then(usersOverview => { |
|
9 |
props.setEmployees(usersOverview); |
|
10 |
}).catch(reason => { |
|
11 |
alert(reason); |
|
12 |
}); |
|
13 |
}, []); |
|
7 | 14 |
|
8 |
useEffect(() => { |
|
9 |
api_fetch.getUsersOverview().then(usersOverview => { |
|
10 |
props.setEmployees(usersOverview); |
|
11 |
}).catch(reason => { |
|
12 |
alert(reason) |
|
13 |
}); |
|
14 |
}, []); |
|
15 |
const [isEdit, setEdit] = useState(false); |
|
16 |
const [editedUserId, setEditedUserId] = useState(); |
|
17 |
const [prevEdit, setPrevEdit] = useState(); |
|
15 | 18 |
|
16 |
const [isEdit, setEdit] = useState(false); |
|
17 |
const [editedUserId, setEditedUserId] = useState(); |
|
18 |
const [prevEdit, setPrevEdit] = useState(); |
|
19 |
|
|
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 |
} |
|
20 | 47 |
|
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 |
} |
|
48 |
async function submitEdit(e) { |
|
49 |
setEdit(isEdit === true ? false : true); |
|
50 |
setPrevEdit(props.employees); |
|
51 |
e.preventDefault(); |
|
35 | 52 |
|
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 |
} |
|
53 |
const found = props.employees.find(employee => editedUserId === employee.id); |
|
54 |
const foundPrevEdit = prevEdit.find(employee => editedUserId === employee.id); |
|
49 | 55 |
|
50 |
const submitEdit = async (e) => { |
|
51 |
|
|
52 |
setEdit(isEdit === true ? false : true); |
|
53 |
setPrevEdit(props.employees); |
|
54 |
e.preventDefault(); |
|
56 |
const dataOverviewObject = { |
|
57 |
id: found.id, |
|
58 |
vacationCount: Number(found.holiday) - foundPrevEdit.holiday, |
|
59 |
sickDayCount: Number(found.sickday), |
|
60 |
role: found.role, |
|
61 |
}; |
|
55 | 62 |
|
56 |
const found = props.employees.find(employee => editedUserId === employee.id); |
|
57 |
const foundPrevEdit = prevEdit.find(employee => editedUserId === employee.id); |
|
63 |
api.saveDataOverview(dataOverviewObject).catch(reason => { |
|
64 |
alert(reason); |
|
65 |
}); |
|
66 |
} |
|
58 | 67 |
|
59 |
const dataOverviewObject = { |
|
60 |
id: found.id, |
|
61 |
vacationCount: Number(found.holiday) - foundPrevEdit.holiday, |
|
62 |
sickDayCount: Number(found.sickday), |
|
63 |
role: found.role |
|
64 |
} |
|
68 |
function cancel() { |
|
69 |
props.setEmployees(prevEdit); |
|
70 |
setEdit(false); |
|
71 |
} |
|
65 | 72 |
|
66 |
api_fetch.saveDataOverview(dataOverviewObject).catch(reason => { |
|
67 |
alert(reason) |
|
68 |
}); |
|
69 |
} |
|
73 |
// ***** overview button ****** |
|
70 | 74 |
|
71 |
const cancel = () => {
|
|
72 |
props.setEmployees(prevEdit)
|
|
73 |
setEdit(false)
|
|
74 |
}
|
|
75 |
function editEmployee(employeeId, e) {
|
|
76 |
setEdit(true);
|
|
77 |
setEditedUserId(employeeId);
|
|
78 |
setPrevEdit(props.employees);
|
|
75 | 79 |
|
76 |
// ***** overview button ****** |
|
80 |
e.preventDefault(); |
|
81 |
} |
|
77 | 82 |
|
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> |
|
83 |
return ( |
|
84 |
<div> |
|
88 | 85 |
<div className="side-content"> |
89 |
<div className="side-board column">
|
|
90 |
<form className="column side-board-items" onSubmit={(e) => submitEdit(e)}>
|
|
86 |
<div className="side-board column"> |
|
87 |
<form className="column side-board-items" onSubmit={(e) => submitEdit(e)}> |
|
91 | 88 |
<div className="side-board-heading row"> |
92 |
<h3>Overview</h3>
|
|
89 |
<h3>Overview</h3> |
|
93 | 90 |
</div> |
94 | 91 |
<div className="underline-1"></div> |
95 | 92 |
<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> |
|
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>
|
|
104 | 101 |
{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> |
|
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>
|
|
133 | 130 |
))} |
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> |
|
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> |
|
146 | 144 |
</div> |
147 |
</div> |
|
148 |
</div> |
|
149 |
); |
|
150 |
} |
|
151 |
|
|
152 |
|
|
153 |
export default OverviewAdmin; |
|
145 |
</div> |
|
146 |
); |
|
147 |
} |
Také k dispozici: Unified diff
re #58 massive code formatting