From ec4e2f5996937267620333d7f5c42eb025bcd91f Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Wed, 27 Sep 2023 10:26:55 +0100 Subject: [PATCH] fix: Blank state wouldn't set 'schedule'; resulting toggle had no effect (#11335) --- .../eventtype/EventAvailabilityTab.tsx | 24 ++++++++++--------- packages/core/getUserAvailability.ts | 9 ++++++- packages/prisma/selects/user.ts | 9 ++++++- .../trpc/server/routers/viewer/slots/util.ts | 10 +++++++- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/apps/web/components/eventtype/EventAvailabilityTab.tsx b/apps/web/components/eventtype/EventAvailabilityTab.tsx index 48e72eb349cdb5..07ed8a9515e7bc 100644 --- a/apps/web/components/eventtype/EventAvailabilityTab.tsx +++ b/apps/web/components/eventtype/EventAvailabilityTab.tsx @@ -1,5 +1,5 @@ import type { EventTypeSetup, FormValues } from "pages/event-types/[type]"; -import { useState, memo } from "react"; +import { useState, memo, useEffect } from "react"; import { Controller, useFormContext } from "react-hook-form"; import type { OptionProps, SingleValueProps } from "react-select"; import { components } from "react-select"; @@ -164,9 +164,8 @@ const EventTypeSchedule = ({ eventType }: { eventType: EventTypeSetup }) => { t("locked_fields_admin_description"), t("locked_fields_member_description") ); - const { watch } = useFormContext(); + const { watch, setValue, getValues } = useFormContext(); const watchSchedule = watch("schedule"); - const formMethods = useFormContext(); const [options, setOptions] = useState([]); const { isLoading } = trpc.viewer.availability.list.useQuery(undefined, { @@ -214,7 +213,7 @@ const EventTypeSchedule = ({ eventType }: { eventType: EventTypeSetup }) => { setOptions(options); - const scheduleId = formMethods.getValues("schedule"); + const scheduleId = getValues("schedule"); const value = options.find((option) => scheduleId ? option.value === scheduleId @@ -223,11 +222,16 @@ const EventTypeSchedule = ({ eventType }: { eventType: EventTypeSetup }) => { : option.value === schedules.find((schedule) => schedule.isDefault)?.id ); - formMethods.setValue("availability", value); + setValue("availability", value); }, }); - const availabilityValue = formMethods.watch("availability"); + const availabilityValue = watch("availability"); + + useEffect(() => { + if (!availabilityValue?.value) return; + setValue("schedule", availabilityValue.value); + }, [availabilityValue, setValue]); return (
@@ -248,7 +252,7 @@ const EventTypeSchedule = ({ eventType }: { eventType: EventTypeSetup }) => { isSearchable={false} onChange={(selected) => { field.onChange(selected?.value || null); - if (selected?.value) formMethods.setValue("availability", selected); + if (selected?.value) setValue("availability", selected); }} className="block w-full min-w-0 flex-1 rounded-sm text-sm" value={availabilityValue} @@ -276,7 +280,7 @@ const EventTypeSchedule = ({ eventType }: { eventType: EventTypeSetup }) => { const UseCommonScheduleSettingsToggle = ({ eventType }: { eventType: EventTypeSetup }) => { const { t } = useLocale(); - const { resetField, setValue } = useFormContext(); + const { setValue } = useFormContext(); return ( { onChange(!checked); - if (checked) { - resetField("schedule"); - } else { + if (!checked) { setValue("schedule", null); } }} diff --git a/packages/core/getUserAvailability.ts b/packages/core/getUserAvailability.ts index c683a3aa0410a5..07ea15a0fee3d5 100644 --- a/packages/core/getUserAvailability.ts +++ b/packages/core/getUserAvailability.ts @@ -52,7 +52,14 @@ const getEventType = async (id: number) => { metadata: true, schedule: { select: { - availability: true, + availability: { + select: { + days: true, + date: true, + startTime: true, + endTime: true, + }, + }, timeZone: true, }, }, diff --git a/packages/prisma/selects/user.ts b/packages/prisma/selects/user.ts index 9c62e70275cf4e..dffa09a29859bc 100644 --- a/packages/prisma/selects/user.ts +++ b/packages/prisma/selects/user.ts @@ -13,7 +13,14 @@ export const availabilityUserSelect = Prisma.validator()({ // Relationships schedules: { select: { - availability: true, + availability: { + select: { + date: true, + startTime: true, + endTime: true, + days: true, + }, + }, timeZone: true, id: true, }, diff --git a/packages/trpc/server/routers/viewer/slots/util.ts b/packages/trpc/server/routers/viewer/slots/util.ts index 38062a6bc9a60e..ac62bb4cd1a827 100644 --- a/packages/trpc/server/routers/viewer/slots/util.ts +++ b/packages/trpc/server/routers/viewer/slots/util.ts @@ -161,7 +161,14 @@ export async function getEventType( metadata: true, schedule: { select: { - availability: true, + availability: { + select: { + date: true, + startTime: true, + endTime: true, + days: true, + }, + }, timeZone: true, }, }, @@ -192,6 +199,7 @@ export async function getEventType( }, }, }); + if (!eventType) { return null; }