Post PossibleDates, add them with id to state

This commit is contained in:
rui hildt 2020-08-23 16:30:25 +02:00
parent c7afdff759
commit bbc9a3c12c
3 changed files with 60 additions and 6 deletions

View File

@ -81,6 +81,7 @@ export default function App() {
path='/availability' path='/availability'
component={Availability} component={Availability}
possibleDates={possibleDates} possibleDates={possibleDates}
currentUser={currentUser}
/> />
<PrivateRoute path='/invite' component={Invite} /> <PrivateRoute path='/invite' component={Invite} />
</Switch> </Switch>

View File

@ -16,9 +16,13 @@ import { NavBar, TimezonePicker, IntervalSelector } from '../components';
import './styles/Availability.less'; import './styles/Availability.less';
import './styles/layout.less'; import './styles/layout.less';
export default function Availability({ possibleDates }) { export default function Availability({ possibleDates, currentUser }) {
const [availability, setAvailability] = useState([]); const [availability, setAvailability] = useState([]);
console.log(currentUser)
console.log(possibleDates)
//TODO post intervals to backend
const handleClear = () => { const handleClear = () => {
setAvailability([]); setAvailability([]);
}; };
@ -30,11 +34,17 @@ export default function Availability({ possibleDates }) {
end: end, end: end,
}; };
updatedEvents.push(newAvailability); updatedEvents.push(newAvailability);
// console.log("NEW AVAILABILITY UNIX EPOCH: ", Math.floor(newAvailability.start / 1000))
setAvailability([...availability, ...updatedEvents]); setAvailability([...availability, ...updatedEvents]);
}; };
if(!possibleDates) { const handleSubmit = () => {
return <Redirect to='/schedule' /> console.log(availability);
};
if (!possibleDates) {
return <Redirect to='/schedule' />;
} }
return ( return (
@ -71,6 +81,7 @@ export default function Availability({ possibleDates }) {
size='lg' size='lg'
block block
disabled={availability.length === 0} disabled={availability.length === 0}
onClick={handleSubmit}
> >
Save your availability Save your availability
</Button> </Button>

View File

@ -136,14 +136,58 @@ export default function Schedule({
// MEETING // MEETING
const handleSchedule = () => { const handleSchedule = () => {
// ADD THE MEETING
backend backend
.post('/meetings', currentMeeting) .post('/meetings', currentMeeting)
.then((response) => { .then((response) => {
setCurrentMeeting({
...currentMeeting,
id: response.data.id,
});
postPossibleDates({ meeting_id: response.data.id });
history.push('/availability'); history.push('/availability');
}) })
.catch((error) => { .catch((error) => {
setError('Failed to add new account.'); setError('Failed to add new account.');
}); });
const postPossibleDates = ({ meeting_id }) => {
// Post the possible dates and set add their ID to state
const postPossibleDate = (data) => {
return backend.post('/possible-dates', data);
};
const createRequest = ({ start }) => {
let data = {
meeting_id,
possible_date: start,
};
return postPossibleDate(data);
};
const requests = possibleDates.map((possibleDate) =>
createRequest(possibleDate),
);
// Perform concurrent requests and update possible dates with id
Promise.all(requests).then(function (results) {
const addID = ({ data }) => {
let possibleDate = {
id: data.id,
start: data.possible_date.substring(0, 10),
display: 'background',
};
return possibleDate;
};
const possibleDatesWithID = results.map((result) =>
addID(result),
);
setPossibleDates(possibleDatesWithID);
});
};
}; };
const handleSelectTimezone = (value, item, event) => { const handleSelectTimezone = (value, item, event) => {
@ -254,10 +298,8 @@ export default function Schedule({
> >
Confirm dates Confirm dates
</Button> </Button>
{error && (
<Message type='error' description={error} />
)}
</ButtonGroup> </ButtonGroup>
{error && <Message type='error' description={error} />}
<div className={'selected-dates'}></div> <div className={'selected-dates'}></div>
{datesList.length > 0 && ( {datesList.length > 0 && (
<> <>