1
|
import React, { useState, useEffect } from 'react';
|
2
|
import './App.css';
|
3
|
import moment from 'moment';
|
4
|
import * as api_fetch from './api'
|
5
|
|
6
|
function UpcomingRequests(props) {
|
7
|
|
8
|
useEffect( () => {
|
9
|
getData();
|
10
|
}, []);
|
11
|
|
12
|
// get requests from server
|
13
|
const getData = async () => {
|
14
|
|
15
|
api_fetch.loadAdminRequests().then((data) => {
|
16
|
props.setUser(data.map(request => {
|
17
|
|
18
|
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
|
type: convertVacationType(request.type),
|
26
|
start: moment(b).format("D.M.YYYY"),
|
27
|
end: null,
|
28
|
status: request.status = request.status.toLowerCase()
|
29
|
})
|
30
|
}))
|
31
|
}).catch(reason => {
|
32
|
alert(reason)
|
33
|
});
|
34
|
}
|
35
|
|
36
|
function convertVacationType(vacationType) {
|
37
|
switch (vacationType) {
|
38
|
case 'SICK_DAY' :
|
39
|
return 'sickday';
|
40
|
case 'HOLIDAY':
|
41
|
return 'holiday';
|
42
|
}
|
43
|
}
|
44
|
|
45
|
|
46
|
|
47
|
// send accepted request to server
|
48
|
const acceptRequest = async (user) => {
|
49
|
try {
|
50
|
|
51
|
const acceptedRequests = {
|
52
|
id: user.id,
|
53
|
status: 'ACCEPTED',
|
54
|
}
|
55
|
|
56
|
await api_fetch.sendAcceptedRequest(acceptedRequests).then(() => {
|
57
|
|
58
|
const userProps = {
|
59
|
title: user.title,
|
60
|
id: 0,
|
61
|
type: user.type,
|
62
|
type: user.type,
|
63
|
type: user.type,
|
64
|
start: user.start
|
65
|
}
|
66
|
//concat new request to current ones
|
67
|
props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
|
68
|
//request accept button
|
69
|
props.setUser((pendingRequest) => pendingRequest.filter((item) => item !== user));
|
70
|
})
|
71
|
} catch (e) {
|
72
|
throw 'error catch GET DATA APP (getCurrentProfile)'
|
73
|
}
|
74
|
}
|
75
|
|
76
|
|
77
|
//send rejected request to server
|
78
|
const declineRequest = async (user) => {
|
79
|
try{
|
80
|
|
81
|
const rejectedRequest = {
|
82
|
id: user.id,
|
83
|
status: 'REJECTED',
|
84
|
}
|
85
|
|
86
|
await api_fetch.sendRejectedRequest(rejectedRequest);
|
87
|
|
88
|
props.setUser((acceptedRequest) => acceptedRequest.filter((item) => item !== user))
|
89
|
|
90
|
const usersOverview = await api_fetch.getUsersOverview();
|
91
|
props.setEmployees(usersOverview);
|
92
|
|
93
|
} catch (e) {
|
94
|
alert(e)
|
95
|
}
|
96
|
}
|
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
|
{/* {props.userRequest.length > 0
|
106
|
? */}
|
107
|
<tbody>
|
108
|
<tr>
|
109
|
<th>Name</th>
|
110
|
<th>Type</th>
|
111
|
<th>Date</th>
|
112
|
</tr>
|
113
|
{props.userRequest.map(user => (
|
114
|
<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
|
{/* :
|
126
|
<tbody>
|
127
|
<p>There are no requests.</p>
|
128
|
</tbody>
|
129
|
} */}
|
130
|
|
131
|
</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
|
// )
|