2022-10-20 05:09:52 +00:00
|
|
|
import { useAutoAnimate } from "@formkit/auto-animate/react";
|
2022-12-07 20:53:44 +00:00
|
|
|
import { GetServerSidePropsContext } from "next";
|
2022-09-12 10:15:30 +00:00
|
|
|
|
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";
|
2022-11-10 23:40:01 +00:00
|
|
|
import { RouterOutputs, trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { EmptyScreen, Icon, Shell, showToast } from "@calcom/ui";
|
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-12-07 20:53:44 +00:00
|
|
|
import { ssrInit } from "@server/lib/ssr";
|
|
|
|
|
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
|
|
|
|
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}
|
2022-12-07 19:35:55 +00:00
|
|
|
updateDefault={updateMutation.mutate}
|
2022-08-24 20:18:42 +00:00
|
|
|
deleteFunction={deleteMutation.mutate}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</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>
|
|
|
|
);
|
|
|
|
}
|
2022-12-07 20:53:44 +00:00
|
|
|
|
|
|
|
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
|
|
|
|
const ssr = await ssrInit(context);
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
trpcState: ssr.dehydrate(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|