1 |
ebfe6347
|
plundrichov
|
import React, { useState, useEffect } from 'react';
|
2 |
c46ffe2f
|
plundrichov
|
import './App.css';
|
3 |
ebfe6347
|
plundrichov
|
import moment from 'moment';
|
4 |
9f045397
|
plundrichov
|
import * as api_fetch from './api'
|
5 |
c46ffe2f
|
plundrichov
|
|
6 |
|
|
function UpcomingRequests(props) {
|
7 |
|
|
|
8 |
|
|
useEffect( () => {
|
9 |
|
|
getData();
|
10 |
|
|
}, []);
|
11 |
|
|
|
12 |
|
|
// get requests from server
|
13 |
|
|
const getData = async () => {
|
14 |
9f045397
|
plundrichov
|
|
15 |
|
|
api_fetch.loadAdminRequests().then((data) => {
|
16 |
|
|
props.setUser(data.map(request => {
|
17 |
c46ffe2f
|
plundrichov
|
|
18 |
9f045397
|
plundrichov
|
const a = request.date;
|
19 |
|
|
const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
|
20 |
|
|
|
21 |
|
|
return (
|
22 |
|
|
{
|
23 |
|
|
title: request.firstName + ' ' + request.lastName,
|
24 |
|
|
id: request.id,
|
25 |
ebfe6347
|
plundrichov
|
type: convertVacationType(request.type),
|
26 |
|
|
start: moment(b).format("D.M.YYYY"),
|
27 |
9f045397
|
plundrichov
|
end: null,
|
28 |
ebfe6347
|
plundrichov
|
status: request.status = request.status.toLowerCase()
|
29 |
9f045397
|
plundrichov
|
})
|
30 |
|
|
}))
|
31 |
|
|
}).catch(reason => {
|
32 |
|
|
alert(reason)
|
33 |
|
|
});
|
34 |
c46ffe2f
|
plundrichov
|
}
|
35 |
|
|
|
36 |
ebfe6347
|
plundrichov
|
function convertVacationType(vacationType) {
|
37 |
|
|
switch (vacationType) {
|
38 |
|
|
case 'SICK_DAY' :
|
39 |
|
|
return 'sickday';
|
40 |
a0cbde1e
|
plundrichov
|
default:
|
41 |
ebfe6347
|
plundrichov
|
return 'holiday';
|
42 |
|
|
}
|
43 |
|
|
}
|
44 |
|
|
|
45 |
9f045397
|
plundrichov
|
|
46 |
|
|
|
47 |
c46ffe2f
|
plundrichov
|
// send accepted request to server
|
48 |
|
|
const acceptRequest = async (user) => {
|
49 |
ebfe6347
|
plundrichov
|
try {
|
50 |
c46ffe2f
|
plundrichov
|
|
51 |
ebfe6347
|
plundrichov
|
const acceptedRequests = {
|
52 |
|
|
id: user.id,
|
53 |
|
|
status: 'ACCEPTED',
|
54 |
|
|
}
|
55 |
9f045397
|
plundrichov
|
|
56 |
ebfe6347
|
plundrichov
|
await api_fetch.sendAcceptedRequest(acceptedRequests).then(() => {
|
57 |
|
|
|
58 |
|
|
const userProps = {
|
59 |
|
|
title: user.title,
|
60 |
|
|
id: 0,
|
61 |
|
|
type: user.type,
|
62 |
9f045397
|
plundrichov
|
type: user.type,
|
63 |
ebfe6347
|
plundrichov
|
type: user.type,
|
64 |
|
|
start: user.start
|
65 |
|
|
}
|
66 |
|
|
//concat new request to current ones
|
67 |
1a58c244
|
plundrichov
|
props.setAcceptedRequest((acceptedRequest) => acceptedRequest.concat(userProps))
|
68 |
ebfe6347
|
plundrichov
|
//request accept button
|
69 |
9f045397
|
plundrichov
|
props.setUser((pendingRequest) => pendingRequest.filter((item) => item !== user));
|
70 |
ebfe6347
|
plundrichov
|
})
|
71 |
|
|
} catch (e) {
|
72 |
|
|
throw 'error catch GET DATA APP (getCurrentProfile)'
|
73 |
|
|
}
|
74 |
c46ffe2f
|
plundrichov
|
}
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
//send rejected request to server
|
78 |
|
|
const declineRequest = async (user) => {
|
79 |
ebfe6347
|
plundrichov
|
try{
|
80 |
c46ffe2f
|
plundrichov
|
|
81 |
ebfe6347
|
plundrichov
|
const rejectedRequest = {
|
82 |
|
|
id: user.id,
|
83 |
|
|
status: 'REJECTED',
|
84 |
|
|
}
|
85 |
9f045397
|
plundrichov
|
|
86 |
ebfe6347
|
plundrichov
|
await api_fetch.sendRejectedRequest(rejectedRequest);
|
87 |
|
|
|
88 |
9f045397
|
plundrichov
|
props.setUser((acceptedRequest) => acceptedRequest.filter((item) => item !== user))
|
89 |
ebfe6347
|
plundrichov
|
|
90 |
|
|
const usersOverview = await api_fetch.getUsersOverview();
|
91 |
|
|
props.setEmployees(usersOverview);
|
92 |
|
|
|
93 |
|
|
} catch (e) {
|
94 |
|
|
alert(e)
|
95 |
|
|
}
|
96 |
c46ffe2f
|
plundrichov
|
}
|
97 |
|
|
|
98 |
|
|
return (
|
99 |
|
|
<div className="offs-request column">
|
100 |
|
|
<h3>New Requests</h3>
|
101 |
|
|
<div className="underline-1"></div>
|
102 |
|
|
<div className="offs-items column">
|
103 |
|
|
<div className="offs-item row">
|
104 |
|
|
<table>
|
105 |
1a58c244
|
plundrichov
|
{/* {props.user.length > 0
|
106 |
ebfe6347
|
plundrichov
|
? */}
|
107 |
c46ffe2f
|
plundrichov
|
<tbody>
|
108 |
|
|
<tr>
|
109 |
|
|
<th>Name</th>
|
110 |
|
|
<th>Type</th>
|
111 |
|
|
<th>Date</th>
|
112 |
|
|
</tr>
|
113 |
1a58c244
|
plundrichov
|
{props.user.map(user => (
|
114 |
c46ffe2f
|
plundrichov
|
<tr>
|
115 |
|
|
<td>{user.title}</td>
|
116 |
|
|
<td>{user.type}</td>
|
117 |
|
|
<td>{user.end ? user.start + " - " + user.end : user.start}</td>
|
118 |
|
|
<div className="offs-btn row">
|
119 |
|
|
<button onClick={() => acceptRequest(user)} type="submit" className="btn btn-submit">Accept</button>
|
120 |
|
|
<button onClick={() => declineRequest(user)} type="submit" className="btn btn-cancel">Decline</button>
|
121 |
|
|
</div>
|
122 |
|
|
</tr>
|
123 |
|
|
))}
|
124 |
|
|
</tbody>
|
125 |
ebfe6347
|
plundrichov
|
{/* :
|
126 |
|
|
<tbody>
|
127 |
|
|
<p>There are no requests.</p>
|
128 |
|
|
</tbody>
|
129 |
|
|
} */}
|
130 |
|
|
|
131 |
c46ffe2f
|
plundrichov
|
</table>
|
132 |
|
|
</div>
|
133 |
|
|
</div>
|
134 |
|
|
</div>
|
135 |
|
|
)
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
export default UpcomingRequests;
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
// return (shouldRemoveThisItem === true) ? false : true;
|
144 |
|
|
//})
|
145 |
|
|
//)
|
146 |
|
|
// props.setUser((pendingRequest) => pendingRequest.filter(
|
147 |
|
|
|
148 |
|
|
// function(item) {
|
149 |
|
|
// const shouldRemoveThisItem = item === user;
|
150 |
|
|
|
151 |
|
|
// if (shouldRemoveThisItem === true) {
|
152 |
|
|
// return false;
|
153 |
|
|
// } else {
|
154 |
|
|
// return true;
|
155 |
|
|
// }})
|
156 |
|
|
// )
|