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/Setting.js
1 1
import React, { useState, useEffect } from 'react';
2 2
import './App.css';
3 3
import { Link } from 'react-router-dom';
4
import * as api_fetch from './api'
4 5

  
5 6

  
6 7
function Setting() {
......
8 9
  const [sickdays, setSickdays] = useState([]);
9 10

  
10 11
  useEffect( () => {
11
    getData();
12
    api_fetch.getSettingData().then(settingData => {
13
      setSetting(settingData);
14
    }).catch(reason => {
15
      alert(reason)
16
    })
12 17
  }, []);
13 18

  
14
  const getData = async () =>  {
15
    try {
16
      const response = await fetch(
17
        'http://devcz.yoso.fi:8090/ymanager/settings', {
18
          headers: {
19
            Authorization: 1
20
          }
21
        }
22
        );
23

  
24
        if (response.ok) {
25
        const data = await response.json();
26
        setSetting({
27
          sickday: data.sickDayCount,
28
        })
29
      } else {
30
        if(response.status === 400) {
31
          alert('error 400')
32
       }
33
          else if (response.status === 500) {
34
             alert ('error 500')
35
          }
36
          else {
37
             alert('spatne neco jinyho')
38
          }
39
      }
40
    } catch (e) {
41
      console.log(e)
42
      alert('spatne vsechno')
43
      }
44
  }
45

  
46 19
  const submitSetting = async (e) => {
47 20
    e.preventDefault();
48
    try {
49
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/settings', {
50
      headers: {
51
        'Authorization': 1,
52
        'Content-Type': 'application/json'
53
      },
54
      method: 'POST',
55
      body: JSON.stringify({
56
        "sickDayCount": Number(setting.sickday),
57
        "notification": "2019/12/01 12:00:00"
58
      }),
59
    });
60

  
61

  
62
    if (response.ok) {
63
  } else {
64
    if(response.status === 400) {
65
      alert('error 400')
66
   }
67
      else if (response.status === 500) {
68
         alert ('error 500')
69
      }
70
      else {
71
         alert('spatne neco jinyho')
72
      }
73
  }
74
} catch (e) {
75
  console.log(e)
76
  alert('spatne vsechno')
77
  }
21
    
22
    const dataSettingObject = {
23
      "sickDayCount": Number(setting.sickday),
24
      "notification": "2019/12/01 12:00:00"  
25
    }
78 26

  
27
    api_fetch.saveDataSetting(dataSettingObject).catch(reason => {
28
      alert(reason)
29
    })
79 30
  }
80

  
81

  
82

  
31
  // states
83 32
  const [setting, setSetting] = useState(
84 33
    {sickday: 5,
85 34
    holiday: 0
86 35
    }
87 36
  ) 
88 37

  
38
  //functions
89 39
  function changeSickday(newValue) {
90 40
    setSetting(
91 41
       {sickday: newValue,
92 42
        holiday: setting.holiday
93
      }
94
    )
43
    })
95 44
  }
96 45

  
97 46
  function changeHoliday(newValue) {
98 47
    setSetting(
99 48
      {sickday: setting.sickday,
100 49
       holiday: newValue
101
      }
102
    )
50
    })
103 51
  }
104 52

  
105 53
  return (

Také k dispozici: Unified diff