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