2022-12-14 17:30:55 +00:00
|
|
|
import { GetServerSidePropsContext } from "next";
|
|
|
|
import { Controller, useFieldArray, useForm } from "react-hook-form";
|
2022-08-24 20:18:42 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
2022-12-14 17:30:55 +00:00
|
|
|
import { DateOverrideInputDialog, DateOverrideList } from "@calcom/features/schedules";
|
2022-09-15 05:49:59 +00:00
|
|
|
import Schedule from "@calcom/features/schedules/components/Schedule";
|
2023-01-10 15:39:29 +00:00
|
|
|
import Shell from "@calcom/features/shell/Shell";
|
2022-09-15 05:49:59 +00:00
|
|
|
import { availabilityAsString } from "@calcom/lib/availability";
|
2022-12-14 17:30:55 +00:00
|
|
|
import { yyyymmdd } from "@calcom/lib/date-fns";
|
2022-08-24 20:18:42 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { stringOrNumber } from "@calcom/prisma/zod-utils";
|
2022-09-15 05:49:59 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-09-20 10:56:52 +00:00
|
|
|
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
|
2022-12-14 17:30:55 +00:00
|
|
|
import type { Schedule as ScheduleType, TimeRange, WorkingHours } from "@calcom/types/schedule";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Form,
|
|
|
|
Icon,
|
|
|
|
Label,
|
|
|
|
showToast,
|
|
|
|
Skeleton,
|
|
|
|
SkeletonText,
|
|
|
|
Switch,
|
|
|
|
TimezoneSelect,
|
2022-12-14 17:30:55 +00:00
|
|
|
Tooltip,
|
2022-11-23 02:55:25 +00:00
|
|
|
VerticalDivider,
|
|
|
|
} from "@calcom/ui";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
import { HttpError } from "@lib/core/http/error";
|
|
|
|
|
2022-11-01 13:29:01 +00:00
|
|
|
import { SelectSkeletonLoader } from "@components/availability/SkeletonLoader";
|
2022-08-24 20:18:42 +00:00
|
|
|
import EditableHeading from "@components/ui/EditableHeading";
|
|
|
|
|
2022-12-14 17:30:55 +00:00
|
|
|
import { ssrInit } from "@server/lib/ssr";
|
2022-12-07 20:53:44 +00:00
|
|
|
|
2022-09-15 05:49:59 +00:00
|
|
|
const querySchema = z.object({
|
|
|
|
schedule: stringOrNumber,
|
|
|
|
});
|
|
|
|
|
|
|
|
type AvailabilityFormValues = {
|
|
|
|
name: string;
|
|
|
|
schedule: ScheduleType;
|
2022-12-14 17:30:55 +00:00
|
|
|
dateOverrides: { ranges: TimeRange[] }[];
|
2022-09-15 05:49:59 +00:00
|
|
|
timeZone: string;
|
|
|
|
isDefault: boolean;
|
|
|
|
};
|
|
|
|
|
2022-12-14 17:30:55 +00:00
|
|
|
const DateOverride = ({ workingHours }: { workingHours: WorkingHours[] }) => {
|
|
|
|
const { remove, append, update, fields } = useFieldArray<AvailabilityFormValues, "dateOverrides">({
|
|
|
|
name: "dateOverrides",
|
|
|
|
});
|
|
|
|
const { t } = useLocale();
|
|
|
|
return (
|
|
|
|
<div className="px-4 py-5 sm:p-6">
|
|
|
|
<h3 className="font-medium leading-6 text-gray-900">
|
|
|
|
{t("date_overrides")}{" "}
|
|
|
|
<Tooltip content={t("date_overrides_info")}>
|
|
|
|
<span className="inline-block">
|
|
|
|
<Icon.FiInfo />
|
|
|
|
</span>
|
|
|
|
</Tooltip>
|
|
|
|
</h3>
|
2023-01-12 16:57:43 +00:00
|
|
|
<p className="mb-4 text-sm text-gray-500 ltr:mr-4 rtl:ml-4">{t("date_overrides_subtitle")}</p>
|
2022-12-14 17:30:55 +00:00
|
|
|
<div className="mt-1 space-y-2">
|
|
|
|
<DateOverrideList
|
|
|
|
excludedDates={fields.map((field) => yyyymmdd(field.ranges[0].start))}
|
|
|
|
remove={remove}
|
|
|
|
update={update}
|
|
|
|
items={fields}
|
|
|
|
workingHours={workingHours}
|
|
|
|
/>
|
|
|
|
<DateOverrideInputDialog
|
|
|
|
workingHours={workingHours}
|
|
|
|
excludedDates={fields.map((field) => yyyymmdd(field.ranges[0].start))}
|
|
|
|
onChange={(ranges) => append({ ranges })}
|
|
|
|
Trigger={
|
2022-12-15 20:19:35 +00:00
|
|
|
<Button color="secondary" StartIcon={Icon.FiPlus} data-testid="add-override">
|
2022-12-14 17:30:55 +00:00
|
|
|
Add an override
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-15 05:49:59 +00:00
|
|
|
export default function Availability({ schedule }: { schedule: number }) {
|
|
|
|
const { t, i18n } = useLocale();
|
2022-08-24 20:18:42 +00:00
|
|
|
const utils = trpc.useContext();
|
2022-09-20 10:56:52 +00:00
|
|
|
const me = useMeQuery();
|
2022-11-01 13:59:44 +00:00
|
|
|
const { timeFormat } = me.data || { timeFormat: null };
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data, isLoading } = trpc.viewer.availability.schedule.get.useQuery({ scheduleId: schedule });
|
2022-12-14 17:30:55 +00:00
|
|
|
const { data: defaultValues } = trpc.viewer.availability.defaultValues.useQuery({ scheduleId: schedule });
|
|
|
|
const form = useForm<AvailabilityFormValues>({ defaultValues });
|
|
|
|
const { control } = form;
|
2022-11-10 23:40:01 +00:00
|
|
|
const updateMutation = trpc.viewer.availability.schedule.update.useMutation({
|
2022-11-03 20:58:52 +00:00
|
|
|
onSuccess: async ({ prevDefaultId, currentDefaultId, ...data }) => {
|
|
|
|
if (prevDefaultId && currentDefaultId) {
|
|
|
|
// check weather the default schedule has been changed by comparing previous default schedule id and current default schedule id.
|
|
|
|
if (prevDefaultId !== currentDefaultId) {
|
|
|
|
// if not equal, invalidate previous default schedule id and refetch previous default schedule id.
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.availability.schedule.get.invalidate({ scheduleId: prevDefaultId });
|
|
|
|
utils.viewer.availability.schedule.get.refetch({ scheduleId: prevDefaultId });
|
2022-11-03 20:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-14 17:30:55 +00:00
|
|
|
utils.viewer.availability.schedule.get.invalidate({ scheduleId: data.schedule.id });
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.availability.list.invalidate();
|
2022-08-24 20:18:42 +00:00
|
|
|
showToast(
|
|
|
|
t("availability_updated_successfully", {
|
2022-11-03 20:58:52 +00:00
|
|
|
scheduleName: data.schedule.name,
|
2022-08-24 20:18:42 +00:00
|
|
|
}),
|
|
|
|
"success"
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onError: (err) => {
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
const message = `${err.statusCode}: ${err.message}`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2022-09-15 05:49:59 +00:00
|
|
|
<Shell
|
|
|
|
backPath="/availability"
|
2022-12-07 20:53:44 +00:00
|
|
|
title={data?.schedule.name ? data.schedule.name + " | " + t("availability") : t("availability")}
|
2022-09-15 05:49:59 +00:00
|
|
|
heading={
|
2022-09-28 18:05:28 +00:00
|
|
|
<Controller
|
|
|
|
control={form.control}
|
|
|
|
name="name"
|
2022-12-15 20:19:35 +00:00
|
|
|
render={({ field }) => (
|
|
|
|
<EditableHeading isReady={!isLoading} {...field} data-testid="availablity-title" />
|
|
|
|
)}
|
2022-09-28 18:05:28 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
subtitle={
|
|
|
|
data ? (
|
2022-12-14 17:30:55 +00:00
|
|
|
data.schedule.availability
|
|
|
|
.filter((availability) => !!availability.days.length)
|
|
|
|
.map((availability) => (
|
|
|
|
<span key={availability.id}>
|
|
|
|
{availabilityAsString(availability, { locale: i18n.language, hour12: timeFormat === 12 })}
|
|
|
|
<br />
|
|
|
|
</span>
|
|
|
|
))
|
2022-09-28 18:05:28 +00:00
|
|
|
) : (
|
|
|
|
<SkeletonText className="h-4 w-48" />
|
|
|
|
)
|
2022-09-15 05:49:59 +00:00
|
|
|
}
|
|
|
|
CTA={
|
|
|
|
<div className="flex items-center justify-end">
|
|
|
|
<div className="flex items-center rounded-md px-2 sm:hover:bg-gray-100">
|
2022-09-28 18:05:28 +00:00
|
|
|
<Skeleton
|
|
|
|
as={Label}
|
|
|
|
htmlFor="hiddenSwitch"
|
|
|
|
className="mt-2 hidden cursor-pointer self-center pr-2 sm:inline">
|
2022-09-15 05:49:59 +00:00
|
|
|
{t("set_to_default")}
|
2022-09-28 18:05:28 +00:00
|
|
|
</Skeleton>
|
2022-09-15 05:49:59 +00:00
|
|
|
<Switch
|
|
|
|
id="hiddenSwitch"
|
2022-11-08 00:09:57 +00:00
|
|
|
disabled={isLoading || data?.isDefault}
|
2022-09-15 05:49:59 +00:00
|
|
|
checked={form.watch("isDefault")}
|
|
|
|
onCheckedChange={(e) => {
|
|
|
|
form.setValue("isDefault", e);
|
|
|
|
}}
|
2022-08-24 20:18:42 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2022-09-15 05:49:59 +00:00
|
|
|
|
|
|
|
<VerticalDivider />
|
|
|
|
|
|
|
|
<div className="border-l-2 border-gray-300" />
|
|
|
|
<Button className="ml-4 lg:ml-0" type="submit" form="availability-form">
|
|
|
|
{t("save")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
}>
|
2022-12-16 12:46:15 +00:00
|
|
|
<div className="w-full">
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
id="availability-form"
|
|
|
|
handleSubmit={async ({ dateOverrides, ...values }) => {
|
|
|
|
updateMutation.mutate({
|
|
|
|
scheduleId: schedule,
|
|
|
|
dateOverrides: dateOverrides.flatMap((override) => override.ranges),
|
|
|
|
...values,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
className="flex flex-col pb-16 sm:mx-0 xl:flex-row xl:space-x-6">
|
|
|
|
<div className="flex-1 divide-y divide-neutral-200 rounded-md border">
|
|
|
|
<div className=" py-5 sm:p-6">
|
|
|
|
<h3 className="mb-2 px-5 text-base font-medium leading-6 text-gray-900 sm:pl-0">
|
|
|
|
{t("change_start_end")}
|
|
|
|
</h3>
|
|
|
|
{typeof me.data?.weekStart === "string" && (
|
|
|
|
<Schedule
|
|
|
|
control={control}
|
|
|
|
name="schedule"
|
|
|
|
weekStart={
|
|
|
|
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"].indexOf(
|
|
|
|
me.data?.weekStart
|
|
|
|
) as 0 | 1 | 2 | 3 | 4 | 5 | 6
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
2022-08-24 20:18:42 +00:00
|
|
|
</div>
|
2022-12-16 12:46:15 +00:00
|
|
|
{data?.workingHours && <DateOverride workingHours={data.workingHours} />}
|
|
|
|
</div>
|
|
|
|
<div className="min-w-40 col-span-3 space-y-2 lg:col-span-1">
|
|
|
|
<div className="xl:max-w-80 mt-4 w-full pr-4 sm:p-0">
|
|
|
|
<div>
|
|
|
|
<label htmlFor="timeZone" className="block text-sm font-medium text-gray-700">
|
|
|
|
{t("timezone")}
|
|
|
|
</label>
|
|
|
|
<Controller
|
|
|
|
name="timeZone"
|
|
|
|
render={({ field: { onChange, value } }) =>
|
|
|
|
value ? (
|
|
|
|
<TimezoneSelect
|
|
|
|
value={value}
|
|
|
|
className="focus:border-brand mt-1 block w-72 rounded-md border-gray-300 text-sm"
|
|
|
|
onChange={(timezone) => onChange(timezone.value)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<SelectSkeletonLoader className="w-72" />
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<hr className="my-8" />
|
|
|
|
<div className="rounded-md">
|
|
|
|
<h3 className="text-sm font-medium text-gray-900">{t("something_doesnt_look_right")}</h3>
|
|
|
|
<div className="mt-3 flex">
|
|
|
|
<Button href="/availability/troubleshoot" color="secondary">
|
|
|
|
{t("launch_troubleshooter")}
|
|
|
|
</Button>
|
2022-09-15 05:49:59 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-12-16 12:46:15 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
2022-08-24 20:18:42 +00:00
|
|
|
</div>
|
2022-09-15 05:49:59 +00:00
|
|
|
</Shell>
|
2022-08-24 20:18:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-14 17:30:55 +00:00
|
|
|
export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
|
2022-08-24 20:18:42 +00:00
|
|
|
const params = querySchema.safeParse(ctx.params);
|
2022-12-14 17:30:55 +00:00
|
|
|
const ssr = await ssrInit(ctx);
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
if (!params.success) return { notFound: true };
|
|
|
|
|
2022-12-14 17:30:55 +00:00
|
|
|
const scheduleId = params.data.schedule;
|
|
|
|
await ssr.viewer.availability.schedule.get.fetch({ scheduleId });
|
|
|
|
await ssr.viewer.availability.defaultValues.fetch({ scheduleId });
|
2022-08-24 20:18:42 +00:00
|
|
|
return {
|
|
|
|
props: {
|
2022-12-14 17:30:55 +00:00
|
|
|
schedule: scheduleId,
|
|
|
|
trpcState: ssr.dehydrate(),
|
2022-08-24 20:18:42 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|