Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 9f045397

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

re #58 Refactoring, every fetch moved to api.js

Zobrazit rozdíly:

client/src/api.js
1
// ******************** GET DATA APP getCurrentProfile, [userName, setUserName] ********************
1
const http = 'http://devcz.yoso.fi:8090/ymanager';
2

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

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

  
5 7
    try {
6 8
    const response = await fetch(
7
      'http://devcz.yoso.fi:8090/ymanager/users/current/profile', {
9
      `${http}/users/current/profile`, {
8 10
        headers: {
9 11
          Authorization: 1
10 12
        }
......
22 24
    }
23 25
    } else {
24 26
        if(response.status === 400) {
25
        alert('error 400 GET DATA APP (getCurrentProfile)')
26
    }
27
          throw 'error 400 GET DATA APP (getCurrentProfile)'
28
        }
27 29
        else if (response.status === 500) {
28
            alert ('error 500 GET DATA APP (getCurrentProfile)')
30
          throw 'error 500 GET DATA APP (getCurrentProfile)'
29 31
        }
30 32
        else {
31
            alert('error GET DATA APP (getCurrentProfile)')
33
          throw 'error GET DATA APP (getCurrentProfile)'
32 34
        }
33 35
    }
34 36

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

  
......
41 43
export const getUserCalendar = async (userName, fromDate ) => {
42 44
  try {
43 45
  const response = await fetch(
44
    `http://devcz.yoso.fi:8090/ymanager/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
46
    `${http}/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
45 47
      headers: {
46 48
        'Accept': 'application/json',
47 49
        Authorization: 6
......
66 68
    })
67 69
  })
68 70
} else {
69
  if(response.status === 400) {
70
    alert('error 400 LOADING DATA (CALENDAR, EMPLOYEE)')
71
 }
71
    if(response.status === 400) {
72
      throw 'error 400 LOADING DATA (CALENDAR, EMPLOYEE)'
73
    }
72 74
    else if (response.status === 500) {
73
       alert ('error 500 LOADING DATA (CALENDAR, EMPLOYEE)')
75
      throw 'error 500 LOADING DATA (CALENDAR, EMPLOYEE)'
74 76
    }
75 77
    else {
76
       alert('error LOADING DATA (CALENDAR, EMPLOYEE)')
78
      throw 'error LOADING DATA (CALENDAR, EMPLOYEE)'
77 79
    }
78 80
}
79 81
} catch (e) {
80
  alert('error catch LOADING DATA (CALENDAR, EMPLOYEE)')
82
  throw 'error catch LOADING DATA (CALENDAR, EMPLOYEE)'
81 83
}
82 84
}
83 85

  
......
107 109
      })
108 110
    })
109 111
  } else {
110
    if(response.status === 400) {
111
      alert('error 400 LOADING DATA (CALENDAR, EMPLOYER)')
112
   }
112
      if(response.status === 400) {
113
        throw 'error 400 LOADING DATA (CALENDAR, EMPLOYER)'
114
      }
113 115
      else if (response.status === 500) {
114
         alert ('error 500 LOADING DATA (CALENDAR, EMPLOYER))')
116
        throw 'error 500 LOADING DATA (CALENDAR, EMPLOYER))'
115 117
      }
116 118
      else {
117
         alert('error LOADING DATA (CALENDAR, EMPLOYER)')
119
        throw 'error LOADING DATA (CALENDAR, EMPLOYER)'
118 120
      }
119
  }
121
    }
120 122
  } catch (e) {
121
    alert('error catch LOADING DATA (CALENDAR, EMPLOYER)')
123
    throw 'error catch LOADING DATA (CALENDAR, EMPLOYER)'
122 124
  }
123 125
}
124 126

  
125 127
// ******************** 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 {
128
export async function addEventApi(dataAddEventEmployee) {
129
try {
133 130
  // send accepted request to server
134
      const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
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(dataAddEventEmployee),
139
    });
140
    if (response.ok) {
141
      
142
      const response = await fetch(
143
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
135 144
        headers: {
136
          Authorization: 6,
137
          'Content-Type': 'application/json',
145
          Authorization: 1
138 146
        },
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 147
      });
148
      if (response.ok) {
148
      const data = await response.json();
149
      
150
    return data.map(request => {
151
      const a = request.date;
152
      const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
149 153

  
150
      const response = await fetch(
151
        'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
154
      return (
155
        {
156
          title: request.firstName + ' ' + request.lastName,
157
          id: request.id,
158
          type: request.type,
159
          start: b,
160
          end: null,
161
          status: request.status
162
        })
163
    })
164

  
165
  } 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'
172
    }
173
    else {
174
      throw 'error ADD EVENT - EMPLOYEE'
175
    }
176
  }
177
} catch (e) {
178
    throw 'error catch ADD EVENT - EMPLOYEE'
179
  }
180
}
181
      
182
// ******************** ADD EVENT to CALENDAR - EMPLOYER ********************
183
export async function addEventApiAdmin(dataAddEventAdmin) {
184
  try {
185
    // send accepted request to server
186
        const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
152 187
          headers: {
153
            Authorization: 1
188
            Authorization: 1,
189
            'Content-Type': 'application/json',
154 190
          },
191
          method: 'POST',
192
    // 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')
155 203
        }
156
  
157
       );
158
      const data = await response.json();
204
        else {
205
           throw('error ADD EVENT ADMIN - EMPLOYER')
206
        }
207
    }
