Projekt

Obecné

Profil

Stáhnout (5.46 KB) Statistiky
| Větev: | Tag: | Revize:
1
import React, { useState, useEffect } from 'react';
2
import './App.css';
3
import FullCalendar from '@fullcalendar/react'
4
import dayGridPlugin from '@fullcalendar/daygrid'
5
import '@fullcalendar/core/main.css'
6
import '@fullcalendar/daygrid/main.css'
7
import interactionPlugin from '@fullcalendar/interaction';
8
import Popup from "reactjs-popup";
9
import moment from 'moment';
10
import * as api_fetch from './api'
11

    
12

    
13
function Calendar(props) {
14
  
15
  useEffect( () => {
16
    if (props.currentUser !== undefined) {
17
      props.currentUser.role === 'EMPLOYER'
18
        ?
19
          api_fetch.getAdminCalendar().then(adminCalendar => { 
20
            props.setAcceptedRequest(adminCalendar);
21
          }).catch(reason => {
22
            alert(reason)
23
          })
24
        :
25
          api_fetch.getUserCalendar(props.currentUser, convertedDate).then(userCalendar => {
26
            props.setAcceptedRequest(userCalendar);
27
          }).catch(reason => {
28
            alert(reason)
29
          });
30
    }
31
  }, [props.currentUser]);
32

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

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

    
38
  const [whatTime, setWhatTime] = useState(7.5)
39

    
40
  const [typeRadio, setType] = useState('sickday')
41

    
42
  var today = new Date();
43

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

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

    
50
const addEvent = async (e) => {
51
  e.preventDefault();
52
  
53
  try {
54
  // setting an object
55
  const newDate = whatDate.split("-").join("/");
56

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

    
64
  await api_fetch.addEventApi(dataAddEventEmployee);
65
    if (typeRadio === 'holiday') {
66
      props.setCurrentUser({
67
        ...props.currentUser,
68
        holiday: props.currentUser.holiday - whatTime
69
      })
70
    } else if (typeRadio === 'sickday') {
71
      props.setCurrentUser({
72
        ...props.currentUser,
73
        takenSickday: props.currentUser.takenSickday + 1
74
      })
75
    }
76
    
77
    setOpen(false)
78
  } catch (e) {
79
    alert(e)
80
  }
81
}
82
// ********************* ADD EVENT ADMIN - EMPLOYER **************************
83

    
84
const addEventAdmin = async (e) => {
85
  e.preventDefault();
86

    
87
// setting an object
88
  const newDate = whatDate.split("-").join("/");
89

    
90
  const dataAddEventAdmin = {
91
    type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
92
    date: newDate,
93
    from: typeRadio === 'sickday' ? null : "00:00",
94
    to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, "hours").format("hh:mm"),
95
  };
96

    
97
  api_fetch.addEventApiAdmin(dataAddEventAdmin).then(() => {
98
    const userProps = {
99
      title: props.currentUser.name, 
100
      start: whatDate      
101
  }
102
  //concat new request to current ones
103
      props.setAcceptedRequest((acceptedRequest) => acceptedRequest.concat(userProps))
104
  }).catch(reason => {
105
      alert(reason)
106
  });
107

    
108
  setOpen(false)
109

    
110
  api_fetch.getUsersOverview().then(usersOverview => {
111
    props.setEmployees(usersOverview);
112
    }).catch(reason => {
113
    alert(reason)
114
 });
115
}
116

    
117
  const DEFAULT_MANDAY_HOURS = 7.5;
118
  
119
    
120
  return (
121
    <div className="calendar">
122

    
123
    <FullCalendar defaultView="dayGridMonth" plugins={[ dayGridPlugin, interactionPlugin ]}
124

    
125
    dateClick={function(info) {
126
      setOpen(info.dateStr > today)
127
      setDate(info.dateStr)
128
      setWhatTime(DEFAULT_MANDAY_HOURS)
129
    }}
130
    events={[
131
      ...props.acceptedRequest
132
  ]}
133
    />
134

    
135
    <Popup 
136
    open={isOpen}
137
    position="right center" 
138
    modal
139
    closeOnDocumentClick
140
    onClose={() => setOpen(false)}
141
    >
142
    <div className="calendar-form">
143
      <form onSubmit={props.currentUser !== undefined && props.currentUser.role === 'EMPLOYER' 
144
      ? (e) => addEventAdmin(e)
145
      : (e) => addEvent(e)
146
      }>
147
        <h2>Choose vacation type</h2>
148
        <div className="calendar-radio">
149
          <input checked={
150
            typeRadio === 'sickday' ? 'checked' : null}
151
            onChange={() => setType(typeRadio === 'holiday' ? 'sickday' : 'holiday')}
152
            type="radio" id="sickday" name="radiobutton" value="sickday"
153
          />
154
          <label for="sickday">Sickday</label>
155
        </div>
156
        <div className="calendar-radio">
157
          <input checked={
158
            typeRadio === 'holiday' ? 'checked' : null} 
159
            onChange={() => setType(typeRadio === 'holiday' ? 'sickday' : 'holiday')} 
160
            type="radio" id="holiday" name="radiobutton" value="holiday"
161
          />
162
          <label for="holiday">Extra Holiday</label>
163
        </div>
164
        <div>
165
          {typeRadio === 'holiday' ? <h4>Date & Hours</h4> : <h4>Date</h4>}
166
          <div className="row calendarInputs">
167
            <input 
168
              className="date-input" 
169
              type='date' onChange={(e) => setDate(e.target.value)} 
170
              value={whatDate} min={today} 
171
            />
172
            {typeRadio === 'holiday' ? 
173
            <input
174
            className="input-time"
175
            step={0.5}
176
            min={0.5}
177
            max={7.5}
178
            type="number"
179
            onChange={(e) => setWhatTime(e.target.value)}
180
            required
181
            value={whatTime}
182
          /> : null}
183
             </div> 
184
          </div>
185
        <button className="btn btn-submit" type="submit">Request vacation</button>
186
      </form>
187
    </div>
188
    </Popup>
189

    
190
    </div>
191
  );
192
  }
193

    
194

    
195
export default Calendar;
(4-4/18)