From d08c3fdc716c2a4b13a6217ce864ebc2240fa032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20L=C3=B3pez?= Date: Thu, 21 Mar 2024 16:07:32 -0700 Subject: [PATCH] fix: revalidation replacing availability form values (#14114) --- .../components/AvailabilityEditSheet.tsx | 59 ++++++++++++------- .../availability/AvailabilitySettings.tsx | 35 ++++++----- .../WebAvailabilitySettingsWrapper.tsx | 35 ++++------- 3 files changed, 65 insertions(+), 64 deletions(-) diff --git a/packages/features/timezone-buddy/components/AvailabilityEditSheet.tsx b/packages/features/timezone-buddy/components/AvailabilityEditSheet.tsx index ee4a6ef3b3..573c8f5641 100644 --- a/packages/features/timezone-buddy/components/AvailabilityEditSheet.tsx +++ b/packages/features/timezone-buddy/components/AvailabilityEditSheet.tsx @@ -1,14 +1,16 @@ -import { useForm, useFieldArray } from "react-hook-form"; +import { useFieldArray, useForm } from "react-hook-form"; import dayjs from "@calcom/dayjs"; import { DateOverrideInputDialog, DateOverrideList } from "@calcom/features/schedules"; import Schedule from "@calcom/features/schedules/components/Schedule"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { HttpError } from "@calcom/lib/http-error"; +import type { RouterOutputs } from "@calcom/trpc/react"; import { trpc } from "@calcom/trpc/react"; import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery"; import type { Schedule as ScheduleType, TimeRange, WorkingHours } from "@calcom/types/schedule"; import { + Alert, Button, Form, Label, @@ -16,9 +18,8 @@ import { SheetContent, SheetHeader, SheetTitle, - TimezoneSelect, showToast, - Alert, + TimezoneSelect, } from "@calcom/ui"; import { Plus } from "@calcom/ui/components/icon"; @@ -82,6 +83,25 @@ const DateOverride = ({ workingHours, disabled }: { workingHours: WorkingHours[] }; export function AvailabilityEditSheet(props: Props) { + // This sheet will not be rendered without a selected user + const userId = props.selectedUser?.id as number; + const { data, isPending } = trpc.viewer.availability.schedule.getScheduleByUserId.useQuery({ + userId: userId, + }); + + // TODO: reimplement Skeletons for this page in here + if (isPending) return null; + + if (!data) return null; + + // We wait for the schedule to be loaded before rendering the form since `defaultValues` + // cannot be redeclared after first render. And using `values` will trigger a form reset + // when revalidating. + return ; +} + +type Data = RouterOutputs["viewer"]["availability"]["schedule"]["getScheduleByUserId"]; +export function AvailabilityEditSheetForm(props: Props & { data: Data; isPending: boolean }) { // This sheet will not be rendered without a selected user const userId = props.selectedUser?.id as number; const { t } = useLocale(); @@ -92,13 +112,11 @@ export function AvailabilityEditSheet(props: Props) { memberId: userId, }); - const { data, isPending } = trpc.viewer.availability.schedule.getScheduleByUserId.useQuery({ - userId: userId, - }); + const { data, isPending } = props; const updateMutation = trpc.viewer.availability.schedule.update.useMutation({ onSuccess: async () => { - utils.viewer.availability.listTeam.invalidate(); + await utils.viewer.availability.listTeam.invalidate(); showToast(t("success"), "success"); props.onOpenChange(false); }, @@ -111,10 +129,10 @@ export function AvailabilityEditSheet(props: Props) { }); const form = useForm({ - values: data && { + defaultValues: { ...data, - timeZone: data?.timeZone || Intl.DateTimeFormat().resolvedOptions().timeZone, - schedule: data?.availability || [], + timeZone: data.timeZone || Intl.DateTimeFormat().resolvedOptions().timeZone, + schedule: data.availability || [], }, }); @@ -128,12 +146,11 @@ export function AvailabilityEditSheet(props: Props) { handleSubmit={async ({ dateOverrides, ...values }) => { // Just blocking on a UI side -> Backend will also do the validation if (!hasEditPermission) return; - data && - updateMutation.mutate({ - scheduleId: data?.id, - dateOverrides: dateOverrides.flatMap((override) => override.ranges), - ...values, - }); + updateMutation.mutate({ + scheduleId: data.id, + dateOverrides: dateOverrides.flatMap((override) => override.ranges), + ...values, + }); }}>