Projekt

Obecné

Profil

« Předchozí | Další » 

Revize faed7f5e

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

re #58 conected to correct server, added config.json, added credentials in api.js and relocated errors 401, 403 to home page instead of throwing alert, edited login, edit link navbar logo

Zobrazit rozdíly:

client/public/config.json
1
{
2
    "baseUrl": "https://ymanager-api-yosoinfra.apps.oc.yoso.fi/api",
3
    "redirectUrl": "http://localhost:3000/"
4
}
client/src/App.css
56 56
    margin-left: 85px;
57 57
}
58 58

  
59
.link-nav {
60
    text-decoration: none;
61
    color: #fff; 
62
    cursor: pointer;  
63
}
64

  
59 65
.top-nav h4 {
60 66
    color: white;
61 67
}
......
292 298
    color: white;
293 299
    font-size: 90px;
294 300
}
301
.login-container a {
302
  
303
    text-decoration: none;
304
    color: #fff; 
305
    cursor: pointer;  
306

  
307
}
295 308
/* ------------- SETTING -------------------*/
296 309

  
297 310
.setting-container {
client/src/App.js
15 15
function App() {
16 16

  
17 17
  useEffect(() => {
18
    if (window.location.pathname === '/login') return;
19

  
18 20
    api_fetch.getCurrentProfile().then(currentProfile => {
19 21
      setUserName(currentProfile);
20 22
    }).catch(reason => {
client/src/Login.js
3 3

  
4 4

  
5 5
function Login() {
6

  
6
console.log(window.config)
7 7
  return (
8 8
    <div className="login-container column">
9
      <h1>log in</h1>
9
     
10
    <a href={window.config.baseUrl + '/login/google?target=' + window.config.redirectUrl}><h1>log in</h1></a>
11
     
12

  
10 13
    </div>
11 14
  )
12 15
}
client/src/Nav.js
6 6

  
7 7
  return (
8 8
    <nav className="top-nav">
9
      <h3>YOSO</h3>
9
      <a className="link-nav" href="http://localhost:3000/"><h3>YOSO</h3></a>
10 10
      <div className="profile-comp">
11 11
      <img src="https://cataas.com/cat/says/hello%20world!" className="img-avatar" alt="cat"></img>
12 12
        <h5>{props.userName.name}</h5>
client/src/api.js
1

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

  
4 1
// ******************** GET DATA APP getCurrentProfile ********************
5 2

  
6 3
export const getCurrentProfile = async () => {
......
9 6

  
10 7
  try {
11 8
    response = await fetch(
12
      `${http}/users/current/profile`, {
9
      `${window.config.baseUrl}/users/current/profile`, {
13 10
        headers: {
14 11
          Authorization: 1
15
          
16
        }
12

  
13
        },
14
        credentials: 'include'
17 15
      }
18 16
    );    
19 17
  } catch (e) {
......
34 32
  } else {
35 33
      switch (response.status) {
36 34
        case 401:
37
          throw new Error('Not authenticated.')
35
          window.location.replace("http://localhost:3000/login")
36
          break;
37
        case 403:
38
          window.location.replace("http://localhost:3000/login")
39
          break;
38 40
        case 500:
39 41
          throw new Error('Internal server error.')
40 42
        default:
......
50 52

  
51 53
  try {
52 54
    response = await fetch(
53
      `${http}/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
55
      `${window.config.baseUrl}/user/${userName.id}/calendar?from=${fromDate}&status=ACCEPTED&status=REJECTED`, {
54 56
        headers: {
55 57
          'Accept': 'application/json',
56 58
          Authorization: 6
57 59
        },
60
        credentials: 'include',
58 61
        method: 'GET',
59 62
      }
60 63
    );
......
82 85
        case 400:
83 86
          throw new Error('Bad request. Check query parameters.')
84 87
        case 401:
85
          throw new Error('Not authenticated.')
88
          window.location.replace("http://localhost:3000/login")
86 89
        case 403:
87
          throw new Error('Not authorized.')
90
          window.location.replace("http://localhost:3000/login")
88 91
        case 404:
89 92
          throw new Error('User with given ID does not exist.')
90 93
        case 500:
......
103 106

  
104 107
  try {
105 108
    response = await fetch(
106
      `${http}/users/requests/vacation?status=ACCEPTED`, {
109
      `${window.config.baseUrl}/users/requests/vacation?status=ACCEPTED`, {
107 110
        headers: {
108 111
          'Accept': 'application/json',
109
          Authorization: 1
112
          Authorization: 6
110 113
        },
114
        credentials: 'include',
111 115
        method: 'GET',
112 116
      }
113 117
    );
......
132 136
        case 400:
133 137
          throw new Error('Bad request. Check query parameters.')
134 138
        case 401:
135
          throw new Error('Not authenticated.')
139
          window.location.replace("http://localhost:3000/login")
136 140
        case 403:
137
          throw new Error('Not authorized.')
141
          window.location.replace("http://localhost:3000/login")
138 142
        case 500:
139 143
          throw new Error('Internal server error.')
140 144
        default:
......
145 149

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

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

  
151 155
  try {
152 156
  // send accepted request to server
153
    response = await fetch(`${http}/user/calendar/create`, {
157
    response = await fetch(`${window.config.baseUrl}/user/calendar/create`, {
154 158
      headers: {
155 159
        Authorization: 6,
156 160
        'Content-Type': 'application/json',
157 161
      },
162
      credentials: 'include',
158 163
      method: 'POST',
159 164
  // object which is sent to server
160 165
  body: JSON.stringify(dataAddEventEmployee),
......
166 171
  if (response.ok) {
167 172
      
168 173
    response = await fetch(
169
    `${http}/users/requests/vacation?status=PENDING`, {
174
    `${window.config.baseUrl}/users/requests/vacation?status=PENDING`, {
170 175
      headers: {
171 176
        Authorization: 1
172 177
      },
178
      credentials: 'include',
173 179
    });
174 180
    const data = await response.json();
175 181
      
......
193 199
        case 400:
194 200
          throw new Error('Bad request. Check request body.')
195 201
        case 401:
196
          throw new Error('Not authenticated.')
202
          window.location.replace("http://localhost:3000/login")
197 203
        case 403:
198
          throw new Error('Not authorized.')
204
          window.location.replace("http://localhost:3000/login")
199 205
        case 500:
200 206
          throw new Error('Internal server error.')
201 207
        default:
......
211 217

  
212 218
  try {
213 219
  // send accepted request to server
214
    response = await fetch(`${http}/user/calendar/create`, {
220
    response = await fetch(`${window.config.baseUrl}/user/calendar/create`, {
215 221
      headers: {
216 222
        Authorization: 1,
217 223
        'Content-Type': 'application/json',
218 224
    },
225
    credentials: 'include',
219 226
      method: 'POST',
220 227
    // object which is sent to server
221 228
      body: JSON.stringify(dataAddEventAdmin),
......
229 236
      case 400:
230 237
        throw new Error('Bad request. Check request body.')
231 238
      case 401:
232
        throw new Error('Not authenticated.')
239
        window.location.replace("http://localhost:3000/login")
233 240
      case 403:
234
        throw new Error('Not authorized.')
241
        window.location.replace("http://localhost:3000/login")
235 242
      case 500:
236 243
        throw new Error('Internal server error.')
237 244
      default:
......
247 254

  
248 255
  try {
249 256
  response = await fetch (
250
    `${http}/users`, {
257
    `${window.config.baseUrl}/users`, {
251 258
      headers: {
252
        Authorization: 1          }
259
        Authorization: 1          
260
      },
261
      credentials: 'include',
253 262
    }
254 263
  );
255 264
  } catch (e) {
......
275 284
      case 400:
276 285
        throw new Error('Bad request. Check query parameters.')
277 286
      case 401:
278
        throw new Error('Not authenticated.')
287
        window.location.replace("http://localhost:3000/login")
279 288
      case 403:
280
        throw new Error('Not authorized.')
289
        window.location.replace("http://localhost:3000/login")
281 290
      case 500:
282 291
        throw new Error('Internal server error.')
283 292
      default:
......
293 302

  
294 303
  try {
295 304
    // send accepted request to server
296
        response = await fetch(`${http}/user/settings`, {
305
        response = await fetch(`${window.config.baseUrl}/user/settings`, {
297 306
          headers: {
298 307
            Authorization: 1,
299 308
            'Content-Type': 'application/json',
300 309
          },
310
          credentials: 'include',
301 311
          method: 'PUT',
302 312
 
303 313
    // object which is sent to server
......
312 322
      case 400:
313 323
        throw new Error('Bad request. Check query parameters.')
314 324
      case 401:
315
        throw new Error('Not authenticated.')
325
        window.location.replace("http://localhost:3000/login")
316 326
      case 403:
317
        throw new Error('Not authorized.')
327
        window.location.replace("http://localhost:3000/login")
318 328
      case 500:
319 329
        throw new Error('Internal server error.')
320 330
      default:
......
329 339

  
330 340
  try {
331 341
    response = await fetch(
332
      `${http}/settings`, {
342
      `${window.config.baseUrl}/settings`, {
333 343
        headers: {
334 344
          Authorization: 1
345
        },
346
        credentials: 'include',
347
      }
348
    );
335 349

  
336
        }
337
      });
338 350
  } catch (e) {
339 351
    throw 'Server is not available'
340 352
    }
......
360 372
  let response;
361 373

  
362 374
  try {
363
    response = await fetch(`${http}/settings`, {
375
    response = await fetch(`${window.config.baseUrl}/settings`, {
364 376
      headers: {
365 377
        'Authorization': 1,
366 378
        'Content-Type': 'application/json'
367 379
      },
380
      credentials: 'include',
368 381
      method: 'POST',
369 382
      body: JSON.stringify(dataSettingObject),
370 383
    });
......
375 388
  if (!response.ok) {
376 389
    switch (response.status) {
377 390
      case 401:
378
        throw new Error('Not authenticated.')
391
        window.location.replace("http://localhost:3000/login")
379 392
      case 403:
380
        throw new Error('Not authorized.')
393
        window.location.replace("http://localhost:3000/login")
381 394
      case 500:
382 395
        throw new Error('Internal server error.')
383 396
      default:
......
388 401

  
389 402
// ****************** LOAD DATA to YOUR REQUESTS - EMPLOYEE ******************
390 403

  
391
export async function loadYourRequests() {
404
export async function loadYourRequests(userName) {
392 405
  let response;
393 406
  
394 407
  try {
395 408
    response = await fetch(
396
      `${http}/user/6/calendar?from=2020/06/24&status=PENDING`, {
409
      `${window.config.baseUrl}/user/${userName.id}/calendar?from=2020/06/24&status=PENDING`, {
397 410
        headers: {
398 411
          Authorization: 6
399 412
        },
413
        credentials: 'include',
400 414
      }
401 415
    );
402 416
  } catch (e) {
......
411 425
        case 400:
412 426
          throw new Error('Bad request. Check query parameters.')
413 427
        case 401:
414
          throw new Error('Not authenticated.')
428
          window.location.replace("http://localhost:3000/login")
415 429
        case 403:
416
          throw new Error('Not authorized.')
430
          window.location.replace("http://localhost:3000/login")
417 431
        case 500:
418 432
          throw new Error('Internal server error.')
419 433
        default:
......
428 442

  
429 443
  try {
430 444
    response = await fetch(
431
      `${http}/users/requests/vacation?status=PENDING`, {
445
      `${window.config.baseUrl}/users/requests/vacation?status=PENDING`, {
432 446
        headers: {
433 447
          Authorization: 1
434
        }
448
        },
449
        credentials: 'include',
435 450
      },
436 451
    );
437 452
  } catch (e) {
......
446 461
        case 400:
447 462
          throw new Error('Bad request. Check query parameters.')
448 463
        case 401:
449
          throw new Error('Not authenticated.')
464
          window.location.replace("http://localhost:3000/login")
450 465
        case 403:
451
          throw new Error('Not authorized.')
466
          window.location.replace("http://localhost:3000/login")
452 467
        case 500:
453 468
          throw new Error('Internal server error.')
454 469
        default:
......
463 478
  let response;
464 479

  
465 480
  try {
466
    response = await fetch(`${http}/user/requests?type=VACATION`, {
481
    response = await fetch(`${window.config.baseUrl}/user/requests?type=VACATION`, {
467 482
      headers: {
468 483
        Authorization: 1,
469 484
        'Content-Type': 'application/json',
470 485
      },
486
      credentials: 'include',
471 487
      method: 'PUT',
472 488
      body: JSON.stringify(acceptedRequests),
473 489
    });
......
480 496
      case 400:
481 497
        throw new Error('Bad request. Check query parameters and request body.')
482 498
      case 401:
483
        throw new Error('Not authenticated.')
499
        window.location.replace("http://localhost:3000/login")
484 500
      case 403:
485
        throw new Error('Not authorized.')
501
        window.location.replace("http://localhost:3000/login")
486 502
      case 404:
487 503
        throw new Error('Neither vacation nor authorization request with given ID exists.')
488 504
      case 500:
......
499 515
  let response;
500 516
  
501 517
  try {
502
    response = await fetch(`${http}/user/requests?type=VACATION`, {
518
    response = await fetch(`${window.config.baseUrl}/user/requests?type=VACATION`, {
503 519
      headers: {
504 520
        Authorization: 1,
505 521
        'Content-Type': 'application/json',
506 522
      },
523
      credentials: 'include',
507 524
      method: 'PUT',
508 525
      body: JSON.stringify(rejectedRequest),
509 526
    });
......
516 533
      case 400:
517 534
        throw new Error('Bad request. Check query parameters and request body.')
518 535
      case 401:
519
        throw new Error('Not authenticated.')
536
        window.location.replace("http://localhost:3000/login")
520 537
      case 403:
521
        throw new Error('Not authorized.')
538
        window.location.replace("http://localhost:3000/login")
522 539
      case 404:
523 540
        throw new Error('Neither vacation nor authorization request with given ID exists.')
524 541
      case 500:
client/src/index.js
3 3
import './index.css';
4 4
import App from './App';
5 5
import * as serviceWorker from './serviceWorker';
6
import { render } from '@testing-library/react';
7

  
8
fetch(
9
  "config.json").then((response) => response.json()).then(config => {
10
    window.config = config;
11
    ReactDOM.render(
12
      <React.StrictMode>
13
        <App />
14
      </React.StrictMode>,
15
      document.getElementById('root')
16
    );
17
    
18
  });
19

  
20

  
6 21

  
7
ReactDOM.render(
8
  <React.StrictMode>
9
    <App />
10
  </React.StrictMode>,
11
  document.getElementById('root')
12
);
13 22

  
14 23
// If you want your app to work offline and load faster, you can change
15 24
// unregister() to register() below. Note this comes with some pitfalls.

Také k dispozici: Unified diff