Revize 1865a0be
Přidáno uživatelem Pavel Fidransky před více než 4 roky(ů)
client/src/UpcomingRequests.js | ||
---|---|---|
1 |
import React, { useState, useEffect } from 'react';
|
|
1 |
import React, { useEffect } from 'react'; |
|
2 | 2 |
import './App.css'; |
3 | 3 |
import moment from 'moment'; |
4 |
import * as api_fetch from './api'
|
|
5 |
import convertVacationType from './convertVacationType' |
|
4 |
import * as api from './api';
|
|
5 |
import convertVacationType from './convertVacationType';
|
|
6 | 6 |
|
7 |
function UpcomingRequests(props) { |
|
7 |
export default function UpcomingRequests(props) {
|
|
8 | 8 |
|
9 |
useEffect( () => {
|
|
9 |
useEffect(() => { |
|
10 | 10 |
getData(); |
11 |
}, []); |
|
11 |
}, []); // eslint-disable-line
|
|
12 | 12 |
|
13 | 13 |
// get requests from server |
14 |
const getData = async () => { |
|
15 |
|
|
16 |
api_fetch.loadAdminRequests().then((data) => { |
|
14 |
async function getData() { |
|
15 |
api.loadAdminRequests().then((data) => { |
|
17 | 16 |
props.setUser(data.map(request => { |
17 |
const convertedStartDate = request.date.split('/').join('-'); |
|
18 | 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 |
})) |
|
19 |
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 |
})); |
|
31 | 28 |
}).catch(reason => { |
32 |
alert(reason) |
|
29 |
alert(reason);
|
|
33 | 30 |
}); |
34 |
} |
|
35 |
|
|
31 |
} |
|
36 | 32 |
|
37 | 33 |
// send accepted request to server |
38 |
const acceptRequest = async (user) => {
|
|
34 |
async function acceptRequest(user) {
|
|
39 | 35 |
try { |
40 |
|
|
41 | 36 |
const acceptedRequests = { |
42 | 37 |
id: user.id, |
43 | 38 |
status: 'ACCEPTED', |
44 |
} |
|
45 |
|
|
46 |
await api_fetch.sendAcceptedRequest(acceptedRequests).then(() => { |
|
47 |
|
|
39 |
}; |
|
40 |
|
|
41 |
await api.sendAcceptedRequest(acceptedRequests).then(() => { |
|
48 | 42 |
const userProps = { |
49 |
title: user.title,
|
|
50 |
type: user.type,
|
|
51 |
start: user.start
|
|
52 |
} |
|
43 |
title: user.title, |
|
44 |
type: user.type,
|
|
45 |
start: user.start,
|
|
46 |
};
|
|
53 | 47 |
//concat new request to current ones |
54 |
props.setAcceptedRequest((acceptedRequest) => acceptedRequest.concat(userProps))
|
|
48 |
props.setAcceptedRequest((acceptedRequest) => acceptedRequest.concat(userProps));
|
|
55 | 49 |
//request accept button |
56 |
props.setUser((pendingRequest) => pendingRequest.filter((item) => item !== user));
|
|
57 |
}) |
|
50 |
props.setUser((pendingRequest) => pendingRequest.filter((item) => item !== user)); |
|
51 |
});
|
|
58 | 52 |
} catch (e) { |
59 |
alert(e) |
|
53 |
alert(e);
|
|
60 | 54 |
} |
61 | 55 |
} |
62 | 56 |
|
63 |
|
|
64 | 57 |
//send rejected request to server |
65 |
const declineRequest = async (user) => { |
|
66 |
try{ |
|
67 |
|
|
58 |
async function declineRequest(user) { |
|
59 |
try { |
|
68 | 60 |
const rejectedRequest = { |
69 | 61 |
id: user.id, |
70 | 62 |
status: 'REJECTED', |
71 |
} |
|
63 |
}; |
|
64 |
|
|
65 |
await api.sendRejectedRequest(rejectedRequest); |
|
72 | 66 |
|
73 |
await api_fetch.sendRejectedRequest(rejectedRequest); |
|
74 |
|
|
75 |
props.setUser((acceptedRequest) => acceptedRequest.filter((item) => item !== user)) |
|
67 |
props.setUser((acceptedRequest) => acceptedRequest.filter((item) => item !== user)); |
|
76 | 68 |
|
77 |
const usersOverview = await api_fetch.getUsersOverview();
|
|
69 |
const usersOverview = await api.getUsersOverview(); |
|
78 | 70 |
props.setEmployees(usersOverview); |
79 | 71 |
|
80 |
} catch (e) { |
|
81 |
alert(e)
|
|
72 |
} catch (e) {
|
|
73 |
alert(e);
|
|
82 | 74 |
} |
83 | 75 |
} |
84 | 76 |
|
... | ... | |
89 | 81 |
<div className="offs-items column"> |
90 | 82 |
<div className="offs-item row"> |
91 | 83 |
<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> |
|
84 |
{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> |
|
116 | 109 |
</div> |
117 | 110 |
</div> |
118 | 111 |
</div> |
119 |
) |
|
120 |
} |
|
121 |
|
|
122 |
export default UpcomingRequests; |
|
112 |
); |
|
113 |
} |
Také k dispozici: Unified diff
re #58 massive code formatting