Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render Availabilities as TimeSlots in Scheduler View from FreeBusy API #71

Open
wants to merge 3 commits into
base: feature-be-google-calendar-freebusy-api
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions client/src/pages/scheduling/Scheduler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useState, useEffect } from "react";
import { Link } from "react-router-dom";
import axios from "axios";
import Container from "../../components/Layout/Container";
import { Box, Grid, Button, Divider, Typography, Paper, useMediaQuery } from "@material-ui/core";
Expand All @@ -21,19 +20,22 @@ const useStyles = makeStyles((theme) => ({
marginTop: theme.spacing(4)
},
timePicker: {
height: "300px",
overflowY: "auto",
'& > *': {
marginTop: theme.spacing(3),
}
},
noAvail: {
marginTop: theme.spacing(3)
}
}));

const Scheduler = (props) => {
const theme = useTheme();
const matches = useMediaQuery(theme.breakpoints.down("sm"));

// console.log(props.match.params.event_type);
const eventTypeID = props.match.params.event_type;

const [eventType, setEventType] = useState(null);

// Retrieve Event Type Information from params
Expand All @@ -42,17 +44,31 @@ const Scheduler = (props) => {
.then(response => setEventType(response.data));
}, []);

const todaysDate = new Date();
const tomorrowsDate = new Date(todaysDate);
tomorrowsDate.setDate(tomorrowsDate.getDate() + 1);

const classes = useStyles();
const [selectedDay, setSelectedDay] = useState(null);
const [selectedDay, setSelectedDay] = useState({
day: tomorrowsDate.getDate(),
month: tomorrowsDate.getMonth() + 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how come this needs to be + 1?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's due to indices, maybes make a note of this in a comment

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, regular JavaScript indexes the months starting with 0 whereas this library indexes it with 0, so December in the .getMonth() method returns 11, but to display it as December for this library, it should be 12 which is where the ...+ 1 adjusts it to.

year: tomorrowsDate.getFullYear()
});
const [avail, setAvail] = useState([]);

const handleDaySelection = async (props) => {
setSelectedDay(props);
const { day, month, year } = props;
await axios.get(`http://localhost:3001/api/avail/freebusy?user=${eventType.user.id}&day=${day}&month=${month - 1}&year=${year}&duration=${eventType.duration}`)
.then(response => setAvail(response.data));
};

return (
<Container>
<Grid container justify="flex-end">
<Grid container justify="flex-end" item xs={4}>
<Button
className={classes.nav} variant="outlined"
// component={Link}
href="http://localhost:3000/event_types/user/me">Home</Button>
</Grid>
</Grid>
Expand Down Expand Up @@ -80,20 +96,27 @@ const Scheduler = (props) => {
<Grid container justify="center" item md={6} xs={12}>
<Calendar
value={selectedDay}
onChange={setSelectedDay}
onChange={handleDaySelection}
shouldHighlightWeekends
/>
</Grid>
<Grid item md={6} xs={12}>
{selectedDay &&
<>
<Typography className={classes.chosenDay} align="center">{new Date(selectedDay.year, selectedDay.month - 1, selectedDay.day).toLocaleString("default", { month: "long" })} {selectedDay.day}, {selectedDay.year}</Typography>
<div className={classes.timePicker}>
<Button variant="outlined" size="large" color="primary" fullWidth >16:30</Button>
<Button variant="outlined" size="large" color="primary" fullWidth >17:00</Button>
<Button variant="outlined" size="large" color="primary" fullWidth >17:30</Button>
<Button variant="outlined" size="large" color="primary" fullWidth >18:00</Button>
</div>
{ avail.length ?
<div className={classes.timePicker}>
{avail.map((timeSlot, index) => {
return(
<Button key={index} variant="outlined" size="large" color="primary" fullWidth >{ (new Date(timeSlot.start)).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'}) }</Button>
)})}
</div>
:
<div className={classes.noAvail}>
<Typography align={matches ? "left" : "center"} gutterBottom>Sorry, all spots are booked today.</Typography>
<Typography align={matches ? "left" : "center"} gutterBottom>Please try another day.</Typography>
</div>
}
</>
}
</Grid>
Expand Down
19 changes: 9 additions & 10 deletions server/controllers/availability.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@ const { google } = require("googleapis");

// @desc FreeBusy
// @route GET /freebusy?date=143213434&meetingTypeID=df34234fgdg235
availabilitiesRouter.get("/freebusy", (request, response) => {
// request.query.meetingTypeID
// request.query.date // must be in datetime format
availabilitiesRouter.get("/freebusy", async (request, response) => {
const { day, month, year } = request.query;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { day, month, year } = request.query;
const { day, month, year, duration, user } = request.query;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this has to be

Suggested change
const { day, month, year } = request.query;
let{ day, month, year, duration, user } = request.query;

in order to reassignment of

duration = Number(duration)
user = await User.findbyId(user)

in the suggestions below:

const duration = Number(request.query.duration);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const duration = Number(request.query.duration);
const duration = Number(duration);

const user = await User.findById(request.query.user);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const user = await User.findById(request.query.user);
const user = await User.findById(user);


const oauth2Client = new google.auth.OAuth2({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "/api/auth/google/callback"
});

oauth2Client.credentials = {
access_token: request.user.accessToken,
refresh_token: request.user.refreshToken
access_token: user.accessToken,
refresh_token: user.refreshToken
};

const availStartTime = new Date(2020, 11, 12, 9, 0, 0, 0);
const availEndTime = new Date(2020, 11, 12, 17, 0, 0, 0);

// Duration in minutes
const duration = 60;
const availStartTime = new Date(year, month, day, 9, 0, 0, 0);
const availEndTime = new Date(year, month, day, 17, 0, 0, 0);


const calendar = google.calendar({ version: "v3", auth: oauth2Client });
Expand Down