Revize 9f045397
Přidáno uživatelem plundrichov před více než 4 roky(ů)
client/src/OverviewAdmin.js | ||
---|---|---|
1 | 1 |
import React, { useState, useEffect } from 'react'; |
2 | 2 |
import './App.css'; |
3 |
import * as api_fetch from './api' |
|
3 | 4 |
|
4 | 5 |
|
5 | 6 |
const OverviewAdmin = () => { |
6 | 7 |
|
7 |
useEffect( () => { |
|
8 |
getData(); |
|
8 |
useEffect(() => { |
|
9 |
api_fetch.getUsersOverview().then(usersOverview => { |
|
10 |
setEmployees(usersOverview); |
|
11 |
}).catch(reason => { |
|
12 |
alert(reason) |
|
13 |
}); |
|
9 | 14 |
}, []); |
10 | 15 |
|
11 |
const getData = async () => { |
|
12 |
try { |
|
13 |
const response = await fetch ( |
|
14 |
'http://devcz.yoso.fi:8090/ymanager/users', { |
|
15 |
headers: { |
|
16 |
Authorization: 1 } |
|
17 |
} |
|
18 |
); |
|
19 |
|
|
20 |
if (response.ok) { |
|
21 |
|
|
22 |
const data = await response.json(); |
|
23 |
setEmployees(data.map(user => { |
|
24 |
|
|
25 |
return ( |
|
26 |
{ |
|
27 |
name: user.firstName + ' ' + user.lastName, |
|
28 |
id: user.id, |
|
29 |
sickday: user.sickDayCount, |
|
30 |
holiday: user.vacationCount, |
|
31 |
role: user.role |
|
32 |
}) |
|
33 |
})) |
|
34 |
} else { |
|
35 |
if(response.status === 400) { |
|
36 |
alert('error 400 GET DATA (OVERVIEW, EMPLOYER)') |
|
37 |
} |
|
38 |
else if (response.status === 500) { |
|
39 |
alert ('error 500 GET DATA (OVERVIEW, EMPLOYER)') |
|
40 |
} |
|
41 |
else { |
|
42 |
alert('error GET DATA (OVERVIEW, EMPLOYER)') |
|
43 |
} |
|
44 |
} |
|
45 |
} catch (e) { |
|
46 |
console.log(e) |
|
47 |
alert('spatne') |
|
48 |
} |
|
49 |
} |
|
50 |
|
|
16 |
// states |
|
51 | 17 |
const [employees, setEmployees] = useState([ |
52 | 18 |
{ |
53 | 19 |
name: 'Sadam', |
... | ... | |
59 | 25 |
|
60 | 26 |
const [isEdit, setEdit] = useState(false); |
61 | 27 |
const [editedUserId, setEditedUserId] = useState(); |
62 |
|
|
63 | 28 |
const [prevEdit, setPrevEdit] = useState(); |
29 |
|
|
64 | 30 |
|
31 |
// functions |
|
65 | 32 |
function changeSickdays(newValue) { |
66 | 33 |
const newEmployees = employees.map(employee => { |
67 | 34 |
if (editedUserId === employee.id) { |
... | ... | |
91 | 58 |
} |
92 | 59 |
|
93 | 60 |
const submitEdit = async (e) => { |
94 |
try { |
|
61 |
|
|
95 | 62 |
setEdit(isEdit === true ? false : true); |
96 | 63 |
setPrevEdit(employees); |
97 | 64 |
e.preventDefault(); |
98 | 65 |
|
99 | 66 |
const found = employees.find(employee => editedUserId === employee.id); |
100 | 67 |
const foundPrevEdit = prevEdit.find(employee => editedUserId === employee.id); |
101 |
console.log(found) |
|
102 |
|
|
103 |
|
|
104 |
// send accepted request to server |
|
105 |
const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/settings', { |
|
106 |
headers: { |
|
107 |
Authorization: 1, |
|
108 |
'Content-Type': 'application/json', |
|
109 |
}, |
|
110 |
method: 'PUT', |
|
111 |
|
|
112 |
// object which is sent to server |
|
113 |
body: JSON.stringify({ |
|
114 |
id: found.id, |
|
115 |
vacationCount: Number(found.holiday) - foundPrevEdit.holiday, |
|
116 |
sickDayCount: Number(found.sickday), |
|
117 |
role: found.role |
|
118 |
}), |
|
119 |
}); |
|
120 |
console.log(response.status) |
|
121 |
if (response.status === 400) { |
|
122 |
alert('error 400 SAVE DATA (OVERVIEW, EMPLOYER)') |
|
123 |
} |
|
124 |
else if (response.status === 500) { |
|
125 |
alert('error 500 SAVE DATA (OVERVIEW, EMPLOYER)') |
|
126 |
} |
|
127 |
else if (!response.ok) { |
|
128 |
alert('error SAVE DATA (OVERVIEW, EMPLOYER)') |
|
129 |
} |
|
130 |
|
|
131 |
} catch (e) { |
|
132 |
alert('error catch SAVE DATA (OVERVIEW, EMPLOYER') |
|
68 |
|
|
69 |
const dataOverviewObject = { |
|
70 |
id: found.id, |
|
71 |
vacationCount: Number(found.holiday) - foundPrevEdit.holiday, |
|
72 |
sickDayCount: Number(found.sickday), |
|
73 |
role: found.role |
|
133 | 74 |
} |
134 |
} |
|
135 | 75 |
|
76 |
api_fetch.saveDataOverview(dataOverviewObject).catch(reason => { |
|
77 |
alert(reason) |
|
78 |
}); |
|
79 |
} |
|
136 | 80 |
|
137 | 81 |
const cancel = () => { |
138 | 82 |
setEmployees(prevEdit) |
... | ... | |
148 | 92 |
|
149 | 93 |
e.preventDefault(); |
150 | 94 |
} |
151 |
|
|
152 |
console.log(isEdit, editedUserId) |
|
153 | 95 |
|
154 | 96 |
return ( |
155 | 97 |
<div> |
Také k dispozici: Unified diff
re #58 Refactoring, every fetch moved to api.js