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'
component={Availability}
possibleDates={possibleDates}
currentUser={currentUser}
/>
<PrivateRoute path='/invite' component={Invite} />
</Switch>

View File

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

View File

@ -136,14 +136,58 @@ export default function Schedule({
// MEETING
const handleSchedule = () => {
// ADD THE MEETING
backend
.post('/meetings', currentMeeting)
.then((response) => {
setCurrentMeeting({
...currentMeeting,
id: response.data.id,
});
postPossibleDates({ meeting_id: response.data.id });
history.push('/availability');
})
.catch((error) => {
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) => {
@ -254,10 +298,8 @@ export default function Schedule({
>
Confirm dates
</Button>
{error && (
<Message type='error' description={error} />
)}
</ButtonGroup>
{error && <Message type='error' description={error} />}
<div className={'selected-dates'}></div>
{datesList.length > 0 && (
<>