Projekt

Obecné

Profil

Stáhnout (5.48 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.userName.id !== undefined) {
17
      props.userName.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.userName, todayTwo).then(userCalendar => {
26
            props.setAcceptedRequest(userCalendar);
27
          }).catch(reason => {
28
            alert(reason)
29
          });
30
    }
31
  }, [props.userName.id]);
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 todayTwo = today.split("-").join("/")
47

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

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

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

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

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

    
88
const addEventAdmin = async (e) => {
89
  e.preventDefault();
90

    
91
// setting an object
92
  const newDate = whatDate.split("-").join("/");
93

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

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

    
112
  setOpen(false)
113

    
114
  api_fetch.getUsersOverview().then(usersOverview => {
115
    props.setEmployees(usersOverview);
116
    }).catch(reason => {
117
    alert(reason)
118
 });
119
}
120
    
121
  return (
122
    <div className="calendar">
123

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

    
126
    dateClick={function(info) {
127
      //setOpen(true === info.dateStr > today ? true : false )
128
      setOpen(info.dateStr > today)
129
      setDate(info.dateStr)
130
      setWhatTime(7.5)
131
    }}
132
    events={[
133
      ...props.acceptedRequest
134
  ]}
135
    />
136

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

    
193
    </div>
194
  );
195
  }
196

    
197

    
198
export default Calendar;
(4-4/17)