Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 5bedee9e

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

re #58 Rewrited client app to React #58 // yoso internal app // in progress #60 // fixed

Zobrazit rozdíly:

client/src/api.js
7 7
  try {
8 8
    response = await fetch(
9 9
      `${window.config.baseUrl}/users/current/profile`, {
10
        headers: {
11
          Authorization: 1
12

  
13
        },
10
        headers: {},
14 11
        credentials: 'include'
15 12
      }
16 13
    );    
......
27 24
      id: data.id,
28 25
      holiday: data.vacationCount,
29 26
      sickday: data.sickDayCount,
27
      photo: data.photo,
30 28
      takenSickday: data.takenSickDayCount
31 29
    }
32 30
  } else {
33 31
      switch (response.status) {
34 32
        case 401:
35
          window.location.replace("http://localhost:3000/login")
33
          window.location.replace(`/login`)
36 34
          break;
37 35
        case 403:
38
          window.location.replace("http://localhost:3000/login")
36
          window.location.replace(`/login`)
39 37
          break;
40 38
        case 500:
41 39
          throw new Error('Internal server error.')
......
46 44
}
47 45

  
48 46
// ******************** LOAD DATA to CALENDAR - EMPLOYEE ********************
49
export const getUserCalendar = async (userName, fromDate ) => {
47
export const getUserCalendar = async (currentUser, fromDate ) => {
50 48

  
51 49
  let response;
52 50

  
53 51
  try {
54 52
    response = await fetch(
55
      `${window.config.baseUrl}/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
53
      `${window.config.baseUrl}/user/${currentUser.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
56 54
        headers: {
57 55
          'Accept': 'application/json',
58
          Authorization: 6
59 56
        },
60 57
        credentials: 'include',
61 58
        method: 'GET',
......
75 72
    const newDate = day.date.split("/").join("-");
76 73

  
77 74
    return ({
78
      title: userName.name,
75
      title: currentUser.name,
79 76
      start: newDate,
80 77
      backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
81 78
    })
......
85 82
        case 400:
86 83
          throw new Error('Bad request. Check query parameters.')
87 84
        case 401:
88
          window.location.replace("http://localhost:3000/login")
85
          window.location.replace(`/login`)
89 86
        case 403:
90
          window.location.replace("http://localhost:3000/login")
87
          window.location.replace(`/login`)
91 88
        case 404:
92 89
          throw new Error('User with given ID does not exist.')
93 90
        case 500:
......
109 106
      `${window.config.baseUrl}/users/requests/vacation?status=ACCEPTED`, {
110 107
        headers: {
111 108
          'Accept': 'application/json',
112
          Authorization: 6
113 109
        },
114 110
        credentials: 'include',
115 111
        method: 'GET',
......
136 132
        case 400:
137 133
          throw new Error('Bad request. Check query parameters.')
138 134
        case 401:
139
          window.location.replace("http://localhost:3000/login")
135
          window.location.replace(`/login`)
140 136
        case 403:
141
          window.location.replace("http://localhost:3000/login")
137
          window.location.replace(`/login`)
142 138
        case 500:
143 139
          throw new Error('Internal server error.')
144 140
        default:
......
149 145

  
150 146
// ******************** ADD EVENT to CALENDAR - EMPLOYEE ********************
151 147

  
152
export async function addEventApi(userName, dataAddEventEmployee) {
148
export async function addEventApi(dataAddEventEmployee) {
153 149
  let response;
154 150

  
155 151
  try {
156 152
  // send accepted request to server
157 153
    response = await fetch(`${window.config.baseUrl}/user/calendar/create`, {
158 154
      headers: {
159
        Authorization: 6,
160 155
        'Content-Type': 'application/json',
161 156
      },
162 157
      credentials: 'include',
......
168 163
    throw 'Server is not available'
169 164
  }
170 165

  
171
  if (response.ok) {
172
      
173
    response = await fetch(
174
    `${window.config.baseUrl}/users/requests/vacation?status=PENDING`, {
175
      headers: {
176
        Authorization: 1
177
      },
178
      credentials: 'include',
179
    });
180
    const data = await response.json();
181
      
182
    return data.map(request => {
183
      const a = request.date;
184
      const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
185

  
186
      return (
187
        {
188
          title: request.firstName + ' ' + request.lastName,
189
          id: request.id,
190
          type: request.type,
191
          start: b,
192
          end: null,
193
          status: request.status
194
        })
195
    })
196

  
197
  } else {
166
  if (!response.ok) {
198 167
      switch (response.status) {
199 168
        case 400:
200 169
          throw new Error('Bad request. Check request body.')
201 170
        case 401:
202
          window.location.replace("http://localhost:3000/login")
171
          window.location.replace(`/login`)
203 172
        case 403:
204
          window.location.replace("http://localhost:3000/login")
173
          window.location.replace(`/login`)
205 174
        case 500:
206 175
          throw new Error('Internal server error.')
207 176
        default:
......
219 188
  // send accepted request to server
220 189
    response = await fetch(`${window.config.baseUrl}/user/calendar/create`, {
221 190
      headers: {
222
        Authorization: 1,
223 191
        'Content-Type': 'application/json',
224 192
    },
225 193
    credentials: 'include',
......
236 204
      case 400:
237 205
        throw new Error('Bad request. Check request body.')
238 206
      case 401:
239
        window.location.replace("http://localhost:3000/login")
207
        window.location.replace(`/login`)
240 208
      case 403:
241
        window.location.replace("http://localhost:3000/login")
209
        window.location.replace(`/login`)
242 210
      case 500:
243 211
        throw new Error('Internal server error.')
244 212
      default:
......
255 223
  try {
256 224
  response = await fetch (
257 225
    `${window.config.baseUrl}/users`, {
258
      headers: {
259
        Authorization: 1          
260
      },
261 226
      credentials: 'include',
262 227
    }
263 228
  );
......
284 249
      case 400:
285 250
        throw new Error('Bad request. Check query parameters.')
286 251
      case 401:
287
        window.location.replace("http://localhost:3000/login")
252
        window.location.replace(`/login`)
288 253
      case 403:
289
        window.location.replace("http://localhost:3000/login")
254
        window.location.replace(`/login`)
290 255
      case 500:
291 256
        throw new Error('Internal server error.')
292 257
      default:
......
304 269
    // send accepted request to server
305 270
        response = await fetch(`${window.config.baseUrl}/user/settings`, {
306 271
          headers: {
307
            Authorization: 1,
308 272
            'Content-Type': 'application/json',
309 273
          },
310 274
          credentials: 'include',
......
322 286
      case 400:
323 287
        throw new Error('Bad request. Check query parameters.')
324 288
      case 401:
325
        window.location.replace("http://localhost:3000/login")
289
        window.location.replace(`/login`)
326 290
      case 403:
327
        window.location.replace("http://localhost:3000/login")
291
        window.location.replace(`/login`)
328 292
      case 500:
329 293
        throw new Error('Internal server error.')
330 294
      default:
......
340 304
  try {
341 305
    response = await fetch(
342 306
      `${window.config.baseUrl}/settings`, {
343
        headers: {
344
          Authorization: 1
345
        },
346 307
        credentials: 'include',
347 308
      }
348 309
    );
......
374 335
  try {
375 336
    response = await fetch(`${window.config.baseUrl}/settings`, {
376 337
      headers: {
377
        'Authorization': 1,
378 338
        'Content-Type': 'application/json'
379 339
      },
380 340
      credentials: 'include',
......
388 348
  if (!response.ok) {
389 349
    switch (response.status) {
390 350
      case 401:
391
        window.location.replace("http://localhost:3000/login")
351
        window.location.replace(`/login`)
392 352
      case 403:
393
        window.location.replace("http://localhost:3000/login")
353
        window.location.replace(`/login`)
394 354
      case 500:
395 355
        throw new Error('Internal server error.')
396 356
      default:
......
401 361

  
402 362
// ****************** LOAD DATA to YOUR REQUESTS - EMPLOYEE ******************
403 363

  
404
export async function loadYourRequests(userName) {
364
export async function loadYourRequests(currentUser) {
405 365
  let response;
406 366
  
407 367
  try {
408 368
    response = await fetch(
409
      `${window.config.baseUrl}/user/${userName.id}/calendar?from=2020/06/24&status=PENDING`, {
410
        headers: {
411
          Authorization: 6
412
        },
369
      `${window.config.baseUrl}/user/${currentUser.id}/calendar?from=2020/06/24&status=PENDING`, {
413 370
        credentials: 'include',
414 371
      }
415 372
    );
......
425 382
        case 400:
426 383
          throw new Error('Bad request. Check query parameters.')
427 384
        case 401:
428
          window.location.replace("http://localhost:3000/login")
385
          window.location.replace(`/login`)
429 386
        case 403:
430
          window.location.replace("http://localhost:3000/login")
387
          window.location.replace(`/login`)
431 388
        case 500:
432 389
          throw new Error('Internal server error.')
433 390
        default:
......
443 400
  try {
444 401
    response = await fetch(
445 402
      `${window.config.baseUrl}/users/requests/vacation?status=PENDING`, {
446
        headers: {
447
          Authorization: 1
448
        },
449 403
        credentials: 'include',
450 404
      },
451 405
    );
......
461 415
        case 400:
462 416
          throw new Error('Bad request. Check query parameters.')
463 417
        case 401:
464
          window.location.replace("http://localhost:3000/login")
418
          window.location.replace(`/login`)
465 419
        case 403:
466
          window.location.replace("http://localhost:3000/login")
420
          window.location.replace(`/login`)
467 421
        case 500:
468 422
          throw new Error('Internal server error.')
469 423
        default:
......
480 434
  try {
481 435
    response = await fetch(`${window.config.baseUrl}/user/requests?type=VACATION`, {
482 436
      headers: {
483
        Authorization: 1,
484 437
        'Content-Type': 'application/json',
485 438
      },
486 439
      credentials: 'include',
......
496 449
      case 400:
497 450
        throw new Error('Bad request. Check query parameters and request body.')
498 451
      case 401:
499
        window.location.replace("http://localhost:3000/login")
452
        window.location.replace(`/login`)
500 453
      case 403:
501
        window.location.replace("http://localhost:3000/login")
454
        window.location.replace(`/login`)
502 455
      case 404:
503 456
        throw new Error('Neither vacation nor authorization request with given ID exists.')
504 457
      case 500:
......
517 470
  try {
518 471
    response = await fetch(`${window.config.baseUrl}/user/requests?type=VACATION`, {
519 472
      headers: {
520
        Authorization: 1,
521 473
        'Content-Type': 'application/json',
522 474
      },
523 475
      credentials: 'include',
......
533 485
      case 400:
534 486
        throw new Error('Bad request. Check query parameters and request body.')
535 487
      case 401:
536
        window.location.replace("http://localhost:3000/login")
488
        window.location.replace(`/login`)
537 489
      case 403:
538
        window.location.replace("http://localhost:3000/login")
490
        window.location.replace(`/login`)
539 491
      case 404:
540 492
        throw new Error('Neither vacation nor authorization request with given ID exists.')
541 493
      case 500:

Také k dispozici: Unified diff