refactor: availabilitySettings atoms web wrapper and translations (#14167)
* refactor: availabilitySettings atoms web wrapper and translations * fixup! refactor: availabilitySettings atoms web wrapper and translations
This commit is contained in:
1 parent
0fc79f93de
commit
e19e829ff7
5 files changed
+131
-211
No files matched your search
@@ -1,124 +1,9 @@
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useForm } from "react-hook-form";
|
||||
|
||||
import { withErrorFromUnknown } from "@calcom/lib/getClientErrorFromUnknown";
|
||||
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { AvailabilitySettings } from "@calcom/platform-atoms";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
|
||||
import type { Schedule as ScheduleType, TimeRange } from "@calcom/types/schedule";
|
||||
import { showToast } from "@calcom/ui";
|
||||
|
||||
import PageWrapper from "@components/PageWrapper";
|
||||
|
||||
type AvailabilityFormValues = {
|
||||
name: string;
|
||||
schedule: ScheduleType;
|
||||
dateOverrides: { ranges: TimeRange[] }[];
|
||||
timeZone: string;
|
||||
isDefault: boolean;
|
||||
};
|
||||
|
||||
export default function Availability() {
|
||||
const searchParams = useCompatSearchParams();
|
||||
const { t } = useLocale();
|
||||
const router = useRouter();
|
||||
const utils = trpc.useContext();
|
||||
const me = useMeQuery();
|
||||
const scheduleId = searchParams?.get("schedule") ? Number(searchParams.get("schedule")) : -1;
|
||||
const fromEventType = searchParams?.get("fromEventType");
|
||||
const { timeFormat } = me.data || { timeFormat: null };
|
||||
const { data: schedule, isPending } = trpc.viewer.availability.schedule.get.useQuery(
|
||||
{ scheduleId },
|
||||
{
|
||||
enabled: !!scheduleId,
|
||||
}
|
||||
);
|
||||
|
||||
const form = useForm<AvailabilityFormValues>({
|
||||
values: schedule && {
|
||||
...schedule,
|
||||
schedule: schedule?.availability || [],
|
||||
},
|
||||
});
|
||||
const updateMutation = trpc.viewer.availability.schedule.update.useMutation({
|
||||
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.
|
||||
utils.viewer.availability.schedule.get.invalidate({ scheduleId: prevDefaultId });
|
||||
utils.viewer.availability.schedule.get.refetch({ scheduleId: prevDefaultId });
|
||||
}
|
||||
}
|
||||
utils.viewer.availability.schedule.get.invalidate({ scheduleId: data.schedule.id });
|
||||
utils.viewer.availability.list.invalidate();
|
||||
showToast(
|
||||
t("availability_updated_successfully", {
|
||||
scheduleName: data.schedule.name,
|
||||
}),
|
||||
"success"
|
||||
);
|
||||
},
|
||||
onError: (err) => {
|
||||
if (err instanceof HttpError) {
|
||||
const message = `${err.statusCode}: ${err.message}`;
|
||||
showToast(message, "error");
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const deleteMutation = trpc.viewer.availability.schedule.delete.useMutation({
|
||||
onError: withErrorFromUnknown((err) => {
|
||||
showToast(err.message, "error");
|
||||
}),
|
||||
onSettled: () => {
|
||||
utils.viewer.availability.list.invalidate();
|
||||
},
|
||||
onSuccess: () => {
|
||||
showToast(t("schedule_deleted_successfully"), "success");
|
||||
router.push("/availability");
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<AvailabilitySettings
|
||||
schedule={
|
||||
schedule
|
||||
? {
|
||||
name: schedule.name,
|
||||
id: schedule.id,
|
||||
isLastSchedule: schedule.isLastSchedule,
|
||||
isDefault: schedule.isDefault,
|
||||
workingHours: schedule.workingHours,
|
||||
dateOverrides: schedule.dateOverrides,
|
||||
timeZone: schedule.timeZone,
|
||||
availability: schedule.availability || [],
|
||||
schedule: schedule.schedule || [],
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
isDeleting={deleteMutation.isPending}
|
||||
isLoading={isPending}
|
||||
isSaving={updateMutation.isPending}
|
||||
timeFormat={timeFormat}
|
||||
weekStart={me.data?.weekStart || "Sunday"}
|
||||
backPath={fromEventType ? true : "/availability"}
|
||||
handleDelete={() => {
|
||||
scheduleId && deleteMutation.mutate({ scheduleId });
|
||||
}}
|
||||
handleSubmit={async ({ dateOverrides, ...values }) => {
|
||||
scheduleId &&
|
||||
updateMutation.mutate({
|
||||
scheduleId,
|
||||
dateOverrides: dateOverrides.flatMap((override) => override.ranges),
|
||||
...values,
|
||||
});
|
||||
}}
|
||||
/>
|
||||
);
|
||||
return <AvailabilitySettings />;
|
||||
}
|
||||
|
||||
Availability.PageWrapper = PageWrapper;
|
||||
Reference in new issue
Block a user