2021-11-10 11:16:32 +00:00
|
|
|
import { PlusIcon, TrashIcon } from "@heroicons/react/outline";
|
2021-11-18 01:03:19 +00:00
|
|
|
import dayjs, { Dayjs, ConfigType } from "dayjs";
|
|
|
|
import timezone from "dayjs/plugin/timezone";
|
|
|
|
import utc from "dayjs/plugin/utc";
|
2021-11-18 03:29:36 +00:00
|
|
|
import React, { useCallback, useState } from "react";
|
2021-11-10 11:16:32 +00:00
|
|
|
import { Controller, useFieldArray } from "react-hook-form";
|
|
|
|
|
2021-11-11 05:44:53 +00:00
|
|
|
import { defaultDayRange } from "@lib/availability";
|
2021-11-10 11:16:32 +00:00
|
|
|
import { weekdayNames } from "@lib/core/i18n/weekday";
|
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
2021-11-11 05:44:53 +00:00
|
|
|
import { TimeRange } from "@lib/types/schedule";
|
2021-11-10 11:16:32 +00:00
|
|
|
|
2022-02-28 16:24:47 +00:00
|
|
|
import { useMeQuery } from "@components/Shell";
|
2021-11-10 11:16:32 +00:00
|
|
|
import Button from "@components/ui/Button";
|
|
|
|
import Select from "@components/ui/form/Select";
|
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
/** Begin Time Increments For Select */
|
|
|
|
const increment = 15;
|
|
|
|
/**
|
|
|
|
* Creates an array of times on a 15 minute interval from
|
|
|
|
* 00:00:00 (Start of day) to
|
|
|
|
* 23:45:00 (End of day with enough time for 15 min booking)
|
|
|
|
*/
|
|
|
|
const TIMES = (() => {
|
2022-02-08 16:01:18 +00:00
|
|
|
const end = dayjs().utc().endOf("day");
|
|
|
|
let t: Dayjs = dayjs().utc().startOf("day");
|
2021-11-10 11:16:32 +00:00
|
|
|
|
2022-02-10 17:42:06 +00:00
|
|
|
const times: Dayjs[] = [];
|
2021-11-10 11:16:32 +00:00
|
|
|
while (t.isBefore(end)) {
|
|
|
|
times.push(t);
|
|
|
|
t = t.add(increment, "minutes");
|
|
|
|
}
|
|
|
|
return times;
|
|
|
|
})();
|
|
|
|
/** End Time Increments For Select */
|
|
|
|
|
2021-11-18 03:29:36 +00:00
|
|
|
type Option = {
|
|
|
|
readonly label: string;
|
|
|
|
readonly value: number;
|
|
|
|
};
|
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
type TimeRangeFieldProps = {
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const TimeRangeField = ({ name }: TimeRangeFieldProps) => {
|
2022-02-28 16:24:47 +00:00
|
|
|
// Get user so we can determine 12/24 hour format preferences
|
|
|
|
const query = useMeQuery();
|
|
|
|
const user = query.data;
|
|
|
|
|
2021-11-18 03:29:36 +00:00
|
|
|
// Lazy-loaded options, otherwise adding a field has a noticable redraw delay.
|
|
|
|
const [options, setOptions] = useState<Option[]>([]);
|
2022-02-03 13:23:29 +00:00
|
|
|
const [selected, setSelected] = useState<number | undefined>();
|
2021-11-18 03:29:36 +00:00
|
|
|
// const { i18n } = useLocale();
|
2022-02-03 13:23:29 +00:00
|
|
|
|
|
|
|
const handleSelected = (value: number | undefined) => {
|
|
|
|
setSelected(value);
|
|
|
|
};
|
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
const getOption = (time: ConfigType) => ({
|
2021-11-18 03:29:36 +00:00
|
|
|
value: dayjs(time).toDate().valueOf(),
|
2022-02-28 16:24:47 +00:00
|
|
|
label: dayjs(time)
|
|
|
|
.utc()
|
|
|
|
.format(user && user.timeFormat === 12 ? "h:mma" : "HH:mm"),
|
2021-11-18 03:29:36 +00:00
|
|
|
// .toLocaleTimeString(i18n.language, { minute: "numeric", hour: "numeric" }),
|
2021-11-10 11:16:32 +00:00
|
|
|
});
|
|
|
|
|
2022-02-03 13:23:29 +00:00
|
|
|
const timeOptions = useCallback(
|
|
|
|
(offsetOrLimitorSelected: { offset?: number; limit?: number; selected?: number } = {}) => {
|
|
|
|
const { limit, offset, selected } = offsetOrLimitorSelected;
|
|
|
|
return TIMES.filter(
|
|
|
|
(time) =>
|
|
|
|
(!limit || time.isBefore(limit)) &&
|
|
|
|
(!offset || time.isAfter(offset)) &&
|
|
|
|
(!selected || time.isAfter(selected))
|
|
|
|
).map((t) => getOption(t));
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
2021-11-10 11:16:32 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Controller
|
|
|
|
name={`${name}.start`}
|
2022-02-03 13:23:29 +00:00
|
|
|
render={({ field: { onChange, value } }) => {
|
|
|
|
handleSelected(value);
|
|
|
|
return (
|
|
|
|
<Select
|
2022-02-28 16:24:47 +00:00
|
|
|
className="w-30"
|
2022-02-03 13:23:29 +00:00
|
|
|
options={options}
|
|
|
|
onFocus={() => setOptions(timeOptions())}
|
|
|
|
onBlur={() => setOptions([])}
|
|
|
|
defaultValue={getOption(value)}
|
|
|
|
onChange={(option) => {
|
|
|
|
onChange(new Date(option?.value as number));
|
|
|
|
handleSelected(option?.value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
2021-11-10 11:16:32 +00:00
|
|
|
/>
|
|
|
|
<span>-</span>
|
|
|
|
<Controller
|
|
|
|
name={`${name}.end`}
|
|
|
|
render={({ field: { onChange, value } }) => (
|
|
|
|
<Select
|
2022-02-28 16:24:47 +00:00
|
|
|
className="w-30"
|
2021-11-18 03:29:36 +00:00
|
|
|
options={options}
|
2022-02-03 13:23:29 +00:00
|
|
|
onFocus={() => setOptions(timeOptions({ selected }))}
|
2021-11-18 03:29:36 +00:00
|
|
|
onBlur={() => setOptions([])}
|
|
|
|
defaultValue={getOption(value)}
|
2021-11-10 11:16:32 +00:00
|
|
|
onChange={(option) => onChange(new Date(option?.value as number))}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
type ScheduleBlockProps = {
|
|
|
|
day: number;
|
|
|
|
weekday: string;
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const ScheduleBlock = ({ name, day, weekday }: ScheduleBlockProps) => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const { fields, append, remove, replace } = useFieldArray({
|
|
|
|
name: `${name}.${day}`,
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleAppend = () => {
|
|
|
|
// FIXME: Fix type-inference, can't get this to work. @see https://github.com/react-hook-form/react-hook-form/issues/4499
|
|
|
|
const nextRangeStart = dayjs((fields[fields.length - 1] as unknown as TimeRange).end);
|
|
|
|
const nextRangeEnd = dayjs(nextRangeStart).add(1, "hour");
|
|
|
|
|
|
|
|
if (nextRangeEnd.isBefore(nextRangeStart.endOf("day"))) {
|
|
|
|
return append({
|
|
|
|
start: nextRangeStart.toDate(),
|
|
|
|
end: nextRangeEnd.toDate(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-02-09 00:05:13 +00:00
|
|
|
<fieldset className="flex min-h-[86px] flex-col justify-between space-y-2 py-5 sm:flex-row sm:space-y-0">
|
2021-11-10 11:16:32 +00:00
|
|
|
<div className="w-1/3">
|
2022-02-03 13:23:29 +00:00
|
|
|
<label className="flex items-center space-x-2 rtl:space-x-reverse">
|
2021-11-10 11:16:32 +00:00
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
checked={fields.length > 0}
|
|
|
|
onChange={(e) => (e.target.checked ? replace([defaultDayRange]) : replace([]))}
|
2022-02-09 00:05:13 +00:00
|
|
|
className="inline-block rounded-sm border-gray-300 text-neutral-900 focus:ring-neutral-500"
|
2021-11-10 11:16:32 +00:00
|
|
|
/>
|
2021-11-11 05:44:53 +00:00
|
|
|
<span className="inline-block text-sm capitalize">{weekday}</span>
|
2021-11-10 11:16:32 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<div className="flex-grow">
|
|
|
|
{fields.map((field, index) => (
|
2022-02-09 00:05:13 +00:00
|
|
|
<div key={field.id} className="mb-1 flex justify-between">
|
2022-02-03 13:23:29 +00:00
|
|
|
<div className="flex items-center space-x-2 rtl:space-x-reverse">
|
2021-11-10 11:16:32 +00:00
|
|
|
<TimeRangeField name={`${name}.${day}.${index}`} />
|
|
|
|
</div>
|
|
|
|
<Button
|
|
|
|
size="icon"
|
|
|
|
color="minimal"
|
|
|
|
StartIcon={TrashIcon}
|
|
|
|
type="button"
|
|
|
|
onClick={() => remove(index)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
))}
|
2021-11-11 05:44:53 +00:00
|
|
|
<span className="block text-sm text-gray-500">{!fields.length && t("no_availability")}</span>
|
2021-11-10 11:16:32 +00:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
color="minimal"
|
|
|
|
size="icon"
|
|
|
|
className={fields.length > 0 ? "visible" : "invisible"}
|
|
|
|
StartIcon={PlusIcon}
|
|
|
|
onClick={handleAppend}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</fieldset>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const Schedule = ({ name }: { name: string }) => {
|
|
|
|
const { i18n } = useLocale();
|
|
|
|
return (
|
|
|
|
<fieldset className="divide-y divide-gray-200">
|
|
|
|
{weekdayNames(i18n.language).map((weekday, num) => (
|
|
|
|
<ScheduleBlock key={num} name={name} weekday={weekday} day={num} />
|
|
|
|
))}
|
|
|
|
</fieldset>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Schedule;
|