2021-06-24 22:15:18 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2021-06-14 18:53:20 +00:00
|
|
|
import TimezoneSelect from "react-timezone-select";
|
2021-06-24 22:15:18 +00:00
|
|
|
import { TrashIcon } from "@heroicons/react/outline";
|
|
|
|
import { WeekdaySelect } from "./WeekdaySelect";
|
2021-06-16 22:27:27 +00:00
|
|
|
import SetTimesModal from "./modal/SetTimesModal";
|
2021-06-24 22:15:18 +00:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
import utc from "dayjs/plugin/utc";
|
|
|
|
import timezone from "dayjs/plugin/timezone";
|
2021-06-29 01:45:58 +00:00
|
|
|
import { Availability } from "@prisma/client";
|
2021-07-07 10:43:13 +00:00
|
|
|
|
2021-06-14 18:53:20 +00:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
|
|
|
|
2021-06-29 01:45:58 +00:00
|
|
|
type Props = {
|
|
|
|
timeZone: string;
|
|
|
|
availability: Availability[];
|
|
|
|
setTimeZone: unknown;
|
|
|
|
};
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-06-29 01:45:58 +00:00
|
|
|
export const Scheduler = ({
|
|
|
|
availability,
|
|
|
|
setAvailability,
|
|
|
|
timeZone: selectedTimeZone,
|
|
|
|
setTimeZone,
|
|
|
|
}: Props) => {
|
2021-06-24 22:15:18 +00:00
|
|
|
const [editSchedule, setEditSchedule] = useState(-1);
|
2021-06-29 01:45:58 +00:00
|
|
|
const [dateOverrides, setDateOverrides] = useState([]);
|
|
|
|
const [openingHours, setOpeningHours] = useState([]);
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-06-24 22:15:18 +00:00
|
|
|
useEffect(() => {
|
2021-06-29 01:45:58 +00:00
|
|
|
setOpeningHours(
|
|
|
|
availability
|
|
|
|
.filter((item: Availability) => item.days.length !== 0)
|
|
|
|
.map((item) => {
|
|
|
|
item.startDate = dayjs().utc().startOf("day").add(item.startTime, "minutes");
|
|
|
|
item.endDate = dayjs().utc().startOf("day").add(item.endTime, "minutes");
|
|
|
|
return item;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
setDateOverrides(availability.filter((item: Availability) => item.date));
|
|
|
|
}, []);
|
2021-06-16 22:27:27 +00:00
|
|
|
|
2021-06-29 01:45:58 +00:00
|
|
|
// updates availability to how it should be formatted outside this component.
|
|
|
|
useEffect(() => {
|
|
|
|
setAvailability({
|
|
|
|
dateOverrides: dateOverrides,
|
|
|
|
openingHours: openingHours,
|
|
|
|
});
|
|
|
|
}, [dateOverrides, openingHours]);
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-06-29 01:45:58 +00:00
|
|
|
const addNewSchedule = () => setEditSchedule(openingHours.length);
|
2021-06-24 22:15:18 +00:00
|
|
|
|
2021-06-29 01:45:58 +00:00
|
|
|
const applyEditSchedule = (changed) => {
|
2021-06-29 22:00:25 +00:00
|
|
|
// new entry
|
2021-06-29 01:45:58 +00:00
|
|
|
if (!changed.days) {
|
|
|
|
changed.days = [1, 2, 3, 4, 5]; // Mon - Fri
|
2021-06-29 22:00:25 +00:00
|
|
|
setOpeningHours(openingHours.concat(changed));
|
|
|
|
} else {
|
|
|
|
// update
|
|
|
|
const replaceWith = { ...openingHours[editSchedule], ...changed };
|
|
|
|
openingHours.splice(editSchedule, 1, replaceWith);
|
|
|
|
setOpeningHours([].concat(openingHours));
|
2021-06-29 01:45:58 +00:00
|
|
|
}
|
2021-06-24 22:15:18 +00:00
|
|
|
};
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-06-16 22:27:27 +00:00
|
|
|
const removeScheduleAt = (toRemove: number) => {
|
2021-06-29 01:45:58 +00:00
|
|
|
openingHours.splice(toRemove, 1);
|
|
|
|
setOpeningHours([].concat(openingHours));
|
2021-06-14 18:53:20 +00:00
|
|
|
};
|
|
|
|
|
2021-06-29 01:45:58 +00:00
|
|
|
const OpeningHours = ({ idx, item }) => (
|
2021-08-02 15:24:01 +00:00
|
|
|
<li className="py-2 flex justify-between border-b">
|
|
|
|
<div className="flex flex-col space-y-4 lg:inline-flex">
|
2021-06-29 01:45:58 +00:00
|
|
|
<WeekdaySelect defaultValue={item.days} onSelect={(selected: number[]) => (item.days = selected)} />
|
2021-08-02 15:24:01 +00:00
|
|
|
<button
|
|
|
|
className="text-sm bg-neutral-100 rounded-sm py-2 px-3"
|
|
|
|
type="button"
|
|
|
|
onClick={() => setEditSchedule(idx)}>
|
2021-06-29 22:00:25 +00:00
|
|
|
{dayjs()
|
|
|
|
.startOf("day")
|
|
|
|
.add(item.startTime, "minutes")
|
|
|
|
.format(item.startTime % 60 === 0 ? "ha" : "h:mma")}
|
2021-06-29 01:45:58 +00:00
|
|
|
until
|
2021-06-29 22:00:25 +00:00
|
|
|
{dayjs()
|
|
|
|
.startOf("day")
|
|
|
|
.add(item.endTime, "minutes")
|
|
|
|
.format(item.endTime % 60 === 0 ? "ha" : "h:mma")}
|
2021-06-29 01:45:58 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => removeScheduleAt(idx)}
|
|
|
|
className="btn-sm bg-transparent px-2 py-1 ml-1">
|
|
|
|
<TrashIcon className="h-6 w-6 inline text-gray-400 -mt-1" />
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
|
2021-06-14 18:53:20 +00:00
|
|
|
return (
|
|
|
|
<div>
|
2021-07-30 23:05:38 +00:00
|
|
|
<div className="flex">
|
2021-07-15 14:10:26 +00:00
|
|
|
<div className="w-full">
|
2021-07-30 23:05:38 +00:00
|
|
|
<div className="">
|
2021-06-14 18:53:20 +00:00
|
|
|
<label htmlFor="timeZone" className="block text-sm font-medium text-gray-700">
|
|
|
|
Timezone
|
|
|
|
</label>
|
|
|
|
<div className="mt-1">
|
2021-06-24 22:15:18 +00:00
|
|
|
<TimezoneSelect
|
|
|
|
id="timeZone"
|
2021-06-29 01:45:58 +00:00
|
|
|
value={selectedTimeZone}
|
|
|
|
onChange={(tz) => setTimeZone(tz.value)}
|
2021-08-02 15:24:01 +00:00
|
|
|
className="shadow-sm focus:ring-black focus:border-black mt-1 block w-full sm:text-sm border-gray-300 rounded-md"
|
2021-06-24 22:15:18 +00:00
|
|
|
/>
|
2021-06-14 18:53:20 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<ul>
|
2021-06-29 01:45:58 +00:00
|
|
|
{openingHours.map((item, idx) => (
|
|
|
|
<OpeningHours key={idx} idx={idx} item={item} />
|
2021-06-24 22:15:18 +00:00
|
|
|
))}
|
2021-06-14 18:53:20 +00:00
|
|
|
</ul>
|
2021-08-02 15:24:01 +00:00
|
|
|
<button type="button" onClick={addNewSchedule} className="btn-white btn-sm mt-2">
|
2021-06-24 22:15:18 +00:00
|
|
|
Add another
|
|
|
|
</button>
|
2021-06-14 18:53:20 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-06-24 22:15:18 +00:00
|
|
|
{editSchedule >= 0 && (
|
|
|
|
<SetTimesModal
|
2021-06-29 22:00:25 +00:00
|
|
|
startTime={openingHours[editSchedule] ? openingHours[editSchedule].startTime : 540}
|
|
|
|
endTime={openingHours[editSchedule] ? openingHours[editSchedule].endTime : 1020}
|
|
|
|
onChange={(times) => applyEditSchedule({ ...(openingHours[editSchedule] || {}), ...times })}
|
2021-06-24 22:15:18 +00:00
|
|
|
onExit={() => setEditSchedule(-1)}
|
|
|
|
/>
|
|
|
|
)}
|
2021-06-14 18:53:20 +00:00
|
|
|
</div>
|
|
|
|
);
|
2021-06-24 22:15:18 +00:00
|
|
|
};
|