Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 7495b9eb

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

re #58 refactoring (api - app, calendar)

Zobrazit rozdíly:

client/src/Calendar.js
7 7
import interactionPlugin from '@fullcalendar/interaction';
8 8
import Popup from "reactjs-popup";
9 9
import moment from 'moment';
10
import * as api_fetch from './api'
10 11

  
11 12

  
12 13
function Calendar(props) {
13 14
  
14 15
  useEffect( () => {
15 16
    if (props.userName.id !== undefined) {
16
      props.userName.role === 'EMPLOYER' ? getDataAdmin() : getData();
17
      props.userName.role === 'EMPLOYER'
18
        ?
19
          api_fetch.getAdminCalendar().then(adminCalendar => {
20
            props.setRequest(adminCalendar)
21
          })
22
        :
23
          api_fetch.getUserCalendar(props.userName, todayTwo).then(userCalendar => {
24
            props.setRequest(userCalendar)
25
          });
17 26
    }
18 27
  }, [props.userName.id]);
19 28

  
20 29
  // LOAD DATA from server to calendar **** EMPLOYEE ****
21
  const getData = async () => {
22
    try {
23
    const response = await fetch(
24
      `http://devcz.yoso.fi:8090/ymanager/user/${props.userName.id}/calendar?from=${todayTwo}&status=ACCEPTED&status=REJECTED`, {
25
        headers: {
26
          'Accept': 'application/json',
27
          Authorization: 6
28
        },
29
        method: 'GET',
30
      }
31
    );
32

  
33
    if (response.ok) {
34
    const data = await response.json();
35
    
36
    props.setRequest(data.filter(day => {
37
      return day.status !== 'PENDING'
38
    }).map(day => {
39

  
40
      const newDate = day.date.split("/").join("-");
41

  
42
      return ({
43
      title: props.userName.name,
44
      start: newDate,
45
      backgroundColor: day.status === 'REJECTED' ? 'red' : 'green'
46
      })
47
    }))
48
  } else {
49
    if(response.status === 400) {
50
      alert('error 400 LOADING DATA (CALENDAR, EMPLOYEE)')
51
   }
52
      else if (response.status === 500) {
53
         alert ('error 500 LOADING DATA (CALENDAR, EMPLOYEE)')
54
      }
55
      else {
56
         alert('error LOADING DATA (CALENDAR, EMPLOYEE)')
57
      }
58
  }
59
  } catch (e) {
60
    alert('error catch LOADING DATA (CALENDAR, EMPLOYEE)')
61
  }
62
  }
30
  
63 31
  // LOAD DATA from server to calendar **** EMPLOYER ****
64
  const getDataAdmin = async () => {
65
    try {
66
    const response = await fetch(
67
      'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=ACCEPTED', {
68
        headers: {
69
          'Accept': 'application/json',
70
          Authorization: 1
71
        },
72
        method: 'GET',
73
      }
74
    );
75

  
76
    if (response.ok) {
77
    const data = await response.json();
78
    
79
    props.setRequest(data.map(day => {
80

  
81
      const newDate = day.date.split("/").join("-");
82

  
83
      return ( {
84
      title: day.firstName + ' ' + day.lastName,
85
      start: newDate
86
      })
87
    }))
88
  } else {
89
    if(response.status === 400) {
90
      alert('error 400 LOADING DATA (CALENDAR, EMPLOYER)')
91
   }
92
      else if (response.status === 500) {
93
         alert ('error 500 LOADING DATA (CALENDAR, EMPLOYER))')
94
      }
95
      else {
96
         alert('error LOADING DATA (CALENDAR, EMPLOYER)')
97
      }
98
  }
99
  } catch (e) {
100
    alert('error catch LOADING DATA (CALENDAR, EMPLOYER)')
101
  }
102
}
103

  
104 32

  
105 33
  //states
106 34
  const [isOpen, setOpen] = useState(false)
......
117 45
  today = today.toISOString().split('T')[0]
118 46
  const todayTwo = today.split("-").join("/")
119 47

  
120

  
121 48
// ********************* ADD EVENT - EMPLOYEE **************************
122 49

  
123
  const addEvent = async (e) => {
124
    e.preventDefault();
50
  
51
// ********************* ADD EVENT ADMIN - EMPLOYER **************************
52

  
53
const addEventAdmin = async (e) => {
54
  e.preventDefault();
125 55

  
126
  // setting an object
127
    const newDate = whatDate.split("-").join("/");
128
      
129
    try {
130
  // send accepted request to server
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({
139
          type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
140
          date: newDate,
141
          from: typeRadio === 'sickday' ? null : "00:00",
142
          to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
143
        }),
144
      });
145
      if (response.ok) {
56
// setting an object
57
  const newDate = whatDate.split("-").join("/");
146 58

  
147
      const response = await fetch(
148
        'http://devcz.yoso.fi:8090/ymanager/users/requests/vacation?status=PENDING', {
149
          headers: {
150
            Authorization: 1
151
          },
152
        }
153
  
154
       );
155
      const data = await response.json();
59
  const peps = {
60
    type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
61
    date: newDate,
62
    from: typeRadio === 'sickday' ? null : "00:00",
63
    to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
64
  };
156 65

  
157
      props.setUser(data.map(request => {
158
        const a = request.date;
159
        const b = [a.slice(0, 4), "-", a.slice(5, 7), "-", a.slice(8, 10)].join('');
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) {
160 80
  
161
        return (
162
          {
163
            title: request.firstName + ' ' + request.lastName,
164
            id: request.id,
165
            type: request.type,
166
            start: b,
167
            end: null,
168
            status: request.status
169
        })
170
      }))
171

  
172
  } else {
173 81

  
174
    if(response.status === 400) {
175
    alert('error 400 ADD EVENT - EMPLOYEE')
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')
176 92
 }
177 93
    else if (response.status === 500) {
178
       alert ('error 500 ADD EVENT - EMPLOYEE')
94
       alert ('error 500 ADD EVENT ADMIN - EMPLOYER')
179 95
    }
180 96
    else {
181
       alert('error ADD EVENT - EMPLOYEE')
182
    }
183
    
184
  }
185
    } catch (e) {
186
      alert('error catch ADD EVENT - EMPLOYEE')
97
       alert('error ADD EVENT ADMIN - EMPLOYER')
187 98
    }
188

  
189
    setOpen(false)}
