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 Schedule from "../../lib/schedule.model";
|
|
|
|
import dayjs from "dayjs";
|
|
|
|
import utc from "dayjs/plugin/utc";
|
|
|
|
import timezone from "dayjs/plugin/timezone";
|
2021-06-14 18:53:20 +00:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
|
|
|
|
|
|
|
export const Scheduler = (props) => {
|
2021-06-24 22:15:18 +00:00
|
|
|
const [schedules, setSchedules]: Schedule[] = useState(
|
|
|
|
props.schedules.map((schedule) => {
|
|
|
|
const startDate = schedule.isOverride
|
|
|
|
? dayjs(schedule.startDate)
|
|
|
|
: dayjs.utc().startOf("day").add(schedule.startTime, "minutes").tz(props.timeZone);
|
|
|
|
return {
|
2021-06-16 22:27:27 +00:00
|
|
|
days: schedule.days,
|
|
|
|
startDate,
|
2021-06-24 22:15:18 +00:00
|
|
|
endDate: startDate.add(schedule.length, "minutes"),
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-06-24 22:15:18 +00:00
|
|
|
const [timeZone, setTimeZone] = useState(props.timeZone);
|
|
|
|
const [editSchedule, setEditSchedule] = useState(-1);
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-06-24 22:15:18 +00:00
|
|
|
useEffect(() => {
|
2021-06-16 22:27:27 +00:00
|
|
|
props.onChange(schedules);
|
2021-06-24 22:15:18 +00:00
|
|
|
}, [schedules]);
|
2021-06-16 22:27:27 +00:00
|
|
|
|
|
|
|
const addNewSchedule = () => setEditSchedule(schedules.length);
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-06-16 22:27:27 +00:00
|
|
|
const applyEditSchedule = (changed: Schedule) => {
|
|
|
|
const replaceWith = {
|
|
|
|
...schedules[editSchedule],
|
2021-06-24 22:15:18 +00:00
|
|
|
...changed,
|
2021-06-16 22:27:27 +00:00
|
|
|
};
|
2021-06-24 22:15:18 +00:00
|
|
|
|
2021-06-16 22:27:27 +00:00
|
|
|
schedules.splice(editSchedule, 1, replaceWith);
|
2021-06-24 22:15:18 +00:00
|
|
|
|
2021-06-16 22:27:27 +00:00
|
|
|
setSchedules([].concat(schedules));
|
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) => {
|
|
|
|
schedules.splice(toRemove, 1);
|
2021-06-14 18:53:20 +00:00
|
|
|
setSchedules([].concat(schedules));
|
|
|
|
};
|
|
|
|
|
2021-06-16 22:27:27 +00:00
|
|
|
const setWeekdays = (idx: number, days: number[]) => {
|
|
|
|
schedules[idx].days = days;
|
|
|
|
setSchedules([].concat(schedules));
|
2021-06-24 22:15:18 +00:00
|
|
|
};
|
2021-06-16 22:27:27 +00:00
|
|
|
|
2021-06-14 18:53:20 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="rounded border flex">
|
|
|
|
<div className="w-3/5">
|
|
|
|
<div className="w-3/4 p-2">
|
|
|
|
<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"
|
|
|
|
value={timeZone}
|
|
|
|
onChange={setTimeZone}
|
|
|
|
className="shadow-sm focus:ring-blue-500 focus:border-blue-500 mt-1 block w-full sm:text-sm border-gray-300 rounded-md"
|
|
|
|
/>
|
2021-06-14 18:53:20 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<ul>
|
2021-06-24 22:15:18 +00:00
|
|
|
{schedules.map((schedule, idx) => (
|
2021-06-16 22:27:27 +00:00
|
|
|
<li key={idx} className="py-2 flex justify-between border-t">
|
2021-06-24 22:15:18 +00:00
|
|
|
<div className="inline-flex ml-2">
|
|
|
|
<WeekdaySelect
|
|
|
|
defaultValue={schedules[idx].days}
|
|
|
|
onSelect={(days: number[]) => setWeekdays(idx, days)}
|
|
|
|
/>
|
|
|
|
<button className="ml-2 text-sm px-2" type="button" onClick={() => setEditSchedule(idx)}>
|
|
|
|
{dayjs(schedule.startDate).format(schedule.startDate.minute() === 0 ? "ha" : "h:mma")}{" "}
|
|
|
|
until {dayjs(schedule.endDate).format(schedule.endDate.minute() === 0 ? "ha" : "h:mma")}
|
|
|
|
</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" />
|
2021-06-14 18:53:20 +00:00
|
|
|
</button>
|
2021-06-24 22:15:18 +00:00
|
|
|
</li>
|
|
|
|
))}
|
2021-06-14 18:53:20 +00:00
|
|
|
</ul>
|
|
|
|
<hr />
|
2021-06-24 22:15:18 +00:00
|
|
|
<button type="button" onClick={addNewSchedule} className="btn-white btn-sm m-2">
|
|
|
|
Add another
|
|
|
|
</button>
|
2021-06-14 18:53:20 +00:00
|
|
|
</div>
|
|
|
|
<div className="border-l p-2 w-2/5 text-sm bg-gray-50">
|
|
|
|
{/*<p className="font-bold mb-2">Add date overrides</p>
|
|
|
|
<p className="mb-2">
|
|
|
|
Add dates when your availability changes from your weekly hours
|
|
|
|
</p>
|
|
|
|
<button className="btn-sm btn-white">Add a date override</button>*/}
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-06-24 22:15:18 +00:00
|
|
|
{editSchedule >= 0 && (
|
|
|
|
<SetTimesModal
|
|
|
|
schedule={schedules[editSchedule]}
|
|
|
|
onChange={applyEditSchedule}
|
|
|
|
onExit={() => setEditSchedule(-1)}
|
|
|
|
/>
|
|
|
|
)}
|
2021-06-14 18:53:20 +00:00
|
|
|
{/*{showDateOverrideModal &&
|
|
|
|
<DateOverrideModal />
|
|
|
|
}*/}
|
|
|
|
</div>
|
|
|
|
);
|
2021-06-24 22:15:18 +00:00
|
|
|
};
|