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/Calendar.js
16 16
    if (props.userName.id !== undefined) {
17 17
      props.userName.role === 'EMPLOYER'
18 18
        ?
19
          api_fetch.getAdminCalendar().then(adminCalendar => {
20
            props.setRequest(adminCalendar)
19
          api_fetch.getAdminCalendar().then(adminCalendar => { 
20
            props.setRequest(adminCalendar);
21
          }).catch(reason => {
22
            alert(reason)
21 23
          })
22 24
        :
23 25
          api_fetch.getUserCalendar(props.userName, todayTwo).then(userCalendar => {
24
            props.setRequest(userCalendar)
26
            props.setRequest(userCalendar);
27
          }).catch(reason => {
28
            alert(reason)
25 29
          });
26 30
    }
27 31
  }, [props.userName.id]);
28 32

  
29
  // LOAD DATA from server to calendar **** EMPLOYEE ****
30
  
31
  // LOAD DATA from server to calendar **** EMPLOYER ****
32

  
33
  //states
33
//states
34 34
  const [isOpen, setOpen] = useState(false)
35 35

  
36 36
  const [whatDate, setDate] = useState('')
......
41 41

  
42 42
  var today = new Date();
43 43

  
44
  // setting date to right format
44
// setting date to right format
45 45
  today = today.toISOString().split('T')[0]
46 46
  const todayTwo = today.split("-").join("/")
47 47

  
48
// LOAD DATA from server to calendar **** EMPLOYEE ****
49
  
50
// LOAD DATA from server to calendar **** EMPLOYER ****
51

  
48 52
// ********************* ADD EVENT - EMPLOYEE **************************
49 53

  
54
const addEvent = async (e) => {
55
  e.preventDefault();
50 56
  
57
  // setting an object
58
  const newDate = whatDate.split("-").join("/");
59

  
60
  const dataAddEventEmployee = {
61
    type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
62
    date: newDate,
63
    from: typeRadio === 'sickday' ? null : "00:00",
64
    to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
65
  }
66

  
67
  api_fetch.addEventApi(dataAddEventEmployee).catch(reason => {
68
    alert(reason)
69
  });
70
    
71
    setOpen(false)
72
  }
73
 
51 74
// ********************* ADD EVENT ADMIN - EMPLOYER **************************
52 75

  
53 76
const addEventAdmin = async (e) => {
......
56 79
// setting an object
57 80
  const newDate = whatDate.split("-").join("/");
58 81

  
59
  const peps = {
82
  const dataAddEventAdmin = {
60 83
    type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
61 84
    date: newDate,
62 85
    from: typeRadio === 'sickday' ? null : "00:00",
63 86
    to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
64 87
  };
65 88

  
66
  api_fetch.tvojefunkce(peps)
67
    
68
  try {
69
// send accepted request to server
70
    const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
71
      headers: {
72
        Authorization: 1,
73
        'Content-Type': 'application/json',
74
      },
75
      method: 'POST',
76
// object which is sent to server
77
      body: JSON.stringify(peps),
78
    });
79
    if (response.ok) {
80
  
81

  
82
  const userProps = {
83
    title: props.userName.name,
84
    start: whatDate
85
  
86
}
87
//concat new request to current ones
88
    props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
89
} else {
90
  if(response.status === 400) {
91
    alert('error 400 ADD EVENT ADMIN - EMPLOYER')
92
 }
93
    else if (response.status === 500) {
94
       alert ('error 500 ADD EVENT ADMIN - EMPLOYER')
95
    }
96
    else {
97
       alert('error ADD EVENT ADMIN - EMPLOYER')
98
    }
99
}
100
  } catch (e) {
101
    alert('error catch ADD EVENT ADMIN - EMPLOYER')
89
  api_fetch.addEventApiAdmin(dataAddEventAdmin).then(() => {
90
    const userProps = {
91
      title: props.userName.name, 
92
      start: whatDate      
102 93
  }
94
  //concat new request to current ones
95
      props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
96
  }).catch(reason => {
97
    alert(reason)
98
  });
103 99

  
104

  
105
  setOpen(false)}
100
  setOpen(false)
101
}
106 102
    
107 103

  
108 104
  return (
......
131 127
    <div className="calendar-form">
132 128
      {/* <form onSubmit={(e) => addEvent(e)}> */}
133 129
      <form onSubmit={props.userName.role === 'EMPLOYER' 
134
      ? (e) => addEventAdmin(e).then(eventAdmin => { props.setUser(eventAdmin)}) 
135
      : (e) => api_fetch.addEvent(e).then(userEvent => { props.setUser(userEvent)})
130
      ? (e) => addEventAdmin(e)
131
      : (e) => addEvent(e)
136 132
      }>
137 133
        <h2>Choose an option</h2>
138 134
        <div className="calendar-radio">

Také k dispozici: Unified diff