2021-07-08 21:14:29 +00:00
|
|
|
import { useState, useEffect } from "react";
|
2021-06-24 22:15:18 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import getSlots from "../../lib/slots";
|
2021-06-29 01:45:58 +00:00
|
|
|
import dayjs, { Dayjs } from "dayjs";
|
2021-06-27 22:02:27 +00:00
|
|
|
import isBetween from "dayjs/plugin/isBetween";
|
2021-06-27 22:30:11 +00:00
|
|
|
import utc from "dayjs/plugin/utc";
|
2021-06-27 22:02:27 +00:00
|
|
|
dayjs.extend(isBetween);
|
2021-06-27 22:30:11 +00:00
|
|
|
dayjs.extend(utc);
|
2021-06-27 22:02:27 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
eventLength: number;
|
|
|
|
minimumBookingNotice?: number;
|
|
|
|
date: Dayjs;
|
2021-07-08 21:14:29 +00:00
|
|
|
workingHours: [];
|
|
|
|
organizerTimeZone: string;
|
2021-06-29 01:45:58 +00:00
|
|
|
};
|
2021-06-27 22:02:27 +00:00
|
|
|
|
2021-07-08 21:14:29 +00:00
|
|
|
const Slots = ({ eventLength, minimumBookingNotice, date, workingHours, organizerTimeZone }: Props) => {
|
2021-06-27 22:02:27 +00:00
|
|
|
minimumBookingNotice = minimumBookingNotice || 0;
|
2021-06-24 22:15:18 +00:00
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
const { user } = router.query;
|
|
|
|
const [slots, setSlots] = useState([]);
|
2021-06-29 01:45:58 +00:00
|
|
|
const [isFullyBooked, setIsFullyBooked] = useState(false);
|
|
|
|
const [hasErrors, setHasErrors] = useState(false);
|
2021-06-24 22:15:18 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setSlots([]);
|
2021-06-27 22:02:27 +00:00
|
|
|
setIsFullyBooked(false);
|
2021-06-27 22:30:11 +00:00
|
|
|
setHasErrors(false);
|
2021-06-24 22:15:18 +00:00
|
|
|
fetch(
|
2021-06-29 01:45:58 +00:00
|
|
|
`/api/availability/${user}?dateFrom=${date.startOf("day").utc().startOf("day").format()}&dateTo=${date
|
2021-06-24 22:15:18 +00:00
|
|
|
.endOf("day")
|
|
|
|
.utc()
|
2021-06-29 01:45:58 +00:00
|
|
|
.endOf("day")
|
2021-06-24 22:15:18 +00:00
|
|
|
.format()}`
|
|
|
|
)
|
|
|
|
.then((res) => res.json())
|
2021-06-27 22:30:11 +00:00
|
|
|
.then(handleAvailableSlots)
|
2021-06-29 01:45:58 +00:00
|
|
|
.catch((e) => {
|
2021-06-27 22:30:11 +00:00
|
|
|
console.error(e);
|
|
|
|
setHasErrors(true);
|
2021-06-29 01:45:58 +00:00
|
|
|
});
|
2021-06-27 22:02:27 +00:00
|
|
|
}, [date]);
|
2021-06-24 22:15:18 +00:00
|
|
|
|
|
|
|
const handleAvailableSlots = (busyTimes: []) => {
|
|
|
|
const times = getSlots({
|
2021-06-27 22:02:27 +00:00
|
|
|
frequency: eventLength,
|
|
|
|
inviteeDate: date,
|
|
|
|
workingHours,
|
|
|
|
minimumBookingNotice,
|
2021-07-08 21:14:29 +00:00
|
|
|
organizerTimeZone,
|
2021-06-24 22:15:18 +00:00
|
|
|
});
|
|
|
|
|
2021-06-27 22:02:27 +00:00
|
|
|
const timesLengthBeforeConflicts: number = times.length;
|
|
|
|
|
2021-06-24 22:15:18 +00:00
|
|
|
// Check for conflicts
|
|
|
|
for (let i = times.length - 1; i >= 0; i -= 1) {
|
2021-06-30 15:27:49 +00:00
|
|
|
busyTimes.every((busyTime): boolean => {
|
2021-06-27 22:30:11 +00:00
|
|
|
const startTime = dayjs(busyTime.start).utc();
|
|
|
|
const endTime = dayjs(busyTime.end).utc();
|
2021-06-24 22:15:18 +00:00
|
|
|
// Check if start times are the same
|
2021-06-30 15:27:49 +00:00
|
|
|
if (times[i].utc().isSame(startTime)) {
|
2021-06-24 22:15:18 +00:00
|
|
|
times.splice(i, 1);
|
|
|
|
}
|
|
|
|
// Check if time is between start and end times
|
2021-06-27 22:30:11 +00:00
|
|
|
else if (times[i].utc().isBetween(startTime, endTime)) {
|
2021-06-24 22:15:18 +00:00
|
|
|
times.splice(i, 1);
|
|
|
|
}
|
|
|
|
// Check if slot end time is between start and end time
|
2021-06-27 22:30:11 +00:00
|
|
|
else if (times[i].utc().add(eventLength, "minutes").isBetween(startTime, endTime)) {
|
2021-06-24 22:15:18 +00:00
|
|
|
times.splice(i, 1);
|
|
|
|
}
|
|
|
|
// Check if startTime is between slot
|
2021-06-27 22:30:11 +00:00
|
|
|
else if (startTime.isBetween(times[i].utc(), times[i].utc().add(eventLength, "minutes"))) {
|
2021-06-24 22:15:18 +00:00
|
|
|
times.splice(i, 1);
|
2021-06-30 15:27:49 +00:00
|
|
|
} else {
|
|
|
|
return true;
|
2021-06-24 22:15:18 +00:00
|
|
|
}
|
2021-06-30 15:27:49 +00:00
|
|
|
return false;
|
2021-06-24 22:15:18 +00:00
|
|
|
});
|
|
|
|
}
|
2021-06-27 22:02:27 +00:00
|
|
|
|
|
|
|
if (times.length === 0 && timesLengthBeforeConflicts !== 0) {
|
|
|
|
setIsFullyBooked(true);
|
|
|
|
}
|
2021-06-24 22:15:18 +00:00
|
|
|
// Display available times
|
|
|
|
setSlots(times);
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
slots,
|
2021-06-27 22:02:27 +00:00
|
|
|
isFullyBooked,
|
2021-06-27 22:30:11 +00:00
|
|
|
hasErrors,
|
2021-06-24 22:15:18 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Slots;
|