2020-06-02 14:17:17 +00:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-06-02 13:42:48 +00:00
|
|
|
import { Container, Form, FormControl, FormGroup, Input, Button } from 'rsuite';
|
2020-06-02 14:17:17 +00:00
|
|
|
import { DateTime } from 'luxon';
|
2020-05-27 13:51:41 +00:00
|
|
|
|
2020-05-27 18:49:49 +00:00
|
|
|
import TimezonePicker from '../components/General/TimezonePicker';
|
2020-05-28 13:42:36 +00:00
|
|
|
import DaySelector from '../components/Schedule/DaySelector';
|
2020-06-02 09:17:10 +00:00
|
|
|
import DurationSelector from '../components/Schedule/DurationSelector';
|
2020-06-02 10:48:28 +00:00
|
|
|
import SelectedDates from '../components/Schedule/SelectedDates';
|
2020-05-27 13:51:41 +00:00
|
|
|
|
|
|
|
export default function Schedule() {
|
2020-06-02 14:17:17 +00:00
|
|
|
const [eventsList, setEventsList] = useState([]);
|
|
|
|
const [datesList, setDatesList] = useState(eventsToDates(eventsList));
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// Update selected dates
|
|
|
|
let updatedDates = eventsToDates(eventsList);
|
|
|
|
setDatesList(updatedDates);
|
|
|
|
}, [eventsList]);
|
|
|
|
|
|
|
|
const handleSelect = (info) => {
|
|
|
|
let updatedEvents = [];
|
|
|
|
let datesList = new Set();
|
|
|
|
|
|
|
|
/// Add each date selected to datesList
|
|
|
|
let start = DateTime.fromISO(info.startStr);
|
|
|
|
let end = DateTime.fromISO(info.endStr);
|
|
|
|
|
|
|
|
// Check if selection contains only one day
|
|
|
|
let singleDay = end.toISODate() === start.plus({ days: 1 }).toISODate();
|
|
|
|
|
|
|
|
if (singleDay) {
|
|
|
|
// When a single date is selected
|
|
|
|
let selectedDate = info.startStr;
|
|
|
|
let selectedEvent = { start: selectedDate, display: 'background' };
|
|
|
|
|
|
|
|
// If selectedEvent exists in the list, find and set its index
|
|
|
|
let selectedEventIndex;
|
|
|
|
for (let index = 0; index < eventsList.length; index++) {
|
|
|
|
const eventDate = eventsList[index].start;
|
|
|
|
if (selectedDate === eventDate) {
|
|
|
|
// If it's the case, set current index to selectedEventIndex
|
|
|
|
selectedEventIndex = index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectedEventIndex !== undefined) {
|
|
|
|
// If selectedEventIndex exists, remove the corresponding event from the list
|
|
|
|
eventsList.splice(selectedEventIndex, 1);
|
|
|
|
updatedEvents = [...eventsList];
|
|
|
|
} else {
|
|
|
|
// Add selected event to the list
|
|
|
|
updatedEvents = [...eventsList, selectedEvent];
|
|
|
|
}
|
|
|
|
// setEventsList(updatedEvents);
|
|
|
|
} else {
|
|
|
|
// When a range of dates is selected
|
|
|
|
let currentDate = start;
|
|
|
|
while (!(+end === +currentDate)) {
|
|
|
|
// Add currentDate to the datesList
|
|
|
|
datesList.add(currentDate.toISODate());
|
|
|
|
let newDate = currentDate.plus({ days: 1 });
|
|
|
|
currentDate = newDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add each date from the eventsList to datesList
|
|
|
|
eventsList.forEach((event) => {
|
|
|
|
datesList.add(event.start);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create events from datesList and add them to updatedEvents
|
|
|
|
datesList.forEach((date) => {
|
|
|
|
updatedEvents.push({ start: date, display: 'background' });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setEventsList(updatedEvents);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleClear = () => {
|
|
|
|
setEventsList([]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDelete = (date) => {
|
|
|
|
let currentEvent = {
|
|
|
|
start: date.toFormat('yyyy-MM-dd'),
|
|
|
|
display: 'background',
|
|
|
|
};
|
|
|
|
|
|
|
|
// Find the event corresponding to the date
|
|
|
|
let selectedEventIndex;
|
|
|
|
for (let index = 0; index < eventsList.length; index++) {
|
|
|
|
const eventDate = eventsList[index].start;
|
|
|
|
if (currentEvent.start === eventDate) {
|
|
|
|
// When it's the case, set current index to selectedEventIndex
|
|
|
|
selectedEventIndex = index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create updated eventsList
|
|
|
|
eventsList.splice(selectedEventIndex, 1);
|
|
|
|
let updatedEvents = [...eventsList];
|
|
|
|
// Update the eventsList
|
|
|
|
setEventsList(updatedEvents);
|
|
|
|
};
|
|
|
|
|
2020-05-27 13:51:41 +00:00
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<h3>Schedule a meeting</h3>
|
2020-05-27 18:49:49 +00:00
|
|
|
<Form>
|
2020-05-27 13:51:41 +00:00
|
|
|
<FormGroup>
|
|
|
|
<FormControl name='title' type='text' placeholder='title' />
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup>
|
|
|
|
<Input
|
|
|
|
name='description'
|
|
|
|
componentClass='textarea'
|
|
|
|
type='text'
|
|
|
|
rows={3}
|
|
|
|
placeholder='Description'
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup>
|
2020-05-27 18:49:49 +00:00
|
|
|
<TimezonePicker />
|
2020-05-27 13:51:41 +00:00
|
|
|
</FormGroup>
|
2020-05-28 13:08:31 +00:00
|
|
|
<FormGroup style={{ width: 200 }}>
|
2020-06-02 09:17:10 +00:00
|
|
|
<DurationSelector />
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup>
|
2020-06-02 14:17:17 +00:00
|
|
|
<DaySelector
|
|
|
|
eventsList={eventsList}
|
|
|
|
handleSelect={handleSelect}
|
|
|
|
handleClear={handleClear}
|
|
|
|
/>
|
2020-06-02 13:42:48 +00:00
|
|
|
</FormGroup>
|
2020-06-02 14:17:17 +00:00
|
|
|
{datesList.length > 0 && (
|
|
|
|
<FormGroup>
|
|
|
|
<SelectedDates
|
|
|
|
datesList={datesList}
|
|
|
|
eventsList={eventsList}
|
|
|
|
handleDelete={handleDelete}
|
|
|
|
/>
|
|
|
|
<Button>Confirm dates</Button>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
2020-05-27 13:51:41 +00:00
|
|
|
</Form>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}
|
2020-06-02 14:17:17 +00:00
|
|
|
|
|
|
|
// Convert events to Luxon Datetime objects
|
|
|
|
const eventsToDates = (events) => {
|
|
|
|
let dates = [];
|
|
|
|
events.forEach((event) => {
|
|
|
|
dates.push(DateTime.fromFormat(event.start, 'yyyy-MM-dd'));
|
|
|
|
});
|
|
|
|
return dates;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Convert Luxon Datetime objects to events
|
|
|
|
const datesToEvents = (dates) => {
|
|
|
|
let events = [];
|
|
|
|
dates.forEach((date) => {
|
|
|
|
let event = {
|
|
|
|
start: date.toFormat('yyyy-MM-dd'),
|
|
|
|
display: 'background',
|
|
|
|
};
|
|
|
|
events.push(event);
|
|
|
|
});
|
|
|
|
return events;
|
|
|
|
};
|