2022-10-20 05:09:52 +00:00
|
|
|
import { useAutoAnimate } from "@formkit/auto-animate/react";
|
2022-09-12 10:15:30 +00:00
|
|
|
|
2022-09-06 22:58:16 +00:00
|
|
|
import { NewScheduleButton, ScheduleListItem } from "@calcom/features/schedules";
|
2023-01-10 15:39:29 +00:00
|
|
|
import Shell from "@calcom/features/shell/Shell";
|
2022-08-24 20:18:42 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { RouterOutputs } from "@calcom/trpc/react";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { EmptyScreen, showToast } from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { Clock } from "@calcom/ui/components/icon";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
import { withQuery } from "@lib/QueryCell";
|
|
|
|
import { HttpError } from "@lib/core/http/error";
|
|
|
|
|
2022-11-01 13:29:01 +00:00
|
|
|
import SkeletonLoader from "@components/availability/SkeletonLoader";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
export function AvailabilityList({ schedules }: RouterOutputs["viewer"]["availability"]["list"]) {
|
2022-08-24 20:18:42 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
const utils = trpc.useContext();
|
2022-09-06 18:34:29 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const meQuery = trpc.viewer.me.useQuery();
|
2022-09-06 18:34:29 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const deleteMutation = trpc.viewer.availability.schedule.delete.useMutation({
|
2022-09-21 21:04:21 +00:00
|
|
|
onMutate: async ({ scheduleId }) => {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.availability.list.cancel();
|
|
|
|
const previousValue = utils.viewer.availability.list.getData();
|
2022-09-21 21:04:21 +00:00
|
|
|
if (previousValue) {
|
|
|
|
const filteredValue = previousValue.schedules.filter(({ id }) => id !== scheduleId);
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.availability.list.setData(undefined, { ...previousValue, schedules: filteredValue });
|
2022-09-21 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { previousValue };
|
2022-08-24 20:18:42 +00:00
|
|
|
},
|
2022-09-21 21:04:21 +00:00
|
|
|
|
|
|
|
onError: (err, variables, context) => {
|
|
|
|
if (context?.previousValue) {
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.availability.list.setData(undefined, context.previousValue);
|
2022-09-21 21:04:21 +00:00
|
|
|
}
|
2022-08-24 20:18:42 +00:00
|
|
|
if (err instanceof HttpError) {
|
|
|
|
const message = `${err.statusCode}: ${err.message}`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
|
|
|
},
|
2022-09-21 21:04:21 +00:00
|
|
|
onSettled: () => {
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.availability.list.invalidate();
|
2022-09-21 21:04:21 +00:00
|
|
|
},
|
|
|
|
onSuccess: () => {
|
|
|
|
showToast(t("schedule_deleted_successfully"), "success");
|
|
|
|
},
|
2022-08-24 20:18:42 +00:00
|
|
|
});
|
2022-09-12 10:15:30 +00:00
|
|
|
|
2022-12-07 19:35:55 +00:00
|
|
|
const updateMutation = trpc.viewer.availability.schedule.update.useMutation({
|
|
|
|
onSuccess: async ({ schedule }) => {
|
|
|
|
await utils.viewer.availability.list.invalidate();
|
|
|
|
showToast(
|
|
|
|
t("availability_updated_successfully", {
|
|
|
|
scheduleName: schedule.name,
|
|
|
|
}),
|
|
|
|
"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
|
2022-10-20 05:09:52 +00:00
|
|
|
|
|
|
|
const [animationParentRef] = useAutoAnimate<HTMLUListElement>();
|
2022-09-12 10:15:30 +00:00
|
|
|
|
2022-08-24 20:18:42 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{schedules.length === 0 ? (
|
|
|
|
<div className="flex justify-center">
|
|
|
|
<EmptyScreen
|
2023-04-12 15:26:31 +00:00
|
|
|
Icon={Clock}
|
2022-08-24 20:18:42 +00:00
|
|
|
headline={t("new_schedule_heading")}
|
|
|
|
description={t("new_schedule_description")}
|
|
|
|
buttonRaw={<NewScheduleButton />}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="border-subtle bg-default mb-16 overflow-hidden rounded-md border">
|
|
|
|
<ul className="divide-subtle divide-y" 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}
|
2023-04-05 15:42:22 +00:00
|
|
|
isDeletable={schedules.length !== 1}
|
2022-12-07 19:35:55 +00:00
|
|
|
updateDefault={updateMutation.mutate}
|
2023-04-05 15:42:22 +00:00
|
|
|
deleteFunction={deleteMutation.mutate}
|
2022-08-24 20:18:42 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const WithQuery = withQuery(trpc.viewer.availability.list);
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|