Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 7495b9eb

Přidáno uživatelem plundrichov před více než 4 roky(ů)

re #58 refactoring (api - app, calendar)

Zobrazit rozdíly:

client/src/App.js
12 12
// import Komponenta1 from './Komponenta1';
13 13
// import Komponenta2 from './Komponenta2';
14 14
import { BrowserRouter, Route, Switch } from "react-router-dom";
15
import * as api_fetch from './api'
15 16

  
16 17
function App() {
17 18

  
18
  useEffect( () => {
19
    getData();
19
  useEffect(() => {
20
    api_fetch.getCurrentProfile().then(currentProfile => {
21
    setUserName(currentProfile);
22
    });
20 23
  }, []);
21 24

  
22
  const getData = async () => {
23
    try {
24
    const response = await fetch(
25
      'http://devcz.yoso.fi:8090/ymanager/users/current/profile', {
26
        headers: {
27
          Authorization: 1
28
        }
29
      }
30
    );
31

  
32
    if (response.ok) {
33
    const data = await response.json();
34
    setUserName({
35
      name: data.firstName + ' ' + data.lastName,
36
      role: data.role,
37
      id: data.id,
38
      holiday: data.vacationCount,
39
      sickday: data.sickDayCount
40
    })
41
  } else {
42
    if(response.status === 400) {
43
      alert('error 400 GET DATA APP')
44
   }
45
      else if (response.status === 500) {
46
         alert ('error 500 GET DATA APP')
47
      }
48
      else {
49
         alert('error GET DATA APP')
50
      }
51
  }
52
} catch (e) {
53
  console.log(e)
54
  alert('error catch GET DATA APP')
55
  }
56
}
57 25

  
58 26
  const [userName, setUserName] = useState([
59 27
    {
client/src/Calendar.js
7 7
import interactionPlugin from '@fullcalendar/interaction';
8 8
import Popup from "reactjs-popup";
9 9
import moment from 'moment';
10
import * as api_fetch from './api'
10 11

  
11 12

  
12 13
function Calendar(props) {
13 14
  
14 15
  useEffect( () => {
15 16
    if (props.userName.id !== undefined) {
16
      props.userName.role === 'EMPLOYER' ? getDataAdmin() : getData();
17
      props.userName.role === 'EMPLOYER'
18
        ?
19
          api_fetch.getAdminCalendar().then(adminCalendar => {
20
            props.setRequest(adminCalendar)
21
          })
22
        :
23
          api_fetch.getUserCalendar(props.userName, todayTwo).then(userCalendar => {
24
            props.setRequest(userCalendar)
25
          });
17 26
    }
18 27
  }, [props.userName.id]);
19 28

  
20 29
  // LOAD DATA from server to calendar **** EMPLOYEE ****
21
  const getData = async () => {
22
    try {
23
    const response = await fetch(
24
      `http://devcz.yoso.fi:8090/ymanager/user/${props.userName.id}/calendar?from=${todayTwo}&status=ACCEPTED&status=REJECTED`, {
25
        headers: {
26
          'Accept': 'application/json',
27
          Authorization: 6
28
        },
29
        method: 'GET',
30
      }
31
    );
32

  
33
    if (response.ok) {
34
    const data = await response.json();
35
    
36
    props.setRequest(data.filter(day => {
37
      return day.status !== 'PENDING'
38
    }).map(day => {
39

  
40
      const newDate = day.date.split("/").join("-");
41

  
42
      return ({
43
      title: props.userName.name,
44
      start: newDate,
45
      backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
46
      })
47
    }))
48
  } else {
49
    if(response.status === 400) {
50
      alert('error 400 LOADING DATA (CALENDAR, EMPLOYEE)')
51
   }
52
      else if (response.status === 500) {
53
         alert ('error 500 LOADING DATA (CALENDAR, EMPLOYEE)')
54
      }
55
      else {
56
         alert('error LOADING DATA (CALENDAR, EMPLOYEE)')
57
      }
58
  }
59
  } catch (e) {
60
    alert('error catch LOADING DATA (CALENDAR, EMPLOYEE)')
61
  }
62
  }
30
  
63 31
  // LOAD DATA from server to calendar **** EMPLOYER ****
64
  const getDataAdmin = async () => {
65
    try {
66
    const response = await fetch(
67
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=ACCEPTED', {
68
        headers: {
69
          'Accept': 'application/json',
70
          Authorization: 1
71
        },
72
        method: 'GET',
73
      }
74
    );
75

  
76
    if (response.ok) {
77
    const data = await response.json();
78
    
79
    props.setRequest(data.map(day => {
80

  
81
      const newDate = day.date.split("/").join("-");
82

  
83
      return ( {
84
      title: day.firstName + ' ' + day.lastName,
85
      start: newDate
86
      })
87
    }))
88
  } else {
89
    if(response.status === 400) {
90
      alert('error 400 LOADING DATA (CALENDAR, EMPLOYER)')
91
   }
92
      else if (response.status === 500) {
93
         alert ('error 500 LOADING DATA (CALENDAR, EMPLOYER))')
94
      }
95
      else {
96
         alert('error LOADING DATA (CALENDAR, EMPLOYER)')
97
      }
98
  }
99
  } catch (e) {
100
    alert('error catch LOADING DATA (CALENDAR, EMPLOYER)')
101
  }
102
}
103

  
104 32

  
105 33
  //states
106 34
  const [isOpen, setOpen] = useState(false)
......
117 45
  today = today.toISOString().split('T')[0]
118 46
  const todayTwo = today.split("-").join("/")
119 47

  
120

  
121 48
// ********************* ADD EVENT - EMPLOYEE **************************
122 49

  
123
  const addEvent = async (e) => {
124
    e.preventDefault();
50
  
51
// ********************* ADD EVENT ADMIN - EMPLOYER **************************
52

  
53
const addEventAdmin = async (e) => {
54
  e.preventDefault();
125 55

  
126
  // setting an object
127
    const newDate = whatDate.split("-").join("/");
128
      
129
    try {
130
  // send accepted request to server
131
      const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
132
        headers: {
133
          Authorization: 6,
134
          'Content-Type': 'application/json',
135
        },
136
        method: 'POST',
137
  // object which is sent to server
138
        body: JSON.stringify({
139
          type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
140
          date: newDate,
141
          from: typeRadio === 'sickday' ? null : "00:00",
142
          to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
143
        }),
144
      });
145
      if (response.ok) {
56
// setting an object
57
  const newDate = whatDate.split("-").join("/");
146 58

  
147
      const response = await fetch(
148
        'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
149
          headers: {
150
            Authorization: 1
151
          },
152
        }
153
  
154
       );
155
      const data = await response.json();
59
  const peps = {
60
    type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
61
    date: newDate,
62
    from: typeRadio === 'sickday' ? null : "00:00",
63
    to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
64
  };
156 65

  
157
      props.setUser(data.map(request => {
158
        const a = request.date;
159
        const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
66
  api_fetch.tvojefunkce(peps)
67
    
68
  try {
69
// send accepted request to server
70
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
71
      headers: {
72
        Authorization: 1,
73
        'Content-Type': 'application/json',
74
      },
75
      method: 'POST',
76
// object which is sent to server
77
      body: JSON.stringify(peps),
78
    });
79
    if (response.ok) {
160 80
  
161
        return (
162
          {
163
            title: request.firstName + ' ' + request.lastName,
164
            id: request.id,
165
            type: request.type,
166
            start: b,
167
            end: null,
168
            status: request.status
169
        })
170
      }))
171

  
172
  } else {
173 81

  
174
    if(response.status === 400) {
175
    alert('error 400 ADD EVENT - EMPLOYEE')
82
  const userProps = {
83
    title: props.userName.name,
84
    start: whatDate
85
  
86
}
87
//concat new request to current ones
88
    props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
89
} else {
90
  if(response.status === 400) {
91
    alert('error 400 ADD EVENT ADMIN - EMPLOYER')
176 92
 }
177 93
    else if (response.status === 500) {
178
       alert ('error 500 ADD EVENT - EMPLOYEE')
94
       alert ('error 500 ADD EVENT ADMIN - EMPLOYER')
179 95
    }
180 96
    else {
181
       alert('error ADD EVENT - EMPLOYEE')
182
    }
183
    
184
  }
185
    } catch (e) {
186
      alert('error catch ADD EVENT - EMPLOYEE')
97
       alert('error ADD EVENT ADMIN - EMPLOYER')
187 98
    }
188

  
189
    setOpen(false)}
190

  
191
  
192
// ********************* ADD EVENT ADMIN - EMPLOYER **************************
193

  
194
  const addEventAdmin = async (e) => {
195
    e.preventDefault();
196

  
197
  // setting an object
198
    const newDate = whatDate.split("-").join("/");
199
      
200
    try {
201
  // send accepted request to server
202
      const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
203
        headers: {
204
          Authorization: 1,
205
          'Content-Type': 'application/json',
206
        },
207
        method: 'POST',
208
  // object which is sent to server
209
        body: JSON.stringify({
210
          type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
211
          date: newDate,
212
          from: typeRadio === 'sickday' ? null : "00:00",
213
          to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
214
        }),
215
      });
216
      if (response.ok) {
217
    
218

  
219
    const userProps = {
220
      title: props.userName.name,
221
      start: whatDate
222
    
223
  }
224
  //concat new request to current ones
225
      props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
226
  } else {
227
    if(response.status === 400) {
228
      alert('error 400 ADD EVENT ADMIN - EMPLOYER')
229
   }
230
      else if (response.status === 500) {
231
         alert ('error 500 ADD EVENT ADMIN - EMPLOYER')
232
      }
233
      else {
234
         alert('error ADD EVENT ADMIN - EMPLOYER')
235
      }
99
}
100
  } catch (e) {
101
    alert('error catch ADD EVENT ADMIN - EMPLOYER')
236 102
  }
237
    } catch (e) {
238
      alert('error catch ADD EVENT ADMIN - EMPLOYER')
239
    }
240 103

  
241 104

  
242
    setOpen(false)}
105
  setOpen(false)}
243 106
    
244 107

  
245 108
  return (
......
267 130
    >
268 131
    <div className="calendar-form">
269 132
      {/* <form onSubmit={(e) => addEvent(e)}> */}
270
      <form onSubmit={props.userName.role === 'EMPLOYER' ? (e) => addEventAdmin(e) : (e) => addEvent(e) }>
133
      <form onSubmit={props.userName.role === 'EMPLOYER' 
134
      ? (e) => addEventAdmin(e).then(eventAdmin => { props.setUser(eventAdmin)}) 
135
      : (e) => api_fetch.addEvent(e).then(userEvent => { props.setUser(userEvent)})
136
      }>
271 137
        <h2>Choose an option</h2>
272 138
        <div className="calendar-radio">
273 139
          <input checked={
client/src/api.js
1
// ******************** GET DATA APP getCurrentProfile, [userName, setUserName] ********************
2

  
3
export const getCurrentProfile = async () => {
4

  
5
    try {
6
    const response = await fetch(
7
      'http://devcz.yoso.fi:8090/ymanager/users/current/profile', {
8
        headers: {
9
          Authorization: 1
10
        }
11
      }
12
    );
13

  
14
    if (response.ok) {
15
        const data = await response.json();
16
        return {
17
        name: data.firstName + ' ' + data.lastName,
18
        role: data.role,
19
        id: data.id,
20
        holiday: data.vacationCount,
21
        sickday: data.sickDayCount
22
    }
23
    } else {
24
        if(response.status === 400) {
25
        alert('error 400 GET DATA APP (getCurrentProfile)')
26
    }
27
        else if (response.status === 500) {
28
            alert ('error 500 GET DATA APP (getCurrentProfile)')
29
        }
30
        else {
31
            alert('error GET DATA APP (getCurrentProfile)')
32
        }
33
    }
34

  
35
} catch (e) {
36
  alert('error catch GET DATA APP (getCurrentProfile)')
37
  }
38
}
39

  
40
// ******************** LOAD DATA to CALENDAR - EMPLOYEE ********************
41
export const getUserCalendar = async (userName, fromDate ) => {
42
  try {
43
  const response = await fetch(
44
    `http://devcz.yoso.fi:8090/ymanager/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
45
      headers: {
46
        'Accept': 'application/json',
47
        Authorization: 6
48
      },
49
      method: 'GET',
50
    }
51
  );
52

  
53
  if (response.ok) {
54
  const data = await response.json();
55
  
56
  return data.filter(day => {
57
    return day.status !== 'PENDING'
58
  }).map(day => {
59

  
60
    const newDate = day.date.split("/").join("-");
61

  
62
    return ({
63
    title: userName.name,
64
    start: newDate,
65
    backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
66
    })
67
  })
68
} else {
69
  if(response.status === 400) {
70
    alert('error 400 LOADING DATA (CALENDAR, EMPLOYEE)')
71
 }
72
    else if (response.status === 500) {
73
       alert ('error 500 LOADING DATA (CALENDAR, EMPLOYEE)')
74
    }
75
    else {
76
       alert('error LOADING DATA (CALENDAR, EMPLOYEE)')
77
    }
78
}
79
} catch (e) {
80
  alert('error catch LOADING DATA (CALENDAR, EMPLOYEE)')
81
}
82
}
83

  
84
// ******************** LOAD DATA to CALENDAR - EMPLOYER ********************
85
export const getAdminCalendar = async () => {
86
    try {
87
    const response = await fetch(
88
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=ACCEPTED', {
89
        headers: {
90
          'Accept': 'application/json',
91
          Authorization: 1
92
        },
93
        method: 'GET',
94
      }
95
    );
96

  
97
    if (response.ok) {
98
    const data = await response.json();
99
    
100
    return data.map(day => {
101

  
102
      const newDate = day.date.split("/").join("-");
103

  
104
      return ( {
105
      title: day.firstName + ' ' + day.lastName,
106
      start: newDate
107
      })
108
    })
109
  } else {
110
    if(response.status === 400) {
111
      alert('error 400 LOADING DATA (CALENDAR, EMPLOYER)')
112
   }
113
      else if (response.status === 500) {
114
         alert ('error 500 LOADING DATA (CALENDAR, EMPLOYER))')
115
      }
116
      else {
117
         alert('error LOADING DATA (CALENDAR, EMPLOYER)')
118
      }
119
  }
120
  } catch (e) {
121
    alert('error catch LOADING DATA (CALENDAR, EMPLOYER)')
122
  }
123
}
124

  
125
// ******************** ADD EVENT to CALENDAR - EMPLOYEE ********************
126
export const addEvent = async (e) => {
127
    e.preventDefault();
128

  
129
  // setting an object
130
    const newDate = whatDate.split("-").join("/");
131
      
132
    try {
133
  // send accepted request to server
134
      const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
135
        headers: {
136
          Authorization: 6,
137
          'Content-Type': 'application/json',
138
        },
139
        method: 'POST',
140
  // object which is sent to server
141
        body: JSON.stringify({
142
          type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
143
          date: newDate,
144
          from: typeRadio === 'sickday' ? null : "00:00",
145
          to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
146
        }),
147
      });
148
      if (response.ok) {
149

  
150
      const response = await fetch(
151
        'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
152
          headers: {
153
            Authorization: 1
154
          },
155
        }
156
  
157
       );
158
      const data = await response.json();
159

  
160
      return data.map(request => {
161
        const a = request.date;
162
        const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
163
  
164
        return (
165
          {
166
            title: request.firstName + ' ' + request.lastName,
167
            id: request.id,
168
            type: request.type,
169
            start: b,
170
            end: null,
171
            status: request.status
172
        })
173
      })
174

  
175
  } else {
176

  
177
    if(response.status === 400) {
178
    alert('error 400 ADD EVENT - EMPLOYEE')
179
 }
180
    else if (response.status === 500) {
181
       alert ('error 500 ADD EVENT - EMPLOYEE')
182
    }
183
    else {
184
       alert('error ADD EVENT - EMPLOYEE')
185
    }
186
    
187
  }
188
    } catch (e) {
189
      alert('error catch ADD EVENT - EMPLOYEE')
190
    }
191

  
192
    setOpen(false)}
193
    
194
//
client/src/index.js
4 4
import App from './App';
5 5
import * as serviceWorker from './serviceWorker';
6 6

  
7
window.$nameRequest = 'Angreštécie';
8

  
9 7
ReactDOM.render(
10 8
  <React.StrictMode>
11 9
    <App />

Také k dispozici: Unified diff