2023-03-03 13:02:02 +00:00
|
|
|
import type { UseFieldArrayRemove } from "react-hook-form";
|
2022-12-14 17:30:55 +00:00
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-03-03 13:02:02 +00:00
|
|
|
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
|
|
|
|
import type { TimeRange, WorkingHours } from "@calcom/types/schedule";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Button, DialogTrigger, Tooltip } from "@calcom/ui";
|
|
|
|
import { FiEdit2, FiTrash2 } from "@calcom/ui/components/icon";
|
2022-12-14 17:30:55 +00:00
|
|
|
|
|
|
|
import DateOverrideInputDialog from "./DateOverrideInputDialog";
|
|
|
|
|
2023-03-03 13:02:02 +00:00
|
|
|
const sortByDate = (a: { ranges: TimeRange[]; id: string }, b: { ranges: TimeRange[]; id: string }) => {
|
|
|
|
return a.ranges[0].start > b.ranges[0].start ? 1 : -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
const useSettings = () => {
|
|
|
|
const { data } = useMeQuery();
|
|
|
|
return {
|
|
|
|
hour12: data?.timeFormat === 12,
|
|
|
|
timeZone: data?.timeZone,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-12-14 17:30:55 +00:00
|
|
|
const DateOverrideList = ({
|
|
|
|
items,
|
|
|
|
remove,
|
|
|
|
update,
|
|
|
|
workingHours,
|
|
|
|
excludedDates = [],
|
|
|
|
}: {
|
|
|
|
remove: UseFieldArrayRemove;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
update: any;
|
|
|
|
items: { ranges: TimeRange[]; id: string }[];
|
|
|
|
workingHours: WorkingHours[];
|
|
|
|
excludedDates?: string[];
|
|
|
|
}) => {
|
|
|
|
const { t, i18n } = useLocale();
|
2023-03-03 13:02:02 +00:00
|
|
|
const { hour12 } = useSettings();
|
2022-12-14 17:30:55 +00:00
|
|
|
if (!items.length) {
|
|
|
|
return <></>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const timeSpan = ({ start, end }: TimeRange) => {
|
|
|
|
return (
|
2023-03-03 13:02:02 +00:00
|
|
|
new Intl.DateTimeFormat(i18n.language, { hour: "numeric", minute: "numeric", hour12 }).format(
|
2022-12-14 17:30:55 +00:00
|
|
|
new Date(start.toISOString().slice(0, -1))
|
|
|
|
) +
|
|
|
|
" - " +
|
2023-03-03 13:02:02 +00:00
|
|
|
new Intl.DateTimeFormat(i18n.language, { hour: "numeric", minute: "numeric", hour12 }).format(
|
2022-12-14 17:30:55 +00:00
|
|
|
new Date(end.toISOString().slice(0, -1))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-12-15 20:19:35 +00:00
|
|
|
<ul className="rounded border border-gray-200" data-testid="date-overrides-list">
|
2023-03-03 13:02:02 +00:00
|
|
|
{items.sort(sortByDate).map((item, index) => (
|
2022-12-14 17:30:55 +00:00
|
|
|
<li key={item.id} className="flex justify-between border-b px-5 py-4 last:border-b-0">
|
|
|
|
<div>
|
|
|
|
<h3 className="text-sm text-gray-900">
|
|
|
|
{new Intl.DateTimeFormat("en-GB", {
|
|
|
|
weekday: "short",
|
|
|
|
month: "long",
|
|
|
|
day: "numeric",
|
|
|
|
}).format(item.ranges[0].start)}
|
|
|
|
</h3>
|
2023-03-03 13:02:02 +00:00
|
|
|
{item.ranges[0].start.valueOf() - item.ranges[0].end.valueOf() === 0 ? (
|
2023-01-12 16:57:43 +00:00
|
|
|
<p className="text-xs text-gray-500">{t("unavailable")}</p>
|
2022-12-14 17:30:55 +00:00
|
|
|
) : (
|
|
|
|
item.ranges.map((range, i) => (
|
2023-01-12 16:57:43 +00:00
|
|
|
<p key={i} className="text-xs text-gray-500">
|
2022-12-14 17:30:55 +00:00
|
|
|
{timeSpan(range)}
|
|
|
|
</p>
|
|
|
|
))
|
|
|
|
)}
|
|
|
|
</div>
|
2023-02-06 01:31:05 +00:00
|
|
|
<div className="flex flex-row-reverse gap-5 space-x-2 rtl:space-x-reverse">
|
2022-12-14 17:30:55 +00:00
|
|
|
<DateOverrideInputDialog
|
|
|
|
excludedDates={excludedDates}
|
|
|
|
workingHours={workingHours}
|
|
|
|
value={item.ranges}
|
|
|
|
onChange={(ranges) => {
|
|
|
|
update(index, {
|
|
|
|
ranges,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
Trigger={
|
|
|
|
<DialogTrigger asChild>
|
|
|
|
<Button
|
|
|
|
tooltip={t("edit")}
|
|
|
|
className="text-gray-700"
|
|
|
|
color="minimal"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2023-01-23 23:08:01 +00:00
|
|
|
StartIcon={FiEdit2}
|
2022-12-14 17:30:55 +00:00
|
|
|
/>
|
|
|
|
</DialogTrigger>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<Tooltip content="Delete">
|
|
|
|
<Button
|
|
|
|
className="text-gray-700"
|
|
|
|
color="destructive"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2023-01-23 23:08:01 +00:00
|
|
|
StartIcon={FiTrash2}
|
2022-12-14 17:30:55 +00:00
|
|
|
onClick={() => remove(index)}
|
|
|
|
/>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DateOverrideList;
|