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 from './api';
|
11
|
|
12
|
const DEFAULT_MANDAY_HOURS = 7.5;
|
13
|
const SICK_DAY_COUNT_INCREMENT = 1;
|
14
|
|
15
|
export default function Calendar(props) {
|
16
|
|
17
|
useEffect(() => {
|
18
|
if (props.currentUser === undefined) {
|
19
|
return;
|
20
|
}
|
21
|
|
22
|
if (props.currentUser.role === 'EMPLOYER') {
|
23
|
api.getAdminCalendar().then(adminCalendar => {
|
24
|
props.setAcceptedRequest(adminCalendar);
|
25
|
}).catch(reason => {
|
26
|
alert(reason);
|
27
|
});
|
28
|
} else {
|
29
|
api.getUserCalendar(props.currentUser, convertedDate).then(userCalendar => {
|
30
|
props.setAcceptedRequest(userCalendar);
|
31
|
}).catch(reason => {
|
32
|
alert(reason);
|
33
|
});
|
34
|
}
|
35
|
}, [props.currentUser]);
|
36
|
|
37
|
//states
|
38
|
const [isOpen, setOpen] = useState(false);
|
39
|
const [whatDate, setDate] = useState('');
|
40
|
const [whatTime, setWhatTime] = useState(DEFAULT_MANDAY_HOURS);
|
41
|
const [typeRadio, setType] = useState('sickday');
|
42
|
|
43
|
var today = new Date();
|
44
|
|
45
|
// setting date to right format
|
46
|
today = today.toISOString().split('T')[0];
|
47
|
const convertedDate = today.split('-').join('/');
|
48
|
|
49
|
// ********************* ADD EVENT - EMPLOYEE **************************
|
50
|
|
51
|
async function addEvent(e) {
|
52
|
e.preventDefault();
|
53
|
|
54
|
try {
|
55
|
// setting an object
|
56
|
const newDate = whatDate.split('-').join('/');
|
57
|
|
58
|
const dataAddEventEmployee = {
|
59
|
type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
|
60
|
date: newDate,
|
61
|
from: typeRadio === 'sickday' ? null : '00:00',
|
62
|
to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, 'hours').format('hh:mm'),
|
63
|
};
|
64
|
|
65
|
await api.addEventApi(dataAddEventEmployee);
|
66
|
|
67
|
if (typeRadio === 'holiday') {
|
68
|
props.setCurrentUser({
|
69
|
...props.currentUser,
|
70
|
holiday: props.currentUser.holiday - whatTime,
|
71
|
});
|
72
|
} else if (typeRadio === 'sickday') {
|
73
|
props.setCurrentUser({
|
74
|
...props.currentUser,
|
75
|
takenSickday: props.currentUser.takenSickday + SICK_DAY_COUNT_INCREMENT,
|
76
|
});
|
77
|
}
|
78
|
|
79
|
setOpen(false);
|
80
|
} catch (e) {
|
81
|
alert(e);
|
82
|
}
|
83
|
}
|
84
|
|
85
|
// ********************* ADD EVENT ADMIN - EMPLOYER **************************
|
86
|
|
87
|
async function addEventAdmin(e) {
|
88
|
e.preventDefault();
|
89
|
|
90
|
// setting an object
|
91
|
const newDate = whatDate.split('-').join('/');
|
92
|
|
93
|
const dataAddEventAdmin = {
|
94
|
type: typeRadio === 'sickday' ? 'SICK_DAY' : 'VACATION',
|
95
|
date: newDate,
|
96
|
from: typeRadio === 'sickday' ? null : '00:00',
|
97
|
to: typeRadio === 'sickday' ? null : moment().startOf('day').add(whatTime, 'hours').format('hh:mm'),
|
98
|
};
|
99
|
|
100
|
api.addEventApiAdmin(dataAddEventAdmin).then(() => {
|
101
|
const userProps = {
|
102
|
title: props.currentUser.name,
|
103
|
start: whatDate,
|
104
|
};
|
105
|
//concat new request to current ones
|
106
|
props.setAcceptedRequest((acceptedRequest) => acceptedRequest.concat(userProps));
|
107
|
}).catch(reason => {
|
108
|
alert(reason);
|
109
|
});
|
110
|
|
111
|
setOpen(false);
|
112
|
|
113
|
api.getUsersOverview().then(usersOverview => {
|
114
|
props.setEmployees(usersOverview);
|
115
|
}).catch(reason => {
|
116
|
alert(reason);
|
117
|
});
|
118
|
}
|
119
|
|
120
|
return (
|
121
|
<div className="calendar">
|
122
|
<FullCalendar
|
123
|
defaultView="dayGridMonth"
|
124
|
plugins={[dayGridPlugin, interactionPlugin]}
|
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
|
<Popup
|
135
|
open={isOpen}
|
136
|
position="right center"
|
137
|
modal
|
138
|
closeOnDocumentClick
|
139
|
onClose={() => setOpen(false)}
|
140
|
>
|
141
|
<div className="calendar-form">
|
142
|
<form onSubmit={props.currentUser !== undefined && props.currentUser.role === 'EMPLOYER' ? (e) => addEventAdmin(e) : (e) => addEvent(e)}>
|
143
|
<h2>Choose vacation type</h2>
|
144
|
<div className="calendar-radio">
|
145
|
<input checked={typeRadio === 'sickday' ? 'checked' : null}
|
146
|
onChange={() => setType(typeRadio === 'holiday' ? 'sickday' : 'holiday')}
|
147
|
type="radio" id="sickday" name="radiobutton" value="sickday"
|
148
|
/>
|
149
|
<label htmlFor="sickday">Sickday</label>
|
150
|
</div>
|
151
|
<div className="calendar-radio">
|
152
|
<input checked={typeRadio === 'holiday' ? 'checked' : null}
|
153
|
onChange={() => setType(typeRadio === 'holiday' ? 'sickday' : 'holiday')}
|
154
|
type="radio" id="holiday" name="radiobutton" value="holiday"
|
155
|
/>
|
156
|
<label htmlFor="holiday">Extra Holiday</label>
|
157
|
</div>
|
158
|
<div>
|
159
|
{typeRadio === 'holiday' ? <h4>Date & Hours</h4> : <h4>Date</h4>}
|
160
|
<div className="row calendarInputs">
|
161
|
<input
|
162
|
className="date-input"
|
163
|
type='date' onChange={(e) => setDate(e.target.value)}
|
164
|
value={whatDate} min={today}
|
165
|
/>
|
166
|
{typeRadio === 'holiday' ? (
|
167
|
<input
|
168
|
className="input-time"
|
169
|
step={0.5}
|
170
|
min={0.5}
|
171
|
max={7.5}
|
172
|
type="number"
|
173
|
onChange={(e) => setWhatTime(e.target.value)}
|
174
|
required
|
175
|
value={whatTime}/>
|
176
|
) : null}
|
177
|
</div>
|
178
|
</div>
|
179
|
<button className="btn btn-submit" type="submit">Request vacation</button>
|
180
|
</form>
|
181
|
</div>
|
182
|
</Popup>
|
183
|
</div>
|
184
|
);
|
185
|
}
|