Scheduling infrastructure for absolutely everyone.
fix: Blank state wouldn't set 'schedule'; resulting toggle had no effect (#11335)
Alex van Andelec4e2f599693
diff --git a/apps/web/components/eventtype/EventAvailabilityTab.tsx b/apps/web/components/eventtype/EventAvailabilityTab.tsxindex 48e72eb349c..07ed8a9515e 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<FormValues>();+ const { watch, setValue, getValues } = useFormContext<FormValues>();const watchSchedule = watch("schedule");- const formMethods = useFormContext<FormValues>();const [options, setOptions] = useState<AvailabilityOption[]>([]);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 (<div className="space-y-4">@@ -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<FormValues>();+ const { setValue } = useFormContext<FormValues>();return (<Controllername="metadata.config.useHostSchedulesForTeamEvent"@@ -285,9 +289,7 @@ const UseCommonScheduleSettingsToggle = ({ eventType }: { eventType: EventTypeSechecked={!value}onCheckedChange={(checked) => {onChange(!checked);- if (checked) {- resetField("schedule");- } else {+ if (!checked) {setValue("schedule", null);}}}diff --git a/packages/core/getUserAvailability.ts b/packages/core/getUserAvailability.tsindex c683a3aa041..07ea15a0fee 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.tsindex 9c62e70275c..dffa09a2985 100644--- a/packages/prisma/selects/user.ts+++ b/packages/prisma/selects/user.ts@@ -13,7 +13,14 @@ export const availabilityUserSelect = Prisma.validator<Prisma.UserSelect>()({// Relationshipsschedules: {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.tsindex 38062a6bc9a..ac62bb4cd1a 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;}