208
  } catch (e) {
209
      throw('error catch ADD EVENT ADMIN - EMPLOYER')
210
    }
211
}
159 212

  
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('');
213
// ******************** GET DATA to OVERVIEW - EMPLOYER ********************
214
export const getUsersOverview = async () => {
215
  try {
216
  const response = await fetch (
217
     'http://devcz.yoso.fi:8090/ymanager/users', {
218
      headers: {
219
        Authorization: 1          }
220
    }
221
  );
163 222
  
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
223
if (response.ok) {
224

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

  
228
     return (
229
        {
230
           name: user.firstName + ' ' + user.lastName,
231
           id: user.id,
232
           sickday: user.sickDayCount,
233
           holiday: user.vacationCount,
234
           role: user.role
172 235
        })
173
      })
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)'
250
  }
251
}
174 252

  
175
  } else {
253
// ******************** SAVE DATA to OVERVIEW - EMPLOYER ********************
254
export async function saveDataOverview(dataOverviewObject) {
255
  try {
256
    // send accepted request to server
257
        const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/settings', {
258
          headers: {
259
            Authorization: 1,
260
            'Content-Type': 'application/json',
261
          },
262
          method: 'PUT',
263
 
264
    // object which is sent to server
265
          body: JSON.stringify(dataOverviewObject),        
266
        });
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
  } catch (e) {
279
    throw 'error catch SAVE DATA (OVERVIEW, EMPLOYER'
280
  }
281
}
282

  
283
// ******************** LOAD DATA to SETTING - EMPLOYER ********************
284
export const getSettingData = async () =>  {
285
  try {
286
    const response = await fetch(
287
      'http://devcz.yoso.fi:8090/ymanager/settings', {
288
        headers: {
289
          Authorization: 1
290
        }
291
      });
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
  } catch (e) {
310
    throw 'error catch LOADING DATA (SETTING, EMPLOYER)'
311
    }
312
}
313

  
314
// ******************** SAVE DATA to SETTING - EMPLOYER ********************
315
export async function saveDataSetting(dataSettingObject) {
316
  try {
317
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/settings', {
318
      headers: {
319
        'Authorization': 6,
320
        'Content-Type': 'application/json'
321
      },
322
      method: 'POST',
323
      body: JSON.stringify(dataSettingObject),
324
    });
325

  
326
    switch (response.status) {
327
      case 200:
328
        throw '...'
329
      case 500:
330
        throw ''
331
      default:
332
        throw response.statusText
333

  
334
    }
176 335

  
177 336
    if(response.status === 400) {
178
    alert('error 400 ADD EVENT - EMPLOYEE')
179
 }
337
      throw 'error 400 SAVE DATA (OVERVIEW, EMPLOYER)'
338
    }
180 339
    else if (response.status === 500) {
181
       alert ('error 500 ADD EVENT - EMPLOYEE')
340
      throw 'error 500 SAVE DATA (OVERVIEW, EMPLOYER)'
182 341
    }
183
    else {
184
       alert('error ADD EVENT - EMPLOYEE')
342
    else if (!response.ok) {
343
      throw 'error SAVE DATA (OVERVIEW, EMPLOYER)'
344
      }
345
  } catch (e) {
346
      throw 'error catch SAVE DATA (OVERVIEW, EMPLOYER)'
185 347
    }
186
    
348
}
349

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

  
352
export async function loadYourRequests() {
353
  try {
354
    const response = await fetch(
355
      'http://devcz.yoso.fi:8090/ymanager/user/6/calendar?from=2020/06/24&status=PENDING', {
356
        headers: {
357
          Authorization: 6
358
        },
359
      }
360
    );
361

  
362
  if (response.ok) {
363
    const data = await response.json();
364
    return data;
365
  } 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)')
187 379
  }
188
    } catch (e) {
189
      alert('error catch ADD EVENT - EMPLOYEE')
380
}
381

  
382
// ****************** LOAD DATA - UPCOMING REQUESTS - EMPLOYER ******************
383
export async function loadAdminRequests() {
384
  try {
385
    const response = await fetch(
386
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
387
        headers: {
388
          Authorization: 1
389
        }
390
      },
391
    );
392

  
393
     if (response.ok) {
394
    const data = await response.json();
395
      return data;
396
  } 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
      }
190 406
    }
407
} catch (e) {
408
  alert('error catch GET DATA (UPCOMING REQUESTS)')
409
  } 
410
}
411

  
412
// ************** SEND ACCEPTED DATA - UPCOMING REQUESTS - EMPLOYER **************
413
export async function sendAcceptedRequest(acceptedRequests) {
414
  try {
415
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
416
      headers: {
417
        Authorization: 1,
418
        'Content-Type': 'application/json',
419
      },
420
      method: 'PUT',
421
      body: JSON.stringify(acceptedRequests),
422
    });
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
  } catch (e) {
439
    alert('error catch SEND ACCEPTED DATA (UPCOMING REQUESTS)')
440
    }
441
}
442

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

  
445
export async function sendRejectedRequest(rejectedRequest) {
446
  try {
447
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/requests?type=VACATION', {
448
      headers: {
449
        Authorization: 1,
450
        'Content-Type': 'application/json',
451
      },
452
      method: 'PUT',
453
      body: JSON.stringify(rejectedRequest),
454
    });
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)')
472
  }
473
}
474

  
191 475

  
192
    setOpen(false)}
193
    
194
//

Také k dispozici: Unified diff