2022-09-12 10:15:30 +00:00
|
|
|
import autoAnimate from "@formkit/auto-animate";
|
|
|
|
import { useRef, useEffect } from "react";
|
|
|
|
|
2022-09-06 22:58:16 +00:00
|
|
|
import { NewScheduleButton, ScheduleListItem } from "@calcom/features/schedules";
|
2022-08-24 20:18:42 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { inferQueryOutput, trpc } from "@calcom/trpc/react";
|
|
|
|
import { Icon } from "@calcom/ui/Icon";
|
|
|
|
import Shell from "@calcom/ui/Shell";
|
2022-09-06 22:58:16 +00:00
|
|
|
import { EmptyScreen, showToast } from "@calcom/ui/v2";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
import { withQuery } from "@lib/QueryCell";
|
|
|
|
import { HttpError } from "@lib/core/http/error";
|
|
|
|
|
|
|
|
import SkeletonLoader from "@components/availability/SkeletonLoader";
|
|
|
|
|
|
|
|
export function AvailabilityList({ schedules }: inferQueryOutput<"viewer.availability.list">) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const utils = trpc.useContext();
|
2022-09-12 10:15:30 +00:00
|
|
|
const animationParentRef = useRef(null);
|
2022-09-06 18:34:29 +00:00
|
|
|
|
|
|
|
const meQuery = trpc.useQuery(["viewer.me"]);
|
|
|
|
|
2022-08-24 20:18:42 +00:00
|
|
|
const deleteMutation = trpc.useMutation("viewer.availability.schedule.delete", {
|
|
|
|
onSuccess: async () => {
|
|
|
|
await utils.invalidateQueries(["viewer.availability.list"]);
|
|
|
|
showToast(t("schedule_deleted_successfully"), "success");
|
|
|
|
},
|
|
|
|
onError: (err) => {
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
const message = `${err.statusCode}: ${err.message}`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2022-09-12 10:15:30 +00:00
|
|
|
|
|
|
|
// Adds smooth delete button - item fades and old item slides into place
|
|
|
|
useEffect(() => {
|
|
|
|
animationParentRef.current && autoAnimate(animationParentRef.current);
|
|
|
|
}, [animationParentRef]);
|
|
|
|
|
2022-08-24 20:18:42 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{schedules.length === 0 ? (
|
|
|
|
<div className="flex justify-center">
|
|
|
|
<EmptyScreen
|
|
|
|
Icon={Icon.FiClock}
|
|
|
|
headline={t("new_schedule_heading")}
|
|
|
|
description={t("new_schedule_description")}
|
|
|
|
buttonRaw={<NewScheduleButton />}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
2022-09-09 15:02:31 +00:00
|
|
|
<div className="mb-16 overflow-hidden rounded-md border border-gray-200 bg-white">
|
2022-09-12 10:15:30 +00:00
|
|
|
<ul className="divide-y divide-neutral-200" data-testid="schedules" ref={animationParentRef}>
|
2022-08-24 20:18:42 +00:00
|
|
|
{schedules.map((schedule) => (
|
|
|
|
<ScheduleListItem
|
2022-09-06 18:34:29 +00:00
|
|
|
displayOptions={{
|
|
|
|
hour12: meQuery.data?.timeFormat ? meQuery.data.timeFormat === 12 : undefined,
|
|
|
|
timeZone: meQuery.data?.timeZone,
|
|
|
|
}}
|
2022-08-24 20:18:42 +00:00
|
|
|
key={schedule.id}
|
|
|
|
schedule={schedule}
|
|
|
|
deleteFunction={deleteMutation.mutate}
|
|
|
|
isDeleting={deleteMutation.isLoading}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const WithQuery = withQuery(["viewer.availability.list"]);
|
|
|
|
|
|
|
|
export default function AvailabilityPage() {
|
|
|
|
const { t } = useLocale();
|
|
|
|
return (
|
|
|
|
<div>
|
2022-09-05 19:06:34 +00:00
|
|
|
<Shell heading={t("availability")} subtitle={t("configure_availability")} CTA={<NewScheduleButton />}>
|
2022-08-24 20:18:42 +00:00
|
|
|
<WithQuery success={({ data }) => <AvailabilityList {...data} />} customLoader={<SkeletonLoader />} />
|
|
|
|
</Shell>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|