Revize 9f045397
Přidáno uživatelem plundrichov před více než 4 roky(ů)
client/src/UpcomingRequests.js | ||
---|---|---|
1 | 1 |
import React, { useEffect } from 'react'; |
2 | 2 |
import './App.css'; |
3 |
|
|
3 |
import * as api_fetch from './api' |
|
4 | 4 |
|
5 | 5 |
function UpcomingRequests(props) { |
6 | 6 |
|
... | ... | |
10 | 10 |
|
11 | 11 |
// get requests from server |
12 | 12 |
const getData = async () => { |
13 |
try { |
|
14 |
const response = await fetch( |
|
15 |
'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', { |
|
16 |
headers: { |
|
17 |
Authorization: 1 |
|
18 |
} |
|
19 |
}, |
|
20 |
); |
|
21 |
|
|
22 |
if (response.ok) { |
|
23 |
const data = await response.json(); |
|
24 |
props.setUser(data.map(request => { |
|
13 |
|
|
14 |
api_fetch.loadAdminRequests().then((data) => { |
|
15 |
props.setUser(data.map(request => { |
|
25 | 16 |
|
26 |
const a = request.date; |
|
27 |
const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join(''); |
|
28 |
|
|
29 |
return ( |
|
30 |
{ |
|
31 |
title: request.firstName + ' ' + request.lastName, |
|
32 |
id: request.id, |
|
33 |
type: request.type, |
|
34 |
start: b, |
|
35 |
end: null, |
|
36 |
status: request.status |
|
37 |
}) |
|
38 |
})) |
|
39 |
} else { |
|
40 |
if(response.status === 400) { |
|
41 |
alert('error 400 GET DATA (UPCOMING REQUESTS)') |
|
42 |
} |
|
43 |
else if (response.status === 500) { |
|
44 |
alert ('error 500 GET DATA (UPCOMING REQUESTS)') |
|
45 |
} |
|
46 |
else { |
|
47 |
alert('error GET DATA (UPCOMING REQUESTS)') |
|
48 |
} |
|
49 |
} |
|
50 |
} catch (e) { |
|
51 |
console.log(e) |
|
52 |
alert('error catch GET DATA (UPCOMING REQUESTS)') |
|
53 |
} |
|
17 |
const a = request.date; |
|
18 |
const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join(''); |
|
19 |
|
|
20 |
return ( |
|
21 |
{ |
|
22 |
title: request.firstName + ' ' + request.lastName, |
|
23 |
id: request.id, |
|
24 |
type: request.type, |
|
25 |
start: b, |
|
26 |
end: null, |
|
27 |
status: request.status |
|
28 |
}) |
|
29 |
})) |
|
30 |
}).catch(reason => { |
|
31 |
alert(reason) |
|
32 |
}); |
|
54 | 33 |
} |
55 | 34 |
|
35 |
|
|
36 |
|
|
56 | 37 |
// send accepted request to server |
57 | 38 |
const acceptRequest = async (user) => { |
58 |
try { |
|
59 |
const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', { |
|
60 |
headers: { |
|
61 |
Authorization: 1, |
|
62 |
'Content-Type': 'application/json', |
|
63 |
}, |
|
64 |
method: 'PUT', |
|
65 |
body: JSON.stringify({ |
|
66 |
id: user.id, |
|
67 |
status: 'ACCEPTED', |
|
68 |
}), |
|
69 |
}); |
|
70 | 39 |
|
71 |
if (response.ok) { |
|
72 |
const userProps = { |
|
73 |
title: user.title, |
|
74 |
id: 0, |
|
75 |
type: user.type, |
|
76 |
start: user.start |
|
77 |
} |
|
78 |
//concat new request to current ones |
|
79 |
props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps)) |
|
80 |
//request accept button |
|
81 |
props.setUser((pendingRequest) => pendingRequest.filter((item) => item !== user)); |
|
82 |
|
|
83 |
} else { |
|
84 |
if(response.status === 400) { |
|
85 |
alert('error 400 SEND ACCEPTED DATA (UPCOMING REQUESTS)') |
|
86 |
} |
|
87 |
else if (response.status === 500) { |
|
88 |
alert ('error 500 SEND ACCEPTED DATA (UPCOMING REQUESTS)') |
|
89 |
} |
|
90 |
else { |
|
91 |
alert('error SEND ACCEPTED DATA (UPCOMING REQUESTS)') |
|
92 |
} |
|
93 |
} |
|
94 |
} catch (e) { |
|
95 |
alert('error catch SEND ACCEPTED DATA (UPCOMING REQUESTS)') |
|
40 |
const acceptedRequests = { |
|
41 |
id: user.id, |
|
42 |
status: 'ACCEPTED', |
|
96 | 43 |
} |
44 |
|
|
45 |
api_fetch.sendAcceptedRequest(acceptedRequests).then((data) => { |
|
46 |
|
|
47 |
const userProps = { |
|
48 |
title: user.title, |
|
49 |
id: 0, |
|
50 |
type: user.type, |
|
51 |
start: user.start |
|
52 |
} |
|
53 |
//concat new request to current ones |
|
54 |
props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps)) |
|
55 |
//request accept button |
|
56 |
props.setUser((pendingRequest) => pendingRequest.filter((item) => item !== user)); |
|
57 |
}).catch(response => { |
|
58 |
alert(response) |
|
59 |
}) |
|
97 | 60 |
} |
98 | 61 |
|
62 |
|
|
63 |
|
|
99 | 64 |
//send rejected request to server |
100 | 65 |
const declineRequest = async (user) => { |
101 |
try { |
|
102 |
const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', { |
|
103 |
headers: { |
|
104 |
Authorization: 1, |
|
105 |
'Content-Type': 'application/json', |
|
106 |
}, |
|
107 |
method: 'PUT', |
|
108 |
body: JSON.stringify({ |
|
109 |
id: user.id, |
|
110 |
status: 'REJECTED', |
|
111 |
}), |
|
112 |
}); |
|
113 |
|
|
114 |
if (response.ok) { |
|
115 |
//request cancel button |
|
116 |
props.setUser((acceptedRequest) => acceptedRequest.filter((item) => item !== user)) |
|
117 | 66 |
|
118 |
} else { |
|
119 |
if(response.status === 400) { |
|
120 |
alert('error 400 SEND REJECTED DATA (UPCOMING REQUESTS)') |
|
121 |
} |
|
122 |
else if (response.status === 500) { |
|
123 |
alert ('error 500 SEND REJECTED DATA (UPCOMING REQUESTS)') |
|
124 |
} |
|
125 |
else { |
|
126 |
alert('error SEND REJECTED DATA (UPCOMING REQUESTS)') |
|
127 |
} |
|
128 |
} |
|
129 |
} catch (e) { |
|
130 |
console.log(e) |
|
131 |
alert('error catch SEND REJECTED DATA (UPCOMING REQUESTS)') |
|
67 |
const rejectedRequest = { |
|
68 |
id: user.id, |
|
69 |
status: 'REJECTED', |
|
132 | 70 |
} |
71 |
|
|
72 |
api_fetch.sendRejectedRequest(rejectedRequest).then((data) => { |
|
73 |
//request cancel button |
|
74 |
props.setUser((acceptedRequest) => acceptedRequest.filter((item) => item !== user)) |
|
75 |
}).catch(reason => { |
|
76 |
alert(reason) |
|
77 |
}); |
|
133 | 78 |
} |
134 | 79 |
|
135 | 80 |
return ( |
Také k dispozici: Unified diff
re #58 Refactoring, every fetch moved to api.js