2021-09-22 19:52:38 +00:00
|
|
|
import { TrashIcon } from "@heroicons/react/outline";
|
|
|
|
import { Availability } from "@prisma/client";
|
|
|
|
import dayjs from "dayjs";
|
|
|
|
import timezone from "dayjs/plugin/timezone";
|
|
|
|
import utc from "dayjs/plugin/utc";
|
2021-06-24 22:15:18 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2021-11-10 11:16:32 +00:00
|
|
|
import TimezoneSelect, { ITimezoneOption } from "react-timezone-select";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-10-15 10:53:42 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
|
|
|
|
2021-11-29 06:37:31 +00:00
|
|
|
import Button from "@components/ui/Button";
|
|
|
|
|
2021-06-24 22:15:18 +00:00
|
|
|
import { WeekdaySelect } from "./WeekdaySelect";
|
2021-06-16 22:27:27 +00:00
|
|
|
import SetTimesModal from "./modal/SetTimesModal";
|
2021-07-07 10:43:13 +00:00
|
|
|
|
2021-06-14 18:53:20 +00:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
|
|
|
|
2022-01-21 21:35:31 +00:00
|
|
|
type AvailabilityInput = Pick<Availability, "days" | "startTime" | "endTime">;
|
|
|
|
|
2021-06-29 01:45:58 +00:00
|
|
|
type Props = {
|
|
|
|
timeZone: string;
|
|
|
|
availability: Availability[];
|
2021-11-10 11:16:32 +00:00
|
|
|
setTimeZone: (timeZone: string) => void;
|
2022-01-21 21:35:31 +00:00
|
|
|
setAvailability: (schedule: {
|
|
|
|
openingHours: AvailabilityInput[];
|
|
|
|
dateOverrides: AvailabilityInput[];
|
|
|
|
}) => void;
|
2021-06-29 01:45:58 +00:00
|
|
|
};
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
export const Scheduler = ({ availability, setAvailability, timeZone, setTimeZone }: Props) => {
|
|
|
|
const { t, i18n } = useLocale();
|
2021-06-24 22:15:18 +00:00
|
|
|
const [editSchedule, setEditSchedule] = useState(-1);
|
2021-11-10 11:16:32 +00:00
|
|
|
const [openingHours, setOpeningHours] = useState<Availability[]>([]);
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-06-24 22:15:18 +00:00
|
|
|
useEffect(() => {
|
2021-11-10 11:16:32 +00:00
|
|
|
setOpeningHours(availability.filter((item: Availability) => item.days.length !== 0));
|
2021-06-29 01:45:58 +00:00
|
|
|
}, []);
|
2021-06-16 22:27:27 +00:00
|
|
|
|
2021-06-29 01:45:58 +00:00
|
|
|
useEffect(() => {
|
2021-11-10 11:16:32 +00:00
|
|
|
setAvailability({ openingHours, dateOverrides: [] });
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [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-11-10 11:16:32 +00:00
|
|
|
const applyEditSchedule = (changed: Availability) => {
|
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);
|
2021-11-10 11:16:32 +00:00
|
|
|
setOpeningHours([...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);
|
2021-11-10 11:16:32 +00:00
|
|
|
setOpeningHours([...openingHours]);
|
2021-06-14 18:53:20 +00:00
|
|
|
};
|
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
const OpeningHours = ({ idx, item }: { idx: number; item: Availability }) => (
|
2022-02-09 00:05:13 +00:00
|
|
|
<li className="flex justify-between border-b py-2">
|
2021-08-02 15:24:01 +00:00
|
|
|
<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
|
2022-02-09 00:05:13 +00:00
|
|
|
className="rounded-sm bg-neutral-100 px-3 py-2 text-sm"
|
2021-08-02 15:24:01 +00:00
|
|
|
type="button"
|
|
|
|
onClick={() => setEditSchedule(idx)}>
|
2022-02-08 16:01:18 +00:00
|
|
|
{item.startTime.toLocaleTimeString(i18n.language, {
|
|
|
|
hour: "numeric",
|
|
|
|
minute: "2-digit",
|
|
|
|
timeZone: "UTC",
|
|
|
|
})}
|
2021-10-15 10:53:42 +00:00
|
|
|
{t("until")}
|
2022-02-08 16:01:18 +00:00
|
|
|
{item.endTime.toLocaleTimeString(i18n.language, {
|
|
|
|
hour: "numeric",
|
|
|
|
minute: "2-digit",
|
|
|
|
timeZone: "UTC",
|
|
|
|
})}
|
2021-06-29 01:45:58 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => removeScheduleAt(idx)}
|
2022-02-09 00:05:13 +00:00
|
|
|
className="btn-sm ml-1 bg-transparent px-2 py-1">
|
|
|
|
<TrashIcon className="-mt-1 inline h-5 w-5 text-gray-400" />
|
2021-06-29 01:45:58 +00:00
|
|
|
</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-09-24 15:03:28 +00:00
|
|
|
<div>
|
2021-06-14 18:53:20 +00:00
|
|
|
<label htmlFor="timeZone" className="block text-sm font-medium text-gray-700">
|
2021-10-15 10:53:42 +00:00
|
|
|
{t("timezone")}
|
2021-06-14 18:53:20 +00:00
|
|
|
</label>
|
|
|
|
<div className="mt-1">
|
2021-06-24 22:15:18 +00:00
|
|
|
<TimezoneSelect
|
|
|
|
id="timeZone"
|
2021-11-10 11:16:32 +00:00
|
|
|
value={timeZone}
|
|
|
|
onChange={(tz: ITimezoneOption) => setTimeZone(tz.value)}
|
2022-02-09 22:32:31 +00:00
|
|
|
className="focus:border-brand mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:ring-black sm:text-sm"
|
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-11-29 06:37:31 +00:00
|
|
|
<Button type="button" onClick={addNewSchedule} className="mt-2" color="secondary" size="sm">
|
2021-10-15 10:53:42 +00:00
|
|
|
{t("add_another")}
|
2021-11-29 06:37:31 +00:00
|
|
|
</Button>
|
2021-06-14 18:53:20 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-06-24 22:15:18 +00:00
|
|
|
{editSchedule >= 0 && (
|
|
|
|
<SetTimesModal
|
2021-11-10 11:16:32 +00:00
|
|
|
startTime={
|
|
|
|
openingHours[editSchedule]
|
2022-02-08 16:01:18 +00:00
|
|
|
? new Date(openingHours[editSchedule].startTime).getUTCHours() * 60 +
|
|
|
|
new Date(openingHours[editSchedule].startTime).getUTCMinutes()
|
2021-11-10 11:16:32 +00:00
|
|
|
: 540
|
|
|
|
}
|
|
|
|
endTime={
|
|
|
|
openingHours[editSchedule]
|
2022-02-08 16:01:18 +00:00
|
|
|
? new Date(openingHours[editSchedule].endTime).getUTCHours() * 60 +
|
|
|
|
new Date(openingHours[editSchedule].endTime).getUTCMinutes()
|
2021-11-10 11:16:32 +00:00
|
|
|
: 1020
|
|
|
|
}
|
|
|
|
onChange={(times: { startTime: number; endTime: number }) =>
|
|
|
|
applyEditSchedule({
|
|
|
|
...(openingHours[editSchedule] || {}),
|
|
|
|
startTime: new Date(
|
2022-02-08 16:01:18 +00:00
|
|
|
new Date().setUTCHours(Math.floor(times.startTime / 60), times.startTime % 60, 0, 0)
|
2021-11-10 11:16:32 +00:00
|
|
|
),
|
|
|
|
endTime: new Date(
|
2022-02-08 16:01:18 +00:00
|
|
|
new Date().setUTCHours(Math.floor(times.endTime / 60), times.endTime % 60, 0, 0)
|
2021-11-10 11:16:32 +00:00
|
|
|
),
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
};
|