2022-03-17 16:48:23 +00:00
|
|
|
import { ClockIcon } from "@heroicons/react/outline";
|
|
|
|
import { DotsHorizontalIcon, TrashIcon } from "@heroicons/react/solid";
|
|
|
|
import { Availability } from "@prisma/client";
|
|
|
|
import Link from "next/link";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-03-17 16:48:23 +00:00
|
|
|
import { availabilityAsString } from "@calcom/lib/availability";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-03-16 23:36:43 +00:00
|
|
|
import showToast from "@calcom/lib/notification";
|
2022-03-17 16:48:23 +00:00
|
|
|
import { Button } from "@calcom/ui";
|
|
|
|
import Dropdown, { DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@calcom/ui/Dropdown";
|
2022-03-16 23:36:43 +00:00
|
|
|
|
2022-04-14 21:49:51 +00:00
|
|
|
import { withQuery } from "@lib/QueryCell";
|
2022-03-17 16:48:23 +00:00
|
|
|
import { HttpError } from "@lib/core/http/error";
|
2021-11-10 11:16:32 +00:00
|
|
|
import { inferQueryOutput, trpc } from "@lib/trpc";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-03-17 19:13:39 +00:00
|
|
|
import EmptyScreen from "@components/EmptyScreen";
|
2021-09-22 19:52:38 +00:00
|
|
|
import Shell from "@components/Shell";
|
2022-03-17 16:48:23 +00:00
|
|
|
import { NewScheduleButton } from "@components/availability/NewScheduleButton";
|
2022-04-25 17:01:51 +00:00
|
|
|
import SkeletonLoader from "@components/availability/SkeletonLoader";
|
2021-09-30 21:37:52 +00:00
|
|
|
|
2022-03-17 16:48:23 +00:00
|
|
|
export function AvailabilityList({ schedules }: inferQueryOutput<"viewer.availability.list">) {
|
|
|
|
const { t, i18n } = useLocale();
|
2022-03-23 23:23:18 +00:00
|
|
|
const utils = trpc.useContext();
|
2022-03-17 16:48:23 +00:00
|
|
|
const deleteMutation = trpc.useMutation("viewer.availability.schedule.delete", {
|
|
|
|
onSuccess: async () => {
|
2022-03-23 23:23:18 +00:00
|
|
|
await utils.invalidateQueries(["viewer.availability.list"]);
|
2022-03-17 16:48:23 +00:00
|
|
|
showToast(t("schedule_deleted_successfully"), "success");
|
|
|
|
},
|
|
|
|
onError: (err) => {
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
const message = `${err.statusCode}: ${err.message}`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
2021-11-10 11:16:32 +00:00
|
|
|
},
|
|
|
|
});
|
2021-07-18 13:12:18 +00:00
|
|
|
return (
|
2022-03-17 19:13:39 +00:00
|
|
|
<>
|
|
|
|
{schedules.length === 0 ? (
|
|
|
|
<EmptyScreen
|
|
|
|
Icon={ClockIcon}
|
|
|
|
headline={t("new_schedule_heading")}
|
|
|
|
description={t("new_schedule_description")}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="-mx-4 mb-16 overflow-hidden rounded-sm border border-gray-200 bg-white sm:mx-0">
|
|
|
|
<ul className="divide-y divide-neutral-200" data-testid="schedules">
|
|
|
|
{schedules.map((schedule) => (
|
|
|
|
<li key={schedule.id}>
|
2022-03-17 20:49:20 +00:00
|
|
|
<div className="flex items-center justify-between py-5 hover:bg-neutral-50 ltr:pl-4 rtl:pr-4 sm:ltr:pl-0 sm:rtl:pr-0">
|
2022-03-17 19:13:39 +00:00
|
|
|
<div className="group flex w-full items-center justify-between hover:bg-neutral-50 sm:px-6">
|
|
|
|
<Link href={"/availability/" + schedule.id}>
|
|
|
|
<a className="flex-grow truncate text-sm" title={schedule.name}>
|
|
|
|
<div>
|
|
|
|
<span className="truncate font-medium text-neutral-900">{schedule.name}</span>
|
|
|
|
{schedule.isDefault && (
|
|
|
|
<span className="ml-2 inline items-center rounded-sm bg-yellow-100 px-1.5 py-0.5 text-xs font-medium text-yellow-800">
|
|
|
|
{t("default")}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<p className="mt-1 text-xs text-neutral-500">
|
|
|
|
{schedule.availability.map((availability: Availability) => (
|
|
|
|
<>
|
|
|
|
{availabilityAsString(availability, i18n.language)}
|
|
|
|
<br />
|
|
|
|
</>
|
|
|
|
))}
|
|
|
|
</p>
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
<Dropdown>
|
2022-03-18 18:22:56 +00:00
|
|
|
<DropdownMenuTrigger className="group mr-5 h-10 w-10 border border-transparent p-0 text-neutral-500 hover:border-gray-200">
|
2022-03-17 19:13:39 +00:00
|
|
|
<DotsHorizontalIcon className="h-5 w-5 group-hover:text-gray-800" />
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent>
|
|
|
|
<DropdownMenuItem>
|
|
|
|
<Button
|
|
|
|
onClick={() =>
|
|
|
|
deleteMutation.mutate({
|
|
|
|
scheduleId: schedule.id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
type="button"
|
2022-03-18 18:22:56 +00:00
|
|
|
color="warn"
|
2022-03-17 19:13:39 +00:00
|
|
|
className="w-full font-normal"
|
|
|
|
StartIcon={TrashIcon}>
|
|
|
|
{t("delete_schedule")}
|
|
|
|
</Button>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</Dropdown>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
2021-11-10 11:16:32 +00:00
|
|
|
);
|
|
|
|
}
|
2021-04-08 14:20:38 +00:00
|
|
|
|
2022-04-14 21:49:51 +00:00
|
|
|
const WithQuery = withQuery(["viewer.availability.list"]);
|
|
|
|
|
2022-03-17 16:48:23 +00:00
|
|
|
export default function AvailabilityPage() {
|
2021-11-10 11:16:32 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
return (
|
|
|
|
<div>
|
2022-04-25 17:01:51 +00:00
|
|
|
<Shell
|
|
|
|
heading={t("availability")}
|
|
|
|
subtitle={t("configure_availability")}
|
|
|
|
CTA={<NewScheduleButton />}
|
|
|
|
customLoader={<SkeletonLoader />}>
|
|
|
|
<WithQuery success={({ data }) => <AvailabilityList {...data} />} customLoader={<SkeletonLoader />} />
|
2021-07-18 13:12:18 +00:00
|
|
|
</Shell>
|
|
|
|
</div>
|
|
|
|
);
|
2021-04-08 14:20:38 +00:00
|
|
|
}
|