Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
fix: Blank state wouldn't set 'schedule'; resulting toggle had no eff…
Browse files Browse the repository at this point in the history
…ect (calcom#11335)
  • Loading branch information
emrysal authored Sep 27, 2023
1 parent 8a413a9 commit ec4e2f5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
24 changes: 13 additions & 11 deletions apps/web/components/eventtype/EventAvailabilityTab.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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
Expand All @@ -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">
Expand All @@ -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}
Expand Down Expand Up @@ -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 (
<Controller
name="metadata.config.useHostSchedulesForTeamEvent"
Expand All @@ -285,9 +289,7 @@ const UseCommonScheduleSettingsToggle = ({ eventType }: { eventType: EventTypeSe
checked={!value}
onCheckedChange={(checked) => {
onChange(!checked);
if (checked) {
resetField("schedule");
} else {
if (!checked) {
setValue("schedule", null);
}
}}
Expand Down
9 changes: 8 additions & 1 deletion packages/core/getUserAvailability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down
9 changes: 8 additions & 1 deletion packages/prisma/selects/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ export const availabilityUserSelect = Prisma.validator<Prisma.UserSelect>()({
// Relationships
schedules: {
select: {
availability: true,
availability: {
select: {
date: true,
startTime: true,
endTime: true,
days: true,
},
},
timeZone: true,
id: true,
},
Expand Down
10 changes: 9 additions & 1 deletion packages/trpc/server/routers/viewer/slots/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down Expand Up @@ -192,6 +199,7 @@ export async function getEventType(
},
},
});

if (!eventType) {
return null;
}
Expand Down

0 comments on commit ec4e2f5

Please sign in to comment.