Projekt

Obecné

Profil

« Předchozí | Další » 

Revize ebfe6347

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

re #58 client part(especially rebuilt api.js, all fetches, little edits in YourRequests/UpcomingRequests and fixed bugs in calendar and overview (add event, save, post, numbers)

Zobrazit rozdíly:

client/src/api.js
1

  
1 2
const http = 'http://devcz.yoso.fi:8090/ymanager';
2 3

  
3 4
// ******************** GET DATA APP getCurrentProfile ********************
4 5

  
5 6
export const getCurrentProfile = async () => {
6 7

  
7
    try {
8
    const response = await fetch(
8
  let response;
9

  
10
  try {
11
    response = await fetch(
9 12
      `${http}/users/current/profile`, {
10 13
        headers: {
11 14
          Authorization: 1
12 15
        }
13 16
      }
14
    );
17
    );    
18
  } catch (e) {
19
    throw 'Server is not available'
20
    }
15 21

  
16
    if (response.ok) {
17
        const data = await response.json();
18
        return {
19
        name: data.firstName + ' ' + data.lastName,
20
        role: data.role,
21
        id: data.id,
22
        holiday: data.vacationCount,
23
        sickday: data.sickDayCount
22
  if (response.ok) {
23
    const data = await response.json();
24

  
25
    return {
26
      name: data.firstName + ' ' + data.lastName,
27
      role: data.role,
28
      id: data.id,
29
      holiday: data.vacationCount,
30
      sickday: data.sickDayCount,
31
      takenSickday: data.takenSickDayCount
24 32
    }
25
    } else {
26
        if(response.status === 400) {
27
          throw 'error 400 GET DATA APP (getCurrentProfile)'
28
        }
29
        else if (response.status === 500) {
30
          throw 'error 500 GET DATA APP (getCurrentProfile)'
31
        }
32
        else {
33
          throw 'error GET DATA APP (getCurrentProfile)'
34
        }
33
  } else {
34
      switch (response.status) {
35
        case 401:
36
          throw new Error('Not authenticated.')
37
        case 500:
38
          throw new Error('Internal server error.')
39
        default:
40
          throw new Error(response.statusText)
41
      }
35 42
    }
36

  
37
} catch (e) {
38
  throw 'error catch GET DATA APP (getCurrentProfile)'
39
  }
40 43
}
41 44

  
42 45
// ******************** LOAD DATA to CALENDAR - EMPLOYEE ********************
43 46
export const getUserCalendar = async (userName, fromDate ) => {
47

  
48
  let response;
49

  
44 50
  try {
45
  const response = await fetch(
46
    `${http}/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
47
      headers: {
48
        'Accept': 'application/json',
49
        Authorization: 6
50
      },
51
      method: 'GET',
52
    }
53
  );
51
    response = await fetch(
52
      `${http}/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
53
        headers: {
54
          'Accept': 'application/json',
55
          Authorization: 6
56
        },
57
        method: 'GET',
58
      }
59
    );
60
  } catch (e) {
61
    throw 'Server is not available'
62
  }
54 63

  
55 64
  if (response.ok) {
56
  const data = await response.json();
65
    const data = await response.json();
57 66
  
58
  return data.filter(day => {
59
    return day.status !== 'PENDING'
60
  }).map(day => {
67
    return data.filter(day => {
68
      return day.status !== 'PENDING'
69
    }).map(day => {
61 70

  
62 71
    const newDate = day.date.split("/").join("-");
63 72

  
64 73
    return ({
65
    title: userName.name,
66
    start: newDate,
67
    backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
74
      title: userName.name,
75
      start: newDate,
76
      backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
68 77
    })
69 78
  })
70
} else {
71
    if(response.status === 400) {
72
      throw 'error 400 LOADING DATA (CALENDAR, EMPLOYEE)'
73
    }
74
    else if (response.status === 500) {
75
      throw 'error 500 LOADING DATA (CALENDAR, EMPLOYEE)'
76
    }
77
    else {
78
      throw 'error LOADING DATA (CALENDAR, EMPLOYEE)'
79
  } else {
80
      switch (response.status) {
81
        case 400:
82
          throw new Error('Bad request. Check query parameters.')
83
        case 401:
84
          throw new Error('Not authenticated.')
85
        case 403:
86
          throw new Error('Not authorized.')
87
        case 404:
88
          throw new Error('User with given ID does not exist.')
89
        case 500:
90
          throw new Error('Internal server error.')
91
        default:
92
          throw new Error(response.statusText)
93
      }
79 94
    }
80 95
}
81
} catch (e) {
82
  throw 'error catch LOADING DATA (CALENDAR, EMPLOYEE)'
83
}
84
}
85 96

  
86 97
// ******************** LOAD DATA to CALENDAR - EMPLOYER ********************
98

  
87 99
export const getAdminCalendar = async () => {
88
    try {
89
    const response = await fetch(
90
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=ACCEPTED', {
100

  
101
  let response;
102

  
103
  try {
104
    response = await fetch(
105
      `${http}/users/requests/vacation?status=ACCEPTED`, {
91 106
        headers: {
92 107
          'Accept': 'application/json',
93 108
          Authorization: 1
......
95 110
        method: 'GET',
96 111
      }
97 112
    );
113
  } catch (e) {
114
    throw 'Server is not available'
115
  }
98 116

  
99
    if (response.ok) {
117
  if (response.ok) {
100 118
    const data = await response.json();
101 119
    
102 120
    return data.map(day => {
......
109 127
      })
110 128
    })
111 129
  } else {
112
      if(response.status === 400) {
113
        throw 'error 400 LOADING DATA (CALENDAR, EMPLOYER)'
114
      }
115
      else if (response.status === 500) {
116
        throw 'error 500 LOADING DATA (CALENDAR, EMPLOYER))'
117
      }
118
      else {
119
        throw 'error LOADING DATA (CALENDAR, EMPLOYER)'
130
      switch (response.status) {
131
        case 400:
132
          throw new Error('Bad request. Check query parameters.')
133
        case 401:
134
          throw new Error('Not authenticated.')
135
        case 403:
136
          throw new Error('Not authorized.')
137
        case 500:
138
          throw new Error('Internal server error.')
139
        default:
140
          throw new Error(response.statusText)
120 141
      }
121 142
    }
122
  } catch (e) {
123
    throw 'error catch LOADING DATA (CALENDAR, EMPLOYER)'
124
  }
125 143
}
126 144

  
127 145
// ******************** ADD EVENT to CALENDAR - EMPLOYEE ********************
146

  
128 147
export async function addEventApi(dataAddEventEmployee) {
129
try {
148
  let response;
149

  
150
  try {
130 151
  // send accepted request to server
131
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
152
    response = await fetch(`${http}/user/calendar/create`, {
132 153
      headers: {
133 154
        Authorization: 6,
134 155
        'Content-Type': 'application/json',
......
137 158
  // object which is sent to server
138 159
  body: JSON.stringify(dataAddEventEmployee),
139 160
    });
140
    if (response.ok) {
161
  } catch (e) {
162
    throw 'Server is not available'
163
  }
164

  
165
  if (response.ok) {
141 166
      
142
      const response = await fetch(
143
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
144
        headers: {
145
          Authorization: 1
146
        },
147
      });
148
      const data = await response.json();
167
    response = await fetch(
168
    `${http}/users/requests/vacation?status=PENDING`, {
169
      headers: {
170
        Authorization: 1
171
      },
172
    });
173
    const data = await response.json();
149 174
      
150 175
    return data.map(request => {
151 176
      const a = request.date;
......
163 188
    })
164 189

  
165 190
  } else {
166
    
167
    if(response.status === 400) {
168
      throw 'error 400 ADD EVENT - EMPLOYEE'
169
    }
170
    else if (response.status === 500) {
171
      throw 'error 500 ADD EVENT - EMPLOYEE'
191
      switch (response.status) {
192
        case 400:
193
          throw new Error('Bad request. Check request body.')
194
        case 401:
195
          throw new Error('Not authenticated.')
196
        case 403:
197
          throw new Error('Not authorized.')
198
        case 500:
199
          throw new Error('Internal server error.')
200
        default:
201
          throw new Error(response.statusText)
202
      }    
172 203
    }
173
    else {
174
      throw 'error ADD EVENT - EMPLOYEE'
175
    }
176
  }
177
} catch (e) {
178
    throw 'error catch ADD EVENT - EMPLOYEE'
179
  }
180 204
}
181 205
      
182 206
// ******************** ADD EVENT to CALENDAR - EMPLOYER ********************
207

  
183 208
export async function addEventApiAdmin(dataAddEventAdmin) {
209
  let response;
210

  
184 211
  try {
185
    // send accepted request to server
186
        const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
187
          headers: {
188
            Authorization: 1,
189
            'Content-Type': 'application/json',
190
          },
191
          method: 'POST',
212
  // send accepted request to server
213
    response = await fetch(`${http}/user/calendar/create`, {
214
      headers: {
215
        Authorization: 1,
216
        'Content-Type': 'application/json',
217
    },
218
      method: 'POST',
192 219
    // object which is sent to server
193
          body: JSON.stringify(dataAddEventAdmin),
194
        });
195
        if (response.ok) {
196
          return;
197
    } else {
198
      if(response.status === 400) {
199
        throw('error 400 ADD EVENT ADMIN - EMPLOYER')
200
     }
201
        else if (response.status === 500) {
202
           throw ('error 500 ADD EVENT ADMIN - EMPLOYER')
203
        }
204
        else {
205
           throw('error ADD EVENT ADMIN - EMPLOYER')
206
        }
207
    }
220
      body: JSON.stringify(dataAddEventAdmin),
221
    });
208 222
  } catch (e) {
209
      throw('error catch ADD EVENT ADMIN - EMPLOYER')
210
    }
223
    throw 'Server is not available'
224
  }
225

  
226
  if (!response.ok) {
227
    switch (response.status) {
228
      case 400:
229
        throw new Error('Bad request. Check request body.')
230
      case 401:
231
        throw new Error('Not authenticated.')
232
      case 403:
233
        throw new Error('Not authorized.')
234
      case 500:
235
        throw new Error('Internal server error.')
236
      default:
237
        throw new Error(response.statusText)
238
    }    
239
  }
211 240
}
212 241

  
213 242
// ******************** GET DATA to OVERVIEW - EMPLOYER ********************
243

  
214 244
export const getUsersOverview = async () => {
245
  let response;
246

  
215 247
  try {
216
  const response = await fetch (
217
     'http://devcz.yoso.fi:8090/ymanager/users', {
248
  response = await fetch (
249
    `${http}/users`, {
218 250
      headers: {
219 251
        Authorization: 1          }
220 252
    }
221 253
  );
254
  } catch (e) {
255
    throw 'Server is not available'
256
  }
222 257
  
223
if (response.ok) {
258
  if (response.ok) {
224 259

  
225
  const data = await response.json();
226
  return data.map(user => {
260
    const data = await response.json();
261
    return data.map(user => {
227 262

  
228
     return (
229
        {
263
     return ({
230 264
           name: user.firstName + ' ' + user.lastName,
231 265
           id: user.id,
232 266
           sickday: user.sickDayCount,
233 267
           holiday: user.vacationCount,
268
           takenSickday: user.takenSickDayCount,
234 269
           role: user.role
235 270
        })
236
  })
237
}  else {
238
      if(response.status === 400) {
239
        throw 'error 400 GET DATA (OVERVIEW, EMPLOYER)'
240
      }
241
      else if (response.status === 500) {
242
        throw 'error 500 GET DATA (OVERVIEW, EMPLOYER)'
243
      }
244
      else {
245
        throw 'error GET DATA (OVERVIEW, EMPLOYER)'
246
        }
247
      }
248
  } catch (e) {
249
      throw 'error catch GET DATA (OVERVIEW, EMPLOYER)'
271
    })
272
  } else {
273
    switch (response.status) {
274
      case 400:
275
        throw new Error('Bad request. Check query parameters.')
276
      case 401:
277
        throw new Error('Not authenticated.')
278
      case 403:
279
        throw new Error('Not authorized.')
280
      case 500:
281
        throw new Error('Internal server error.')
282
      default:
283
        throw new Error(response.statusText)
284
    }    
250 285
  }
251 286
}
252 287

  
253 288
// ******************** SAVE DATA to OVERVIEW - EMPLOYER ********************
289

  
254 290
export async function saveDataOverview(dataOverviewObject) {
291
  let response;
292

  
255 293
  try {
256 294
    // send accepted request to server
257
        const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/settings', {
295
        response = await fetch(`${http}/user/settings`, {
258 296
          headers: {
259 297
            Authorization: 1,
260 298
            'Content-Type': 'application/json',
......
264 302
    // object which is sent to server
265 303
          body: JSON.stringify(dataOverviewObject),        
266 304
        });
267
        console.log(response.status)
268
       if (response.status === 400) {
269
        throw 'error 400 SAVE DATA (OVERVIEW, EMPLOYER)'
270
          }
271
      else if (response.status === 500) {
272
        throw 'error 500 SAVE DATA (OVERVIEW, EMPLOYER)'
273
      }
274
      else if (!response.ok) {
275
        throw 'error SAVE DATA (OVERVIEW, EMPLOYER)'
276
      }
277

  
278 305
  } catch (e) {
279
    throw 'error catch SAVE DATA (OVERVIEW, EMPLOYER'
306
    throw 'Server is not available'
280 307
  }
308

  
309
  if (!response.ok) {
310
    switch (response.status) {
311
      case 400:
312
        throw new Error('Bad request. Check query parameters.')
313
      case 401:
314
        throw new Error('Not authenticated.')
315
      case 403:
316
        throw new Error('Not authorized.')
317
      case 500:
318
        throw new Error('Internal server error.')
319
      default:
320
        throw new Error(response.statusText)
321
    }
322
  }    
281 323
}
282 324

  
283 325
// ******************** LOAD DATA to SETTING - EMPLOYER ********************
284 326
export const getSettingData = async () =>  {
327
  let response;
328

  
285 329
  try {
286 330
    const response = await fetch(
287
      'http://devcz.yoso.fi:8090/ymanager/settings', {
331
      `${http}/settings`, {
288 332
        headers: {
289 333
          Authorization: 1
290 334
        }
291 335
      });
292

  
293
      if (response.ok) {
294
      const data = await response.json();
295
      return {
296
        sickday: data.sickDayCount,
297
      }
298
    } else {
299
        if(response.status === 400) {
300
          throw 'error 400 LOADING DATA (SETTING, EMPLOYER)'
301
        }
302
        else if (response.status === 500) {
303
           throw 'error 500 LOADING DATA (SETTING, EMPLOYER)'
304
        }
305
        else {
306
           throw 'error LOADING DATA (SETTING, EMPLOYER)'
307
        }
308
      }
309 336
  } catch (e) {
310
    throw 'error catch LOADING DATA (SETTING, EMPLOYER)'
337
    throw 'Server is not available'
338
    }
339

  
340
  if (response.ok) {
341
    const data = await response.json();
342
    return {
343
      sickday: data.sickDayCount,
344
    }
345
  } else {
346
    switch (response.status) {
347
      case 500:
348
        throw new Error('Internal server error.')
349
      default:
350
        throw new Error(response.statusText)
351
      }            
311 352
    }
353

  
312 354
}
313 355

  
314 356
// ******************** SAVE DATA to SETTING - EMPLOYER ********************
315 357
export async function saveDataSetting(dataSettingObject) {
358
  let response;
359

  
316 360
  try {
317
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/settings', {
361
    response = await fetch(`${http}/settings`, {
318 362
      headers: {
319
        'Authorization': 6,
363
        'Authorization': 1,
320 364
        'Content-Type': 'application/json'
321 365
      },
322 366
      method: 'POST',
323 367
      body: JSON.stringify(dataSettingObject),
324 368
    });
369
  } catch (e) {
370
    throw 'Server is not available'
371
  }
325 372

  
373
  if (!response.ok) {
326 374
    switch (response.status) {
327
      case 200:
328
        throw '...'
375
      case 401:
376
        throw new Error('Not authenticated.')
377
      case 403:
378
        throw new Error('Not authorized.')
329 379
      case 500:
330
        throw ''
380
        throw new Error('Internal server error.')
331 381
      default:
332
        throw response.statusText
333

  
334
    }
335

  
336
    if(response.status === 400) {
337
      throw 'error 400 SAVE DATA (OVERVIEW, EMPLOYER)'
338
    }
339
    else if (response.status === 500) {
340
      throw 'error 500 SAVE DATA (OVERVIEW, EMPLOYER)'
341
    }
342
    else if (!response.ok) {
343
      throw 'error SAVE DATA (OVERVIEW, EMPLOYER)'
344
      }
345
  } catch (e) {
346
      throw 'error catch SAVE DATA (OVERVIEW, EMPLOYER)'
347
    }
382
        throw new Error(response.statusText)
383
    }    
384
  }
348 385
}
349 386

  
350 387
// ****************** LOAD DATA to YOUR REQUESTS - EMPLOYEE ******************
351 388

  
352 389
export async function loadYourRequests() {
390
  let response;
391
  
353 392
  try {
354
    const response = await fetch(
355
      'http://devcz.yoso.fi:8090/ymanager/user/6/calendar?from=2020/06/24&status=PENDING', {
393
    response = await fetch(
394
      `${http}/user/6/calendar?from=2020/06/24&status=PENDING`, {
356 395
        headers: {
357 396
          Authorization: 6
358 397
        },
359 398
      }
360 399
    );
400
  } catch (e) {
401
    throw 'Server is not available'
402
  }
361 403

  
362 404
  if (response.ok) {
363 405
    const data = await response.json();
364 406
    return data;
365 407
  } else {
366
    if(response.status === 400) {
367
      alert('error 400 GET DATA (YOUR REQUEST)')
368
   }
369
      else if (response.status === 500) {
370
         alert ('error 500 GET DATA (YOUR REQUEST)')
371
      }
372
      else {
373
         alert('error GET DATA (YOUR REQUEST)')
374
      }
375
  }
376
} catch (e) {
377
  console.log(e)
378
  alert('error catch GET DATA (YOUR REQUEST)')
379
  }
408
      switch (response.status) {
409
        case 400:
410
          throw new Error('Bad request. Check query parameters.')
411
        case 401:
412
          throw new Error('Not authenticated.')
413
        case 403:
414
          throw new Error('Not authorized.')
415
        case 500:
416
          throw new Error('Internal server error.')
417
        default:
418
          throw new Error(response.statusText)
419
      }    
420
    }
380 421
}
381 422

  
382
// ****************** LOAD DATA - UPCOMING REQUESTS - EMPLOYER ******************
423
// ****************** LOAD DATA - UPCOMING REQUESTS - EMPLOYER ****************** //tady
383 424
export async function loadAdminRequests() {
425
  let response;
426

  
384 427
  try {
385
    const response = await fetch(
386
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
428
    response = await fetch(
429
      `${http}/users/requests/vacation?status=PENDING`, {
387 430
        headers: {
388 431
          Authorization: 1
389 432
        }
390 433
      },
391 434
    );
392

  
393
     if (response.ok) {
435
  } catch (e) {
436
    throw 'Server is not available'
437
    } 
438
  
439
  if (response.ok) {
394 440
    const data = await response.json();
395 441
      return data;
396 442
  } else {
397
    if(response.status === 400) {
398
      alert('error 400 GET DATA (UPCOMING REQUESTS)')
399
   }
400
      else if (response.status === 500) {
401
         alert ('error 500 GET DATA (UPCOMING REQUESTS)')
402
      }
403
      else {
404
         alert('error GET DATA (UPCOMING REQUESTS)')
405
      }
443
      switch (response.status) {
444
        case 400:
445
          throw new Error('Bad request. Check query parameters.')
446
        case 401:
447
          throw new Error('Not authenticated.')
448
        case 403:
449
          throw new Error('Not authorized.')
450
        case 500:
451
          throw new Error('Internal server error.')
452
        default:
453
          throw new Error(response.statusText)
454
      }   
406 455
    }
407
} catch (e) {
408
  alert('error catch GET DATA (UPCOMING REQUESTS)')
409
  } 
410 456
}
411 457

  
412 458
// ************** SEND ACCEPTED DATA - UPCOMING REQUESTS - EMPLOYER **************
459

  
413 460
export async function sendAcceptedRequest(acceptedRequests) {
461
  let response;
462

  
414 463
  try {
415
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
464
    response = await fetch(`${http}/user/requests?type=VACATION`, {
416 465
      headers: {
417 466
        Authorization: 1,
418 467
        'Content-Type': 'application/json',
......
420 469
      method: 'PUT',
421 470
      body: JSON.stringify(acceptedRequests),
422 471
    });
423

  
424
    if (response.ok) {
425
    return;
426

  
427
    } else {
428
      if(response.status === 400) {
429
        alert('error 400 SEND ACCEPTED DATA (UPCOMING REQUESTS)')
430
     }
431
        else if (response.status === 500) {
432
           alert ('error 500 SEND ACCEPTED DATA (UPCOMING REQUESTS)')
433
        }
434
        else {
435
           alert('error SEND ACCEPTED DATA (UPCOMING REQUESTS)')
436
        }
437
    }
438 472
  } catch (e) {
439
    alert('error catch SEND ACCEPTED DATA (UPCOMING REQUESTS)')
473
    throw 'Server is not available'
440 474
    }
475

  
476
  if (!response.ok) {
477
    switch (response.status) {
478
      case 400:
479
        throw new Error('Bad request. Check query parameters and request body.')
480
      case 401:
481
        throw new Error('Not authenticated.')
482
      case 403:
483
        throw new Error('Not authorized.')
484
      case 404:
485
        throw new Error('Neither vacation nor authorization request with given ID exists.')
486
      case 500:
487
        throw new Error('Internal server error.')
488
      default:
489
        throw new Error(response.statusText)
490
    }   
491
  }
441 492
}
442 493

  
443 494
// ************** SEND REJECTED DATA - UPCOMING REQUESTS - EMPLOYER **************
444 495

  
445 496
export async function sendRejectedRequest(rejectedRequest) {
497
  let response;
498
  
446 499
  try {
447
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
500
    response = await fetch(`${http}/user/requests?type=VACATION`, {
448 501
      headers: {
449 502
        Authorization: 1,
450 503
        'Content-Type': 'application/json',
......
452 505
      method: 'PUT',
453 506
      body: JSON.stringify(rejectedRequest),
454 507
    });
455

  
456
  if (response.ok) {    
457
    return;
458

  
459
  } else {
460
    if(response.status === 400) {
461
      alert('error 400 SEND REJECTED DATA (UPCOMING REQUESTS)')
462
   }
463
      else if (response.status === 500) {
464
         alert ('error 500 SEND REJECTED DATA (UPCOMING REQUESTS)')
465
      }
466
      else {
467
         alert('error SEND REJECTED DATA (UPCOMING REQUESTS)')
468
      }
469
  }
470
} catch (e) {
471
  alert('error catch SEND REJECTED DATA (UPCOMING REQUESTS)')
508
  } catch (e) {
509
    throw 'Server is not available'
510
    }
511
  
512
  if (!response.ok) {
513
    switch (response.status) {
514
      case 400:
515
        throw new Error('Bad request. Check query parameters and request body.')
516
      case 401:
517
        throw new Error('Not authenticated.')
518
      case 403:
519
        throw new Error('Not authorized.')
520
      case 404:
521
        throw new Error('Neither vacation nor authorization request with given ID exists.')
522
      case 500:
523
        throw new Error('Internal server error.')
524
      default:
525
        throw new Error(response.statusText)
526
    }    
472 527
  }
473 528
}
474

  
475

  

Také k dispozici: Unified diff