Projekt

Obecné

Profil

Stáhnout (5.08 KB) Statistiky
| Větev: | Tag: | Revize:
1 c46ffe2f plundrichov
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 7495b9eb plundrichov
import * as api_fetch from './api'
11 c46ffe2f plundrichov
12
13
function Calendar(props) {
14
  
15
  useEffect( () => {
16
    if (props.userName.id !== undefined) {
17 7495b9eb plundrichov
      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
          });
26 c46ffe2f plundrichov
    }
27
  }, [props.userName.id]);
28
29
  // LOAD DATA from server to calendar **** EMPLOYEE ****
30 7495b9eb plundrichov
  
31 c46ffe2f plundrichov
  // LOAD DATA from server to calendar **** EMPLOYER ****
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
// ********************* ADD EVENT - EMPLOYEE **************************
49
50 7495b9eb plundrichov
  
51
// ********************* ADD EVENT ADMIN - EMPLOYER **************************
52
53
const addEventAdmin = async (e) => {
54
  e.preventDefault();
55 c46ffe2f plundrichov
56 7495b9eb plundrichov
// setting an object
57
  const newDate = whatDate.split("-").join("/");
58 c46ffe2f plundrichov
59 7495b9eb plundrichov
  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
  };
65 c46ffe2f plundrichov
66 7495b9eb plundrichov
  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 c46ffe2f plundrichov
  
81
82 7495b9eb plundrichov
  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 c46ffe2f plundrichov
 }
93
    else if (response.status === 500) {
94 7495b9eb plundrichov
       alert ('error 500 ADD EVENT ADMIN - EMPLOYER')
95 c46ffe2f plundrichov
    }
96
    else {
97 7495b9eb plundrichov
       alert('error ADD EVENT ADMIN - EMPLOYER')
98 c46ffe2f plundrichov
    }
99 7495b9eb plundrichov
}
100
  } catch (e) {
101
    alert('error catch ADD EVENT ADMIN - EMPLOYER')
102 c46ffe2f plundrichov
  }
103
104
105 7495b9eb plundrichov
  setOpen(false)}
106 c46ffe2f plundrichov
    
107
108
  return (
109
    <div className="calendar">
110
111
    <FullCalendar defaultView="dayGridMonth" plugins={[ dayGridPlugin, interactionPlugin ]}
112
113
    dateClick={function(info) {
114
      //setOpen(true === info.dateStr > today ? true : false )
115
      setOpen(info.dateStr > today)
116
      setDate(info.dateStr)
117
      setWhatTime(7.5)
118
    }}
119
    events={[
120
      ...props.acceptedRequest
121
  ]}
122
    />
123
124
    <Popup 
125
    open={isOpen}
126
    position="right center" 
127
    modal
128
    closeOnDocumentClick
129
    onClose={() => setOpen(false)}
130
    >
131
    <div className="calendar-form">
132
      {/* <form onSubmit={(e) => addEvent(e)}> */}
133 7495b9eb plundrichov
      <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
      }>
137 c46ffe2f plundrichov
        <h2>Choose an option</h2>
138
        <div className="calendar-radio">
139
          <input checked={
140
            typeRadio === 'sickday' ? 'checked' : null}
141
            onChange={() => setType(typeRadio === 'holiday' ? 'sickday' : 'holiday')}
142
            type="radio" id="sickday" name="radiobutton" value="sickday"
143
          />
144
          <label for="sickday">Sickday</label>
145
        </div>
146
        <div className="calendar-radio">
147
          <input checked={
148
            typeRadio === 'holiday' ? 'checked' : null} 
149
            onChange={() => setType(typeRadio === 'holiday' ? 'sickday' : 'holiday')} 
150
            type="radio" id="holiday" name="radiobutton" value="holiday"
151
          />
152
          <label for="holiday">Extra Holiday</label>
153
        </div>
154
        <div>
155
          <h4>Date & Hours</h4>
156
          <div className="row">
157
            <input 
158
              className="date-input" 
159
              type='date' onChange={(e) => setDate(e.target.value)} 
160
              value={whatDate} min={today} 
161
            />
162
            {typeRadio === 'holiday' ? 
163
            <input
164
            className="input-time"
165
            step={0.5}
166
            min={0.5}
167
            max={7.5}
168
            type="number"
169
            onChange={(e) => setWhatTime(e.target.value)}
170
            required
171
            value={whatTime}
172
          /> : null}
173
             </div> 
174
          </div>
175
        <button className="btn btn-submit" type="submit">Submit</button>
176
      </form>
177
    </div>
178
    </Popup>
179
180
    </div>
181
  );
182
  }
183
184
185
export default Calendar;