1 |
c46ffe2f
|
plundrichov
|
import React, { useState, useEffect } from 'react';
|
2 |
|
|
import './App.css';
|
3 |
1865a0be
|
Pavel Fidransky
|
import FullCalendar from '@fullcalendar/react';
|
4 |
|
|
import dayGridPlugin from '@fullcalendar/daygrid';
|
5 |
|
|
import '@fullcalendar/core/main.css';
|
6 |
|
|
import '@fullcalendar/daygrid/main.css';
|
7 |
c46ffe2f
|
plundrichov
|
import interactionPlugin from '@fullcalendar/interaction';
|
8 |
1865a0be
|
Pavel Fidransky
|
import Popup from 'reactjs-popup';
|
9 |
c46ffe2f
|
plundrichov
|
import moment from 'moment';
|
10 |
1865a0be
|
Pavel Fidransky
|
import * as api from './api';
|
11 |
c46ffe2f
|
plundrichov
|
|
12 |
1865a0be
|
Pavel Fidransky
|
const DEFAULT_MANDAY_HOURS = 7.5;
|
13 |
|
|
const SICK_DAY_COUNT_INCREMENT = 1;
|
14 |
c46ffe2f
|
plundrichov
|
|
15 |
1865a0be
|
Pavel Fidransky
|
export default function Calendar(props) {
|
16 |
c46ffe2f
|
plundrichov
|
|
17 |
1865a0be
|
Pavel Fidransky
|
useEffect(() => {
|
18 |
|
|
if (props.currentUser === undefined) {
|
19 |
|
|
return;
|
20 |
|
|
}
|
21 |
9f045397
|
plundrichov
|
|
22 |
1865a0be
|
Pavel Fidransky
|
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 |
c46ffe2f
|
plundrichov
|
|
37 |
1865a0be
|
Pavel Fidransky
|
//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 |
9f045397
|
plundrichov
|
|
43 |
1865a0be
|
Pavel Fidransky
|
var today = new Date();
|
44 |
9f045397
|
plundrichov
|
|
45 |
1865a0be
|
Pavel Fidransky
|
// 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 |
ebfe6347
|
plundrichov
|
}
|
83 |
9f045397
|
plundrichov
|
}
|
84 |
7495b9eb
|
plundrichov
|
|
85 |
1865a0be
|
Pavel Fidransky
|
// ********************* ADD EVENT ADMIN - EMPLOYER **************************
|
86 |
c46ffe2f
|
plundrichov
|
|
87 |
1865a0be
|
Pavel Fidransky
|
async function addEventAdmin(e) {
|
88 |
|
|
e.preventDefault();
|
89 |
c46ffe2f
|
plundrichov
|
|
90 |
1865a0be
|
Pavel Fidransky
|
// setting an object
|
91 |
|
|
const newDate = whatDate.split('-').join('/');
|
92 |
c46ffe2f
|
plundrichov
|
|
93 |
1865a0be
|
Pavel Fidransky
|
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 |
c46ffe2f
|
plundrichov
|
|
100 |
1865a0be
|
Pavel Fidransky
|
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 |
ebfe6347
|
plundrichov
|
|
113 |
1865a0be
|
Pavel Fidransky
|
api.getUsersOverview().then(usersOverview => {
|
114 |
|
|
props.setEmployees(usersOverview);
|
115 |
ebfe6347
|
plundrichov
|
}).catch(reason => {
|
116 |
1865a0be
|
Pavel Fidransky
|
alert(reason);
|
117 |
|
|
});
|
118 |
|
|
}
|
119 |
5bedee9e
|
plundrichov
|
|
120 |
c46ffe2f
|
plundrichov
|
return (
|
121 |
|
|
<div className="calendar">
|
122 |
1865a0be
|
Pavel Fidransky
|
<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 |
c46ffe2f
|
plundrichov
|
</div>
|
182 |
1865a0be
|
Pavel Fidransky
|
</Popup>
|
183 |
c46ffe2f
|
plundrichov
|
</div>
|
184 |
|
|
);
|
185 |
1865a0be
|
Pavel Fidransky
|
}
|