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

Time slots are now shown dynamically, depending on the date #60

Merged
merged 2 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion barberia/src/App/pages/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Calendar({
inline
/>

<AppoPicker time={time} setTime={setTime} />
<AppoPicker time={time} setTime={setTime} baseDate={baseDate} />
<p>
time picked is {time.toString()}
<br />
Expand Down
11 changes: 8 additions & 3 deletions barberia/src/App/pages/TimePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import AppointmentPicker from "appointment-picker";
import "appointment-picker/dist/appointment-picker.css";

const AppoPicker = props => {
const AppoPicker = ({ baseDate, ...props }) => {
const [options, setOptions] = React.useState({
leadingZero: true,
interval: 30,
Expand All @@ -17,15 +17,20 @@ const AppoPicker = props => {
const [time, setTime] = React.useState({});

React.useEffect(() => {
fetch("/api/getBusyTimeSlots")
fetch("/api/getBusyTimeSlots/" + baseDate)
.then(res => res.json())
.then(obj => obj.map(x => x.booking_time))
.then(arr => {
const newOptions = JSON.parse(JSON.stringify(options));
newOptions.disabled = arr;
setOptions(newOptions);
})
.catch(err => {
const newOptions = JSON.parse(JSON.stringify(options));
newOptions.disabled = [];
setOptions(newOptions);
});
}, []);
}, [baseDate]);

const pickerRef = React.createRef();

Expand Down
5 changes: 3 additions & 2 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ router.post("/savenewbooking", (req, res) => {
queries.saveNewBooking(data).catch(err => console.log(err));
});

router.get("/getBusyTimeSlots", (req, res) => {
router.get("/getBusyTimeSlots/:date", (req, res) => {
const date = req.params.date;
queries
.getBusyTimeSlots()
.getBusyTimeSlots(date)
.then(times => times.rows)
.then(rows => res.json(rows))
.catch(err => console.log(err));
Expand Down
4 changes: 2 additions & 2 deletions src/queries/getUnavailableTimes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const databaseConnection = require("../database/db_connection.js");

const getBusyTimeSlots = () => {
const getBusyTimeSlots = baseDate => {
return databaseConnection.query(
`SELECT booking_time FROM bookings WHERE booking_date='20191203'`
`SELECT booking_time FROM bookings WHERE booking_date='${baseDate}'`
Copy link
Collaborator

Choose a reason for hiding this comment

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

risks of sql injection here, use the $1 syntax for psql so like
(SELECT booking_time FROM bookings WHERE booking_date=$1,[someValue])

Copy link
Collaborator

Choose a reason for hiding this comment

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

fixed

);
};

Expand Down