Skip to content

Commit

Permalink
Time slots are now shown dynamically, depending on the date
Browse files Browse the repository at this point in the history
Relates #48
  • Loading branch information
nataphilips committed Dec 1, 2019
1 parent 62e438f commit a79afbb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
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}'`
);
};

Expand Down

0 comments on commit a79afbb

Please sign in to comment.