2022-08-24 20:18:42 +00:00
|
|
|
import Link from "next/link";
|
|
|
|
import { Fragment } from "react";
|
|
|
|
|
|
|
|
import { availabilityAsString } from "@calcom/lib/availability";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-02-24 14:21:31 +00:00
|
|
|
import type { RouterOutputs } from "@calcom/trpc/react";
|
2022-12-07 19:35:55 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
|
|
|
Badge,
|
|
|
|
Button,
|
|
|
|
Dropdown,
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuItem,
|
2023-01-15 11:40:40 +00:00
|
|
|
DropdownItem,
|
2022-11-23 02:55:25 +00:00
|
|
|
DropdownMenuTrigger,
|
2023-04-05 15:42:22 +00:00
|
|
|
showToast,
|
2022-11-23 02:55:25 +00:00
|
|
|
} from "@calcom/ui";
|
2023-06-26 13:00:19 +00:00
|
|
|
|
|
|
|
import { Globe, MoreHorizontal, Trash, Star, Copy } from "@calcom/ui/components/icon";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
export function ScheduleListItem({
|
|
|
|
schedule,
|
|
|
|
deleteFunction,
|
2022-09-06 18:34:29 +00:00
|
|
|
displayOptions,
|
2022-12-07 19:35:55 +00:00
|
|
|
updateDefault,
|
2023-04-05 15:42:22 +00:00
|
|
|
isDeletable,
|
2023-06-26 11:35:20 +00:00
|
|
|
duplicateFunction,
|
2022-08-24 20:18:42 +00:00
|
|
|
}: {
|
2022-11-10 23:40:01 +00:00
|
|
|
schedule: RouterOutputs["viewer"]["availability"]["list"]["schedules"][number];
|
2022-08-24 20:18:42 +00:00
|
|
|
deleteFunction: ({ scheduleId }: { scheduleId: number }) => void;
|
2022-09-06 18:34:29 +00:00
|
|
|
displayOptions?: {
|
|
|
|
timeZone?: string;
|
|
|
|
hour12?: boolean;
|
|
|
|
};
|
2023-04-05 15:42:22 +00:00
|
|
|
isDeletable: boolean;
|
2022-12-14 17:30:55 +00:00
|
|
|
updateDefault: ({ scheduleId, isDefault }: { scheduleId: number; isDefault: boolean }) => void;
|
2023-06-26 11:35:20 +00:00
|
|
|
duplicateFunction: ({ scheduleId }: { scheduleId: number }) => void;
|
2022-08-24 20:18:42 +00:00
|
|
|
}) {
|
|
|
|
const { t, i18n } = useLocale();
|
|
|
|
|
2022-12-07 19:35:55 +00:00
|
|
|
const { data, isLoading } = trpc.viewer.availability.schedule.get.useQuery({ scheduleId: schedule.id });
|
|
|
|
|
2022-08-24 20:18:42 +00:00
|
|
|
return (
|
|
|
|
<li key={schedule.id}>
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="hover:bg-muted flex items-center justify-between py-5 ltr:pl-4 rtl:pr-4 sm:ltr:pl-0 sm:rtl:pr-0">
|
|
|
|
<div className="group flex w-full items-center justify-between sm:px-6">
|
2023-01-06 12:13:56 +00:00
|
|
|
<Link
|
|
|
|
href={"/availability/" + schedule.id}
|
|
|
|
className="flex-grow truncate text-sm"
|
|
|
|
title={schedule.name}>
|
|
|
|
<div className="space-x-2 rtl:space-x-reverse">
|
2023-04-05 18:14:46 +00:00
|
|
|
<span className="text-emphasis truncate font-medium">{schedule.name}</span>
|
2023-01-06 12:13:56 +00:00
|
|
|
{schedule.isDefault && (
|
|
|
|
<Badge variant="success" className="text-xs">
|
|
|
|
{t("default")}
|
|
|
|
</Badge>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-subtle mt-1">
|
2023-01-06 12:13:56 +00:00
|
|
|
{schedule.availability
|
|
|
|
.filter((availability) => !!availability.days.length)
|
|
|
|
.map((availability) => (
|
|
|
|
<Fragment key={availability.id}>
|
|
|
|
{availabilityAsString(availability, {
|
|
|
|
locale: i18n.language,
|
|
|
|
hour12: displayOptions?.hour12,
|
|
|
|
})}
|
|
|
|
<br />
|
|
|
|
</Fragment>
|
|
|
|
))}
|
2023-02-24 14:21:31 +00:00
|
|
|
{(schedule.timeZone || displayOptions?.timeZone) && (
|
|
|
|
<p className="my-1 flex items-center first-letter:text-xs">
|
2023-04-12 15:26:31 +00:00
|
|
|
<Globe className="h-3.5 w-3.5" />
|
2023-02-24 14:21:31 +00:00
|
|
|
{schedule.timeZone ?? displayOptions?.timeZone}
|
|
|
|
</p>
|
|
|
|
)}
|
2023-01-06 12:13:56 +00:00
|
|
|
</p>
|
2022-08-24 20:18:42 +00:00
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
<Dropdown>
|
2023-01-22 00:03:59 +00:00
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
<Button
|
2023-04-05 15:42:22 +00:00
|
|
|
data-testid="schedule-more"
|
2023-01-22 00:03:59 +00:00
|
|
|
className="mx-5"
|
|
|
|
type="button"
|
|
|
|
variant="icon"
|
|
|
|
color="secondary"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={MoreHorizontal}
|
2023-01-22 00:03:59 +00:00
|
|
|
/>
|
2022-08-24 20:18:42 +00:00
|
|
|
</DropdownMenuTrigger>
|
2022-12-07 19:35:55 +00:00
|
|
|
{!isLoading && data && (
|
|
|
|
<DropdownMenuContent>
|
2023-04-05 18:14:46 +00:00
|
|
|
<DropdownMenuItem className="min-w-40 focus:ring-muted">
|
2022-12-07 19:35:55 +00:00
|
|
|
{!schedule.isDefault && (
|
2023-01-15 11:40:40 +00:00
|
|
|
<DropdownItem
|
2022-12-07 19:35:55 +00:00
|
|
|
type="button"
|
2023-06-26 13:00:19 +00:00
|
|
|
StartIcon={Star}
|
2022-12-07 19:35:55 +00:00
|
|
|
onClick={() => {
|
|
|
|
updateDefault({
|
|
|
|
scheduleId: schedule.id,
|
|
|
|
isDefault: true,
|
|
|
|
});
|
|
|
|
}}>
|
|
|
|
{t("set_as_default")}
|
2023-01-15 11:40:40 +00:00
|
|
|
</DropdownItem>
|
2022-12-07 19:35:55 +00:00
|
|
|
)}
|
|
|
|
</DropdownMenuItem>
|
2023-06-26 11:35:20 +00:00
|
|
|
<DropdownMenuItem className="outline-none">
|
|
|
|
<DropdownItem
|
|
|
|
type="button"
|
|
|
|
data-testid={"schedule-duplicate" + schedule.id}
|
|
|
|
StartIcon={Copy}
|
|
|
|
onClick={() => {
|
|
|
|
duplicateFunction({
|
|
|
|
scheduleId: schedule.id,
|
|
|
|
});
|
|
|
|
}}>
|
|
|
|
{t("duplicate")}
|
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenuItem>
|
2023-04-05 18:14:46 +00:00
|
|
|
<DropdownMenuItem className="min-w-40 focus:ring-muted">
|
2023-01-15 11:40:40 +00:00
|
|
|
<DropdownItem
|
|
|
|
type="button"
|
|
|
|
color="destructive"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Trash}
|
2023-04-05 15:42:22 +00:00
|
|
|
data-testid="delete-schedule"
|
2022-12-07 19:35:55 +00:00
|
|
|
onClick={() => {
|
2023-04-05 15:42:22 +00:00
|
|
|
if (!isDeletable) {
|
|
|
|
showToast(t("requires_at_least_one_schedule"), "error");
|
|
|
|
} else {
|
|
|
|
deleteFunction({
|
|
|
|
scheduleId: schedule.id,
|
|
|
|
});
|
|
|
|
}
|
2023-01-15 11:40:40 +00:00
|
|
|
}}>
|
2022-12-07 19:35:55 +00:00
|
|
|
{t("delete")}
|
2023-01-15 11:40:40 +00:00
|
|
|
</DropdownItem>
|
2022-12-07 19:35:55 +00:00
|
|
|
</DropdownMenuItem>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
)}
|
2022-08-24 20:18:42 +00:00
|
|
|
</Dropdown>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|