190

  
191
  
192
// ********************* ADD EVENT ADMIN - EMPLOYER **************************
193

  
194
  const addEventAdmin = async (e) => {
195
    e.preventDefault();
196

  
197
  // setting an object
198
    const newDate = whatDate.split("-").join("/");
199
      
200
    try {
201
  // send accepted request to server
202
      const response = await fetch('http://devcz.yoso.fi:8090/ymanager/user/calendar/create', {
203
        headers: {
204
          Authorization: 1,
205
          'Content-Type': 'application/json',
206
        },
207
        method: 'POST',
208
  // object which is sent to server
209
        body: JSON.stringify({
210
          type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
211
          date: newDate,
212
          from: typeRadio === 'sickday' ? null : "00:00",
213
          to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
214
        }),
215
      });
216
      if (response.ok) {
217
    
218

  
219
    const userProps = {
220
      title: props.userName.name,
221
      start: whatDate
222
    
223
  }
224
  //concat new request to current ones
225
      props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
226
  } else {
227
    if(response.status === 400) {
228
      alert('error 400 ADD EVENT ADMIN - EMPLOYER')
229
   }
230
      else if (response.status === 500) {
231
         alert ('error 500 ADD EVENT ADMIN - EMPLOYER')
232
      }
233
      else {
234
         alert('error ADD EVENT ADMIN - EMPLOYER')
235
      }
99
}
100
  } catch (e) {
101
    alert('error catch ADD EVENT ADMIN - EMPLOYER')
236 102
  }
237
    } catch (e) {
238
      alert('error catch ADD EVENT ADMIN - EMPLOYER')
239
    }
240 103

  
241 104

  
242
    setOpen(false)}
105
  setOpen(false)}
243 106
    
244 107

  
245 108
  return (
......
267 130
    >
268 131
    <div className="calendar-form">
269 132
      {/* <form onSubmit={(e) => addEvent(e)}> */}
270
      <form onSubmit={props.userName.role === 'EMPLOYER' ? (e) => addEventAdmin(e) : (e) => addEvent(e) }>
133
      <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)})
136
      }>
271 137
        <h2>Choose an option</h2>
272 138
        <div className="calendar-radio">
273 139
          <input checked={

Také k dispozici: Unified diff