1
|
import React, { useState, useEffect } from 'react';
|
2
|
import './App.css';
|
3
|
import moment from 'moment';
|
4
|
import * as api_fetch from './api'
|
5
|
import convertVacationType from './convertVacationType'
|
6
|
|
7
|
function UpcomingRequests(props) {
|
8
|
|
9
|
useEffect( () => {
|
10
|
getData();
|
11
|
}, []);
|
12
|
|
13
|
// get requests from server
|
14
|
const getData = async () => {
|
15
|
|
16
|
api_fetch.loadAdminRequests().then((data) => {
|
17
|
props.setUser(data.map(request => {
|
18
|
|
19
|
const convertedStartDate = request.date.split("/").join("-");
|
20
|
|
21
|
return (
|
22
|
{
|
23
|
title: request.firstName + ' ' + request.lastName,
|
24
|
id: request.id,
|
25
|
type: convertVacationType(request.type),
|
26
|
start: moment(convertedStartDate).format("D.M.YYYY"),
|
27
|
end: null,
|
28
|
status: request.status.toLowerCase()
|
29
|
})
|
30
|
}))
|
31
|
}).catch(reason => {
|
32
|
alert(reason)
|
33
|
});
|
34
|
}
|
35
|
|
36
|
|
37
|
// send accepted request to server
|
38
|
const acceptRequest = async (user) => {
|
39
|
try {
|
40
|
|
41
|
const acceptedRequests = {
|
42
|
id: user.id,
|
43
|
status: 'ACCEPTED',
|
44
|
}
|
45
|
|
46
|
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
|
props.setAcceptedRequest((acceptedRequest) => acceptedRequest.concat(userProps))
|
55
|
//request accept button
|
56
|
props.setUser((pendingRequest) => pendingRequest.filter((item) => item !== user));
|
57
|
})
|
58
|
} catch (e) {
|
59
|
alert(e)
|
60
|
}
|
61
|
}
|
62
|
|
63
|
|
64
|
//send rejected request to server
|
65
|
const declineRequest = async (user) => {
|
66
|
try{
|
67
|
|
68
|
const rejectedRequest = {
|
69
|
id: user.id,
|
70
|
status: 'REJECTED',
|
71
|
}
|
72
|
|
73
|
await api_fetch.sendRejectedRequest(rejectedRequest);
|
74
|
|
75
|
props.setUser((acceptedRequest) => acceptedRequest.filter((item) => item !== user))
|
76
|
|
77
|
const usersOverview = await api_fetch.getUsersOverview();
|
78
|
props.setEmployees(usersOverview);
|
79
|
|
80
|
} catch (e) {
|
81
|
alert(e)
|
82
|
}
|
83
|
}
|
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
|
{props.user.length > 0 ?
|
93
|
<tbody>
|
94
|
<tr>
|
95
|
<th>Name</th>
|
96
|
<th>Type</th>
|
97
|
<th>Date</th>
|
98
|
</tr>
|
99
|
{props.user.map(user => (
|
100
|
<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
|
:
|
112
|
<tbody>
|
113
|
<p>There are no requests.</p>
|
114
|
</tbody>}
|
115
|
</table>
|
116
|
</div>
|
117
|
</div>
|
118
|
</div>
|
119
|
)
|
120
|
}
|
121
|
|
122
|
export default UpcomingRequests;
|