Projekt

Obecné

Profil

Stáhnout (5.01 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.setRequest(adminCalendar);
21
          }).catch(reason => {
22
            alert(reason)
23
          })
24
        :
25
          api_fetch.getUserCalendar(props.userName, todayTwo).then(userCalendar => {
26
            props.setRequest(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
  // 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
 
74
// ********************* ADD EVENT ADMIN - EMPLOYER **************************
75

    
76
const addEventAdmin = async (e) => {
77
  e.preventDefault();
78

    
79
// setting an object
80
  const newDate = whatDate.split("-").join("/");
81

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

    
89
  api_fetch.addEventApiAdmin(dataAddEventAdmin).then(() => {
90
    const userProps = {
91
      title: props.userName.name, 
92
      start: whatDate      
93
  }
94
  //concat new request to current ones
95
      props.setRequest((acceptedRequest) => acceptedRequest.concat(userProps))
96
  }).catch(reason => {
97
    alert(reason)
98
  });
99

    
100
  setOpen(false)
101
}
102
    
103

    
104
  return (
105
    <div className="calendar">
106

    
107
    <FullCalendar defaultView="dayGridMonth" plugins={[ dayGridPlugin, interactionPlugin ]}
108

    
109
    dateClick={function(info) {
110
      //setOpen(true === info.dateStr > today ? true : false )
111
      setOpen(info.dateStr > today)
112
      setDate(info.dateStr)
113
      setWhatTime(7.5)
114
    }}
115
    events={[
116
      ...props.acceptedRequest
117
  ]}
118
    />
119

    
120
    <Popup 
121
    open={isOpen}
122
    position="right center" 
123
    modal
124
    closeOnDocumentClick
125
    onClose={() => setOpen(false)}
126
    >
127
    <div className="calendar-form">
128
      {/* <form onSubmit={(e) => addEvent(e)}> */}
129
      <form onSubmit={props.userName.role === 'EMPLOYER' 
130
      ? (e) => addEventAdmin(e)
131
      : (e) => addEvent(e)
132
      }>
133
        <h2>Choose an option</h2>
134
        <div className="calendar-radio">
135
          <input checked={
136
            typeRadio === 'sickday' ? 'checked' : null}
137
            onChange={() => setType(typeRadio === 'holiday' ? 'sickday' : 'holiday')}
138
            type="radio" id="sickday" name="radiobutton" value="sickday"
139
          />
140
          <label for="sickday">Sickday</label>
141
        </div>
142
        <div className="calendar-radio">
143
          <input checked={
144
            typeRadio === 'holiday' ? 'checked' : null} 
145
            onChange={() => setType(typeRadio === 'holiday' ? 'sickday' : 'holiday')} 
146
            type="radio" id="holiday" name="radiobutton" value="holiday"
147
          />
148
          <label for="holiday">Extra Holiday</label>
149
        </div>
150
        <div>
151
          <h4>Date & Hours</h4>
152
          <div className="row">
153
            <input 
154
              className="date-input" 
155
              type='date' onChange={(e) => setDate(e.target.value)} 
156
              value={whatDate} min={today} 
157
            />
158
            {typeRadio === 'holiday' ? 
159
            <input
160
            className="input-time"
161
            step={0.5}
162
            min={0.5}
163
            max={7.5}
164
            type="number"
165
            onChange={(e) => setWhatTime(e.target.value)}
166
            required
167
            value={whatTime}
168
          /> : null}
169
             </div> 
170
          </div>
171
        <button className="btn btn-submit" type="submit">Submit</button>
172
      </form>
173
    </div>
174
    </Popup>
175

    
176
    </div>
177
  );
178
  }
179

    
180

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