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 |
5bedee9e
|
plundrichov
|
import convertVacationType from './convertVacationType'
|
6 |
c46ffe2f
|
plundrichov
|
|
7 |
|
|
function UpcomingRequests(props) {
|
8 |
|
|
|
9 |
|
|
useEffect( () => {
|
10 |
|
|
getData();
|
11 |
|
|
}, []);
|
12 |
|
|
|
13 |
|
|
// get requests from server
|
14 |
|
|
const getData = async () => {
|
15 |
9f045397
|
plundrichov
|
|
16 |
|
|
api_fetch.loadAdminRequests().then((data) => {
|
17 |
|
|
props.setUser(data.map(request => {
|
18 |
5bedee9e
|
plundrichov
|
|
19 |
|
|
const convertedStartDate = request.date.split("/").join("-");
|
20 |
9f045397
|
plundrichov
|
|
21 |
|
|
return (
|
22 |
|
|
{
|
23 |
|
|
title: request.firstName + ' ' + request.lastName,
|
24 |
|
|
id: request.id,
|
25 |
ebfe6347
|
plundrichov
|
type: convertVacationType(request.type),
|
26 |
5bedee9e
|
plundrichov
|
start: moment(convertedStartDate).format("D.M.YYYY"),
|
27 |
9f045397
|
plundrichov
|
end: null,
|
28 |
bd782af2
|
plundrichov
|
status: request.status.toLowerCase()
|
29 |
9f045397
|
plundrichov
|
})
|
30 |
|
|
}))
|
31 |
|
|
}).catch(reason => {
|
32 |
|
|
alert(reason)
|
33 |
|
|
});
|
34 |
c46ffe2f
|
plundrichov
|
}
|
35 |
|
|
|
36 |
9f045397
|
plundrichov
|
|
37 |
c46ffe2f
|
plundrichov
|
// send accepted request to server
|
38 |
|
|
const acceptRequest = async (user) => {
|
39 |
ebfe6347
|
plundrichov
|
try {
|
40 |
c46ffe2f
|
plundrichov
|
|
41 |
ebfe6347
|
plundrichov
|
const acceptedRequests = {
|
42 |
|
|
id: user.id,
|
43 |
|
|
status: 'ACCEPTED',
|
44 |
|
|
}
|
45 |
9f045397
|
plundrichov
|
|
46 |
ebfe6347
|
plundrichov
|
await api_fetch.sendAcceptedRequest(acceptedRequests).then(() => {
|
47 |
|
|
|
48 |
|
|
const userProps = {
|
49 |
|
|
title: user.title,
|
50 |
|
|
type: user.type,
|
51 |
|
|
start: user.start
|
52 |
|
|
}
|
53 |
|
|
//concat new request to current ones
|
54 |
1a58c244
|
plundrichov
|
props.setAcceptedRequest((acceptedRequest) => acceptedRequest.concat(userProps))
|
55 |
ebfe6347
|
plundrichov
|
//request accept button
|
56 |
9f045397
|
plundrichov
|
props.setUser((pendingRequest) => pendingRequest.filter((item) => item !== user));
|
57 |
ebfe6347
|
plundrichov
|
})
|
58 |
|
|
} catch (e) {
|
59 |
5bedee9e
|
plundrichov
|
alert(e)
|
60 |
ebfe6347
|
plundrichov
|
}
|
61 |
c46ffe2f
|
plundrichov
|
}
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
//send rejected request to server
|
65 |
|
|
const declineRequest = async (user) => {
|
66 |
ebfe6347
|
plundrichov
|
try{
|
67 |
c46ffe2f
|
plundrichov
|
|
68 |
ebfe6347
|
plundrichov
|
const rejectedRequest = {
|
69 |
|
|
id: user.id,
|
70 |
|
|
status: 'REJECTED',
|
71 |
|
|
}
|
72 |
9f045397
|
plundrichov
|
|
73 |
ebfe6347
|
plundrichov
|
await api_fetch.sendRejectedRequest(rejectedRequest);
|
74 |
|
|
|
75 |
9f045397
|
plundrichov
|
props.setUser((acceptedRequest) => acceptedRequest.filter((item) => item !== user))
|
76 |
ebfe6347
|
plundrichov
|
|
77 |
|
|
const usersOverview = await api_fetch.getUsersOverview();
|
78 |
|
|
props.setEmployees(usersOverview);
|
79 |
|
|
|
80 |
|
|
} catch (e) {
|
81 |
|
|
alert(e)
|
82 |
|
|
}
|
83 |
c46ffe2f
|
plundrichov
|
}
|
84 |
|
|
|
85 |
|
|
return (
|
86 |
|
|
<div className="offs-request column">
|
87 |
|
|
<h3>New Requests</h3>
|
88 |
|
|
<div className="underline-1"></div>
|
89 |
|
|
<div className="offs-items column">
|
90 |
|
|
<div className="offs-item row">
|
91 |
|
|
<table>
|
92 |
6b95d204
|
plundrichov
|
{props.user.length > 0 ?
|
93 |
c46ffe2f
|
plundrichov
|
<tbody>
|
94 |
|
|
<tr>
|
95 |
|
|
<th>Name</th>
|
96 |
|
|
<th>Type</th>
|
97 |
|
|
<th>Date</th>
|
98 |
|
|
</tr>
|
99 |
1a58c244
|
plundrichov
|
{props.user.map(user => (
|
100 |
c46ffe2f
|
plundrichov
|
<tr>
|
101 |
|
|
<td>{user.title}</td>
|
102 |
|
|
<td>{user.type}</td>
|
103 |
|
|
<td>{user.end ? user.start + " - " + user.end : user.start}</td>
|
104 |
|
|
<div className="offs-btn row">
|
105 |
|
|
<button onClick={() => acceptRequest(user)} type="submit" className="btn btn-submit">Accept</button>
|
106 |
|
|
<button onClick={() => declineRequest(user)} type="submit" className="btn btn-cancel">Decline</button>
|
107 |
|
|
</div>
|
108 |
|
|
</tr>
|
109 |
|
|
))}
|
110 |
|
|
</tbody>
|
111 |
5bedee9e
|
plundrichov
|
:
|
112 |
ebfe6347
|
plundrichov
|
<tbody>
|
113 |
|
|
<p>There are no requests.</p>
|
114 |
6b95d204
|
plundrichov
|
</tbody>}
|
115 |
5bedee9e
|
plundrichov
|
</table>
|
116 |
c46ffe2f
|
plundrichov
|
</div>
|
117 |
|
|
</div>
|
118 |
|
|
</div>
|
119 |
|
|
)
|
120 |
|
|
}
|
121 |
|
|
|
122 |
5bedee9e
|
plundrichov
|
export default UpcomingRequests;
|