frontend/src/screens/Availability.js

172 lines
3.6 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
import { Redirect } from 'react-router-dom';
2020-06-03 09:14:04 +00:00
import {
Panel,
Form,
ControlLabel,
Button,
ButtonGroup,
Message,
FormGroup,
} from 'rsuite';
import {
NavBar,
TimezonePicker,
TimePicker,
IntervalSelector,
} from '../components';
2020-08-21 23:08:18 +00:00
import './styles/Availability.less';
import './styles/layout.less';
2020-06-03 09:14:04 +00:00
// DEV possibleDates
const possibleDates = [
{
id: 63,
start: '2020-08-01',
display: 'background',
},
{
id: 64,
start: '2020-08-02',
display: 'background',
},
];
2020-06-09 14:07:07 +00:00
export default function Availability({ currentUser }) {
const [availability, setAvailability] = useState([]);
const [timezone, setTimezone] = useState(currentUser.timezone);
const [times, setTimes] = useState({
earliest: currentUser.earliest_time,
latest: currentUser.latest_time,
});
2020-06-03 09:14:04 +00:00
const handleClear = () => {
setAvailability([]);
};
2020-06-09 14:07:07 +00:00
const handleSelect = ({ start, end }) => {
setAvailability([
...availability,
{
start: start,
end: end,
},
]);
2020-06-03 09:14:04 +00:00
};
const handleSubmit = () => {
//TODO post intervals to backend
// Post it as UNIX EPOCH TIME
// timezone = UTC always
// Use Map to add id to events
// // Find corresponding id
// const selectedDate = start.toISOString().substring(0, 10);
// const [date] = possibleDates.filter(
// (date) => date.start === selectedDate,
// );
console.log(availability);
};
const handleSelectTimezone = (value, item, event) => {
setTimezone(item.timezone);
};
const handleClearTimezone = () => {
setTimezone(currentUser.timezone);
};
const handleEarliest = (value, item, event) => {
setTimes({
...times,
earliest: value,
});
};
const handleLatest = (value, item, event) => {
setTimes({
...times,
latest: value,
});
};
if (!possibleDates) {
return <Redirect to='/dashboard' />;
}
2020-06-03 09:14:04 +00:00
return (
<>
2020-08-19 18:04:52 +00:00
<NavBar title='Add your availability' />
2020-08-21 23:08:18 +00:00
<Panel className={'app-container'}>
2020-06-03 09:14:04 +00:00
<Form className={'av-container'}>
<div className={'av-details'}>
<FormGroup>
<ControlLabel>Earliest time</ControlLabel>
<TimePicker
time={times.earliest}
handleSelect={handleEarliest}
/>
2020-06-03 09:14:04 +00:00
</FormGroup>
<FormGroup>
<ControlLabel>Latest time</ControlLabel>
<TimePicker
time={times.latest}
handleSelect={handleLatest}
/>
2020-06-03 09:14:04 +00:00
</FormGroup>
<FormGroup className='av-timezone'>
<ControlLabel>Timezone</ControlLabel>
<TimezonePicker
timezone={timezone}
handleSelect={handleSelectTimezone}
handleClean={handleClearTimezone}
/>
2020-06-03 09:14:04 +00:00
</FormGroup>
<div className='av-controls'>
<ButtonGroup justified>
<Button
appearance='ghost'
block
size='lg'
disabled={availability.length === 0}
onClick={() => handleClear()}
>
Clear selection
</Button>
<Button
appearance='primary'
size='lg'
block
disabled={availability.length === 0}
onClick={handleSubmit}
2020-06-03 09:14:04 +00:00
>
Save your availability
</Button>
</ButtonGroup>
</div>
</div>
<div className={'interval-selector'}>
<Message
showIcon
type='info'
description='Select your availability on the calendar.'
2020-06-03 09:14:04 +00:00
/>
<IntervalSelector
selectedDates={possibleDates}
2020-06-09 14:07:07 +00:00
availability={availability}
setAvailability={setAvailability}
2020-06-03 09:14:04 +00:00
handleSelect={handleSelect}
handleClear={handleClear}
slotMinTime={times.earliest}
slotMaxTime={times.latest}
2020-06-03 09:14:04 +00:00
/>
</div>
</Form>
</Panel>
</>
);
}