2023-05-23 07:39:56 +00:00
|
|
|
import { Fragment, useCallback, useEffect, useMemo, useState, useRef } from "react";
|
2022-09-15 05:49:59 +00:00
|
|
|
import type {
|
|
|
|
ArrayPath,
|
|
|
|
Control,
|
2022-11-23 02:55:25 +00:00
|
|
|
ControllerRenderProps,
|
|
|
|
FieldArrayWithId,
|
|
|
|
FieldPath,
|
|
|
|
FieldPathValue,
|
|
|
|
FieldValues,
|
|
|
|
UseFieldArrayRemove,
|
2022-09-06 22:58:16 +00:00
|
|
|
} from "react-hook-form";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Controller, useFieldArray, useFormContext } from "react-hook-form";
|
2023-03-08 22:04:33 +00:00
|
|
|
import type { GroupBase, Props } from "react-select";
|
2022-09-06 22:58:16 +00:00
|
|
|
|
2023-03-08 22:04:33 +00:00
|
|
|
import type { ConfigType } from "@calcom/dayjs";
|
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-09-06 22:58:16 +00:00
|
|
|
import { defaultDayRange as DEFAULT_DAY_RANGE } from "@calcom/lib/availability";
|
2022-09-15 05:49:59 +00:00
|
|
|
import classNames from "@calcom/lib/classNames";
|
2022-09-06 22:58:16 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { weekdayNames } from "@calcom/lib/weekday";
|
|
|
|
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
|
2023-03-08 22:04:33 +00:00
|
|
|
import type { TimeRange } from "@calcom/types/schedule";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Dropdown,
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
Select,
|
|
|
|
SkeletonText,
|
|
|
|
Switch,
|
2023-07-19 19:45:13 +00:00
|
|
|
CheckboxField,
|
2022-11-23 02:55:25 +00:00
|
|
|
} from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { Copy, Plus, Trash } from "@calcom/ui/components/icon";
|
2022-09-06 22:58:16 +00:00
|
|
|
|
2022-12-14 17:30:55 +00:00
|
|
|
export type { TimeRange };
|
|
|
|
|
2022-09-15 05:49:59 +00:00
|
|
|
export type FieldPathByValue<TFieldValues extends FieldValues, TValue> = {
|
|
|
|
[Key in FieldPath<TFieldValues>]: FieldPathValue<TFieldValues, Key> extends TValue ? Key : never;
|
|
|
|
}[FieldPath<TFieldValues>];
|
|
|
|
|
|
|
|
const ScheduleDay = <TFieldValues extends FieldValues>({
|
|
|
|
name,
|
|
|
|
weekday,
|
|
|
|
control,
|
|
|
|
CopyButton,
|
|
|
|
}: {
|
2022-12-14 17:30:55 +00:00
|
|
|
name: ArrayPath<TFieldValues>;
|
2022-09-15 05:49:59 +00:00
|
|
|
weekday: string;
|
|
|
|
control: Control<TFieldValues>;
|
|
|
|
CopyButton: JSX.Element;
|
|
|
|
}) => {
|
|
|
|
const { watch, setValue } = useFormContext();
|
|
|
|
const watchDayRange = watch(name);
|
2022-09-06 22:58:16 +00:00
|
|
|
|
2022-09-15 05:49:59 +00:00
|
|
|
return (
|
2023-04-11 21:44:14 +00:00
|
|
|
<div className="mb-4 flex w-full flex-col last:mb-0 sm:flex-row sm:px-0">
|
2022-09-15 05:49:59 +00:00
|
|
|
{/* Label & switch container */}
|
2023-02-02 11:21:05 +00:00
|
|
|
<div className="flex h-[36px] items-center justify-between sm:w-32">
|
2022-09-15 05:49:59 +00:00
|
|
|
<div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<label className="text-default flex flex-row items-center space-x-2 rtl:space-x-reverse">
|
2022-09-15 05:49:59 +00:00
|
|
|
<div>
|
|
|
|
<Switch
|
|
|
|
disabled={!watchDayRange}
|
|
|
|
defaultChecked={watchDayRange && watchDayRange.length > 0}
|
|
|
|
checked={watchDayRange && !!watchDayRange.length}
|
|
|
|
onCheckedChange={(isChecked) => {
|
2022-12-14 17:30:55 +00:00
|
|
|
setValue(name, (isChecked ? [DEFAULT_DAY_RANGE] : []) as TFieldValues[typeof name]);
|
2022-09-15 05:49:59 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<span className="inline-block min-w-[88px] text-sm capitalize">{weekday}</span>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<>
|
|
|
|
{watchDayRange ? (
|
|
|
|
<div className="flex sm:ml-2">
|
|
|
|
<DayRanges control={control} name={name} />
|
2023-08-10 14:49:41 +00:00
|
|
|
{!!watchDayRange.length && <div className="block">{CopyButton}</div>}
|
2022-09-15 05:49:59 +00:00
|
|
|
</div>
|
|
|
|
) : (
|
2023-06-22 22:25:37 +00:00
|
|
|
<SkeletonText className="ml-1 mt-2.5 h-6 w-48" />
|
2022-09-15 05:49:59 +00:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2022-09-06 22:58:16 +00:00
|
|
|
|
2022-09-15 05:49:59 +00:00
|
|
|
const CopyButton = ({
|
|
|
|
getValuesFromDayRange,
|
|
|
|
weekStart,
|
|
|
|
}: {
|
|
|
|
getValuesFromDayRange: string;
|
|
|
|
weekStart: number;
|
|
|
|
}) => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const fieldArrayName = getValuesFromDayRange.substring(0, getValuesFromDayRange.lastIndexOf("."));
|
|
|
|
const { setValue, getValues } = useFormContext();
|
|
|
|
return (
|
|
|
|
<Dropdown open={open} onOpenChange={setOpen}>
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
<Button
|
2023-01-20 17:56:28 +00:00
|
|
|
className={classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"text-default",
|
|
|
|
open && "ring-brand-500 !bg-subtle outline-none ring-2 ring-offset-1"
|
2023-01-20 17:56:28 +00:00
|
|
|
)}
|
2022-09-15 05:49:59 +00:00
|
|
|
type="button"
|
2023-01-20 17:56:28 +00:00
|
|
|
tooltip={t("copy_times_to_tooltip")}
|
2022-09-15 05:49:59 +00:00
|
|
|
color="minimal"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Copy}
|
2022-09-15 05:49:59 +00:00
|
|
|
/>
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent align="end">
|
|
|
|
<CopyTimes
|
|
|
|
weekStart={weekStart}
|
|
|
|
disabled={parseInt(getValuesFromDayRange.replace(fieldArrayName + ".", ""), 10)}
|
|
|
|
onClick={(selected) => {
|
|
|
|
selected.forEach((day) => setValue(`${fieldArrayName}.${day}`, getValues(getValuesFromDayRange)));
|
2023-02-16 20:42:41 +00:00
|
|
|
setOpen(false);
|
2022-09-15 05:49:59 +00:00
|
|
|
}}
|
|
|
|
onCancel={() => setOpen(false)}
|
|
|
|
/>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</Dropdown>
|
2022-09-06 22:58:16 +00:00
|
|
|
);
|
2022-09-15 05:49:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const Schedule = <
|
|
|
|
TFieldValues extends FieldValues,
|
|
|
|
TPath extends FieldPathByValue<TFieldValues, TimeRange[][]>
|
|
|
|
>({
|
|
|
|
name,
|
|
|
|
control,
|
|
|
|
weekStart = 0,
|
|
|
|
}: {
|
|
|
|
name: TPath;
|
|
|
|
control: Control<TFieldValues>;
|
|
|
|
weekStart?: number;
|
|
|
|
}) => {
|
|
|
|
const { i18n } = useLocale();
|
2022-09-06 22:58:16 +00:00
|
|
|
|
|
|
|
return (
|
2023-02-02 18:28:46 +00:00
|
|
|
<div className="p-4">
|
2022-09-06 22:58:16 +00:00
|
|
|
{/* First iterate for each day */}
|
2022-09-15 05:49:59 +00:00
|
|
|
{weekdayNames(i18n.language, weekStart, "long").map((weekday, num) => {
|
|
|
|
const weekdayIndex = (num + weekStart) % 7;
|
2022-12-14 17:30:55 +00:00
|
|
|
const dayRangeName = `${name}.${weekdayIndex}` as ArrayPath<TFieldValues>;
|
2022-09-06 22:58:16 +00:00
|
|
|
return (
|
2022-09-15 05:49:59 +00:00
|
|
|
<ScheduleDay
|
|
|
|
name={dayRangeName}
|
|
|
|
key={weekday}
|
|
|
|
weekday={weekday}
|
|
|
|
control={control}
|
|
|
|
CopyButton={<CopyButton weekStart={weekStart} getValuesFromDayRange={dayRangeName} />}
|
|
|
|
/>
|
2022-09-06 22:58:16 +00:00
|
|
|
);
|
|
|
|
})}
|
2022-12-16 12:46:15 +00:00
|
|
|
</div>
|
2022-09-06 22:58:16 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-14 17:30:55 +00:00
|
|
|
export const DayRanges = <TFieldValues extends FieldValues>({
|
2022-09-06 22:58:16 +00:00
|
|
|
name,
|
2022-09-15 05:49:59 +00:00
|
|
|
control,
|
2022-09-06 22:58:16 +00:00
|
|
|
}: {
|
2022-12-14 17:30:55 +00:00
|
|
|
name: ArrayPath<TFieldValues>;
|
|
|
|
control?: Control<TFieldValues>;
|
2022-09-06 22:58:16 +00:00
|
|
|
}) => {
|
2022-09-15 05:49:59 +00:00
|
|
|
const { t } = useLocale();
|
2023-05-02 22:43:32 +00:00
|
|
|
const { getValues } = useFormContext();
|
2022-09-06 22:58:16 +00:00
|
|
|
|
2023-06-30 18:46:38 +00:00
|
|
|
const { remove, fields, prepend, append } = useFieldArray({
|
2022-09-15 05:49:59 +00:00
|
|
|
control,
|
2022-12-14 17:30:55 +00:00
|
|
|
name,
|
2022-09-06 22:58:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2022-09-15 05:49:59 +00:00
|
|
|
<div>
|
|
|
|
{fields.map((field, index: number) => (
|
|
|
|
<Fragment key={field.id}>
|
2023-04-11 21:44:14 +00:00
|
|
|
<div className="mb-2 flex last:mb-0">
|
2022-09-15 05:49:59 +00:00
|
|
|
<Controller name={`${name}.${index}`} render={({ field }) => <TimeRangeField {...field} />} />
|
|
|
|
{index === 0 && (
|
|
|
|
<Button
|
|
|
|
tooltip={t("add_time_availability")}
|
2023-04-05 18:14:46 +00:00
|
|
|
className="text-default mx-2 "
|
2022-09-15 05:49:59 +00:00
|
|
|
type="button"
|
|
|
|
color="minimal"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Plus}
|
2022-09-15 05:49:59 +00:00
|
|
|
onClick={() => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-06-30 18:46:38 +00:00
|
|
|
const slotRange: any = getDateSlotRange(
|
|
|
|
getValues(`${name}.${fields.length - 1}`),
|
|
|
|
getValues(`${name}.0`)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (slotRange?.append) {
|
|
|
|
append(slotRange.append);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slotRange?.prepend) {
|
|
|
|
prepend(slotRange.prepend);
|
|
|
|
}
|
2022-09-15 05:49:59 +00:00
|
|
|
}}
|
2022-09-06 22:58:16 +00:00
|
|
|
/>
|
2022-09-15 05:49:59 +00:00
|
|
|
)}
|
2023-05-15 13:56:26 +00:00
|
|
|
{index !== 0 && (
|
|
|
|
<RemoveTimeButton index={index} remove={remove} className="text-default mx-2 border-none" />
|
|
|
|
)}
|
2022-09-15 05:49:59 +00:00
|
|
|
</div>
|
|
|
|
</Fragment>
|
2022-09-06 22:58:16 +00:00
|
|
|
))}
|
2022-09-15 05:49:59 +00:00
|
|
|
</div>
|
2022-09-06 22:58:16 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const RemoveTimeButton = ({
|
|
|
|
index,
|
|
|
|
remove,
|
|
|
|
className,
|
|
|
|
}: {
|
|
|
|
index: number | number[];
|
|
|
|
remove: UseFieldArrayRemove;
|
|
|
|
className?: string;
|
|
|
|
}) => {
|
2023-05-14 17:46:19 +00:00
|
|
|
const { t } = useLocale();
|
2022-09-06 22:58:16 +00:00
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
type="button"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2023-05-10 15:15:13 +00:00
|
|
|
color="destructive"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Trash}
|
2022-09-06 22:58:16 +00:00
|
|
|
onClick={() => remove(index)}
|
|
|
|
className={className}
|
2023-05-14 17:46:19 +00:00
|
|
|
tooltip={t("delete")}
|
2022-09-06 22:58:16 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-15 05:49:59 +00:00
|
|
|
const TimeRangeField = ({ className, value, onChange }: { className?: string } & ControllerRenderProps) => {
|
|
|
|
// this is a controlled component anyway given it uses LazySelect, so keep it RHF agnostic.
|
2022-09-06 22:58:16 +00:00
|
|
|
return (
|
2023-08-10 14:49:41 +00:00
|
|
|
<div className={classNames("flex flex-row gap-1",className)}>
|
2022-09-15 05:49:59 +00:00
|
|
|
<LazySelect
|
2023-02-02 11:21:05 +00:00
|
|
|
className="inline-block w-[100px]"
|
2022-09-15 05:49:59 +00:00
|
|
|
value={value.start}
|
|
|
|
max={value.end}
|
|
|
|
onChange={(option) => {
|
|
|
|
onChange({ ...value, start: new Date(option?.value as number) });
|
2022-09-06 22:58:16 +00:00
|
|
|
}}
|
|
|
|
/>
|
2023-04-05 18:14:46 +00:00
|
|
|
<span className="text-default mx-2 w-2 self-center"> - </span>
|
2022-09-15 05:49:59 +00:00
|
|
|
<LazySelect
|
|
|
|
className="inline-block w-[100px] rounded-md"
|
|
|
|
value={value.end}
|
|
|
|
min={value.start}
|
|
|
|
onChange={(option) => {
|
|
|
|
onChange({ ...value, end: new Date(option?.value as number) });
|
|
|
|
}}
|
2022-09-06 22:58:16 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const LazySelect = ({
|
|
|
|
value,
|
|
|
|
min,
|
|
|
|
max,
|
|
|
|
...props
|
|
|
|
}: Omit<Props<IOption, false, GroupBase<IOption>>, "value"> & {
|
|
|
|
value: ConfigType;
|
|
|
|
min?: ConfigType;
|
|
|
|
max?: ConfigType;
|
|
|
|
}) => {
|
|
|
|
// Lazy-loaded options, otherwise adding a field has a noticeable redraw delay.
|
|
|
|
const { options, filter } = useOptions();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
filter({ current: value });
|
|
|
|
}, [filter, value]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
options={options}
|
|
|
|
onMenuOpen={() => {
|
|
|
|
if (min) filter({ offset: min });
|
|
|
|
if (max) filter({ limit: max });
|
|
|
|
}}
|
|
|
|
value={options.find((option) => option.value === dayjs(value).toDate().valueOf())}
|
|
|
|
onMenuClose={() => filter({ current: value })}
|
|
|
|
components={{ DropdownIndicator: () => null, IndicatorSeparator: () => null }}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
interface IOption {
|
|
|
|
readonly label: string;
|
|
|
|
readonly value: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
|
|
|
/** Begin Time Increments For Select */
|
|
|
|
const INCREMENT = 15;
|
|
|
|
const useOptions = () => {
|
|
|
|
// Get user so we can determine 12/24 hour format preferences
|
|
|
|
const query = useMeQuery();
|
|
|
|
const { timeFormat } = query.data || { timeFormat: null };
|
|
|
|
|
|
|
|
const [filteredOptions, setFilteredOptions] = useState<IOption[]>([]);
|
|
|
|
|
|
|
|
const options = useMemo(() => {
|
|
|
|
const end = dayjs().utc().endOf("day");
|
|
|
|
const options: IOption[] = [];
|
2022-12-21 16:13:50 +00:00
|
|
|
for (
|
|
|
|
let t = dayjs().utc().startOf("day");
|
|
|
|
t.isBefore(end);
|
|
|
|
t = t.add(INCREMENT + (!t.add(INCREMENT).isSame(t, "day") ? -1 : 0), "minutes")
|
|
|
|
) {
|
2022-09-06 22:58:16 +00:00
|
|
|
options.push({
|
|
|
|
value: t.toDate().valueOf(),
|
|
|
|
label: dayjs(t)
|
|
|
|
.utc()
|
|
|
|
.format(timeFormat === 12 ? "h:mma" : "HH:mm"),
|
|
|
|
});
|
|
|
|
}
|
2022-12-21 16:13:50 +00:00
|
|
|
// allow 23:59
|
|
|
|
options.push({
|
|
|
|
value: end.toDate().valueOf(),
|
|
|
|
label: dayjs(end)
|
|
|
|
.utc()
|
|
|
|
.format(timeFormat === 12 ? "h:mma" : "HH:mm"),
|
|
|
|
});
|
2022-09-06 22:58:16 +00:00
|
|
|
return options;
|
|
|
|
}, [timeFormat]);
|
|
|
|
|
|
|
|
const filter = useCallback(
|
|
|
|
({ offset, limit, current }: { offset?: ConfigType; limit?: ConfigType; current?: ConfigType }) => {
|
|
|
|
if (current) {
|
|
|
|
const currentOption = options.find((option) => option.value === dayjs(current).toDate().valueOf());
|
|
|
|
if (currentOption) setFilteredOptions([currentOption]);
|
|
|
|
} else
|
|
|
|
setFilteredOptions(
|
|
|
|
options.filter((option) => {
|
|
|
|
const time = dayjs(option.value);
|
|
|
|
return (!limit || time.isBefore(limit)) && (!offset || time.isAfter(offset));
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
[options]
|
|
|
|
);
|
|
|
|
|
|
|
|
return { options: filteredOptions, filter };
|
|
|
|
};
|
|
|
|
|
2023-06-30 18:46:38 +00:00
|
|
|
const getDateSlotRange = (endField?: FieldArrayWithId, startField?: FieldArrayWithId) => {
|
|
|
|
const timezoneStartRange = dayjs((startField as unknown as TimeRange).start).utc();
|
|
|
|
const nextRangeStart = dayjs((endField as unknown as TimeRange).end).utc();
|
2023-03-21 17:56:16 +00:00
|
|
|
const nextRangeEnd =
|
|
|
|
nextRangeStart.hour() === 23
|
|
|
|
? dayjs(nextRangeStart).add(59, "minutes").add(59, "seconds").add(999, "milliseconds")
|
|
|
|
: dayjs(nextRangeStart).add(1, "hour");
|
2022-09-06 22:58:16 +00:00
|
|
|
|
2023-06-30 18:46:38 +00:00
|
|
|
const endOfDay = nextRangeStart.endOf("day");
|
|
|
|
|
|
|
|
if (!nextRangeStart.isSame(endOfDay)) {
|
|
|
|
return {
|
|
|
|
append: {
|
|
|
|
start: nextRangeStart.toDate(),
|
|
|
|
end: nextRangeEnd.isAfter(endOfDay) ? endOfDay.toDate() : nextRangeEnd.toDate(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const previousRangeStart = dayjs((startField as unknown as TimeRange).start).subtract(1, "hour");
|
|
|
|
const startOfDay = timezoneStartRange.startOf("day");
|
|
|
|
|
|
|
|
if (!timezoneStartRange.isSame(startOfDay)) {
|
2022-09-15 05:49:59 +00:00
|
|
|
return {
|
2023-06-30 18:46:38 +00:00
|
|
|
prepend: {
|
|
|
|
start: previousRangeStart.isBefore(startOfDay) ? startOfDay.toDate() : previousRangeStart.toDate(),
|
|
|
|
end: timezoneStartRange.toDate(),
|
|
|
|
},
|
2022-09-15 05:49:59 +00:00
|
|
|
};
|
2022-09-06 22:58:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-15 05:49:59 +00:00
|
|
|
const CopyTimes = ({
|
|
|
|
disabled,
|
|
|
|
onClick,
|
|
|
|
onCancel,
|
|
|
|
weekStart,
|
|
|
|
}: {
|
|
|
|
disabled: number;
|
|
|
|
onClick: (selected: number[]) => void;
|
|
|
|
onCancel: () => void;
|
|
|
|
weekStart: number;
|
|
|
|
}) => {
|
2022-09-06 22:58:16 +00:00
|
|
|
const [selected, setSelected] = useState<number[]>([]);
|
|
|
|
const { i18n, t } = useLocale();
|
2023-05-23 07:39:56 +00:00
|
|
|
const itteratablesByKeyRef = useRef<(HTMLInputElement | HTMLButtonElement)[]>([]);
|
|
|
|
useEffect(() => {
|
|
|
|
document.addEventListener("keydown", handleKeyDown);
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener("keydown", handleKeyDown);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
|
|
const itteratables = itteratablesByKeyRef.current;
|
|
|
|
const isActionRequired =
|
|
|
|
event.key === "Tab" || event.key === "ArrowUp" || event.key === "ArrowDown" || event.key === "Enter";
|
|
|
|
if (!isActionRequired || !itteratables.length) return;
|
|
|
|
event.preventDefault();
|
|
|
|
const currentFocused = document.activeElement as HTMLInputElement | HTMLButtonElement;
|
|
|
|
let currentIndex = itteratables.findIndex((checkbox) => checkbox === currentFocused);
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
if (currentIndex === -1) return;
|
|
|
|
currentFocused.click();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (currentIndex === -1) {
|
|
|
|
itteratables[0].focus();
|
|
|
|
} else {
|
|
|
|
// Move focus based on the arrow key pressed
|
|
|
|
if (event.key === "ArrowUp") {
|
|
|
|
currentIndex = (currentIndex - 1 + itteratables.length) % itteratables.length;
|
|
|
|
} else if (event.key === "ArrowDown" || event.key === "Tab") {
|
|
|
|
currentIndex = (currentIndex + 1) % itteratables.length;
|
|
|
|
}
|
|
|
|
itteratables[currentIndex].focus();
|
|
|
|
}
|
|
|
|
};
|
2022-09-06 22:58:16 +00:00
|
|
|
|
|
|
|
return (
|
2022-09-15 05:49:59 +00:00
|
|
|
<div className="space-y-2 py-2">
|
|
|
|
<div className="p-2">
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="h6 text-emphasis pb-3 pl-1 text-xs font-medium uppercase">{t("copy_times_to")}</p>
|
2022-09-15 05:49:59 +00:00
|
|
|
<ol className="space-y-2">
|
2023-05-14 16:00:55 +00:00
|
|
|
<li key="select all">
|
|
|
|
<label className="text-default flex w-full items-center justify-between">
|
2023-05-15 13:56:26 +00:00
|
|
|
<span className="px-1">{t("select_all")}</span>
|
2023-07-19 19:45:13 +00:00
|
|
|
<CheckboxField
|
2023-05-15 13:56:26 +00:00
|
|
|
description=""
|
|
|
|
value={t("select_all")}
|
2023-05-14 16:00:55 +00:00
|
|
|
checked={selected.length === 7}
|
|
|
|
onChange={(e) => {
|
|
|
|
if (e.target.checked) {
|
|
|
|
setSelected([0, 1, 2, 3, 4, 5, 6]);
|
|
|
|
} else if (!e.target.checked) {
|
|
|
|
setSelected([]);
|
|
|
|
}
|
|
|
|
}}
|
2023-05-23 07:39:56 +00:00
|
|
|
ref={(ref) => {
|
|
|
|
if (ref) {
|
|
|
|
itteratablesByKeyRef.current.push(ref as HTMLInputElement);
|
|
|
|
}
|
|
|
|
}}
|
2023-05-14 16:00:55 +00:00
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</li>
|
2022-09-15 05:49:59 +00:00
|
|
|
{weekdayNames(i18n.language, weekStart).map((weekday, num) => {
|
|
|
|
const weekdayIndex = (num + weekStart) % 7;
|
|
|
|
return (
|
|
|
|
<li key={weekday}>
|
2023-04-05 18:14:46 +00:00
|
|
|
<label className="text-default flex w-full items-center justify-between">
|
2022-09-15 05:49:59 +00:00
|
|
|
<span className="px-1">{weekday}</span>
|
2023-07-19 19:45:13 +00:00
|
|
|
<CheckboxField
|
2023-05-15 13:56:26 +00:00
|
|
|
description=""
|
2022-09-15 05:49:59 +00:00
|
|
|
value={weekdayIndex}
|
2023-05-14 16:00:55 +00:00
|
|
|
checked={selected.includes(weekdayIndex) || disabled === weekdayIndex}
|
2022-09-15 05:49:59 +00:00
|
|
|
disabled={disabled === weekdayIndex}
|
|
|
|
onChange={(e) => {
|
|
|
|
if (e.target.checked && !selected.includes(weekdayIndex)) {
|
|
|
|
setSelected(selected.concat([weekdayIndex]));
|
|
|
|
} else if (!e.target.checked && selected.includes(weekdayIndex)) {
|
2023-05-15 13:56:26 +00:00
|
|
|
setSelected(selected.filter((item) => item !== weekdayIndex));
|
2022-09-15 05:49:59 +00:00
|
|
|
}
|
|
|
|
}}
|
2023-05-23 07:39:56 +00:00
|
|
|
ref={(ref) => {
|
|
|
|
if (ref && disabled !== weekdayIndex) {
|
|
|
|
//we don't need to iterate over disabled elements
|
|
|
|
itteratablesByKeyRef.current.push(ref as HTMLInputElement);
|
|
|
|
}
|
|
|
|
}}
|
2022-09-15 05:49:59 +00:00
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ol>
|
|
|
|
</div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<hr className="border-subtle" />
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="space-x-2 px-2 rtl:space-x-reverse">
|
2023-05-23 07:39:56 +00:00
|
|
|
<Button
|
|
|
|
color="minimal"
|
|
|
|
onClick={() => onCancel()}
|
|
|
|
ref={(ref) => {
|
|
|
|
if (ref) {
|
|
|
|
itteratablesByKeyRef.current.push(ref as HTMLButtonElement);
|
|
|
|
}
|
|
|
|
}}>
|
2022-09-15 05:49:59 +00:00
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
2023-05-23 07:39:56 +00:00
|
|
|
<Button
|
|
|
|
color="primary"
|
|
|
|
onClick={() => onClick(selected)}
|
|
|
|
ref={(ref) => {
|
|
|
|
if (ref) {
|
|
|
|
itteratablesByKeyRef.current.push(ref as HTMLButtonElement);
|
|
|
|
}
|
|
|
|
}}>
|
2022-09-06 22:58:16 +00:00
|
|
|
{t("apply")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Schedule;
|