fix: hide duplicate phone field when attendee phone location selected (#23118)

* fix: hide duplicate phone field when attendee phone location selected

* Instead of hiding use autofill the value to all other phone fields

* add e2e test

* add not sync test and use changeHandler instead of useEffect

* address cubics comments

* adding the phone check back

* use zod schema instead of this type casting

* use Enum instead of hardcoded string for phone

* Fix e2e test

* Fix e2e tests

* Delete .retracify.html

---------

Co-authored-by: Eunjae Lee <[email protected]>
Co-authored-by: Volnei Munhoz <[email protected]>
Co-authored-by: Volnei Munhoz <[email protected]>
This commit is contained in:
authored and GitHub committed 2025-11-12 13:54:36 +00:00
1 parent 3e7a848769
commit a7cb71726a
4 files changed
+250 -8

No files matched your search

@@ -1,7 +1,10 @@
import { useMemo, useRef } from "react";
import { useFormContext } from "react-hook-form";
import { z } from "zod";
import type { LocationObject } from "@calcom/app-store/locations";
import { getOrganizerInputLocationTypes } from "@calcom/app-store/locations";
import { DefaultEventLocationTypeEnum } from "@calcom/app-store/locations";
import { useBookerStore } from "@calcom/features/bookings/Booker/store";
import type { GetBookingType } from "@calcom/features/bookings/lib/get-booking";
import getLocationOptionsForSelect from "@calcom/features/bookings/lib/getLocationOptionsForSelect";
@@ -13,7 +16,15 @@ import { useLocale } from "@calcom/lib/hooks/useLocale";
import { markdownToSafeHTML } from "@calcom/lib/markdownToSafeHTML";
import type { RouterOutputs } from "@calcom/trpc/react";
type TouchedFields = {
responses?: Record<string, boolean>;
};
type Fields = NonNullable<RouterOutputs["viewer"]["public"]["event"]>["bookingFields"];
const PhoneLocationSchema = z.object({
value: z.literal(DefaultEventLocationTypeEnum.Phone),
optionValue: z.string().optional(),
});
export const BookingFields = ({
fields,
locations,
@@ -32,11 +43,45 @@ export const BookingFields = ({
paymentCurrency?: string;
}) => {
const { t, i18n } = useLocale();
const { watch, setValue } = useFormContext();
const { watch, setValue, formState } = useFormContext();
const locationResponse = watch("responses.location");
const currentView = rescheduleUid ? "reschedule" : "";
const isInstantMeeting = useBookerStore((state) => state.isInstantMeeting);
// Identify all phone fields (except location field)
const otherPhoneFieldNames = useMemo(
() => fields.filter((f) => f.type === "phone" && f.name !== SystemField.Enum.location).map((f) => f.name),
[fields]
);
// Track last synced value to avoid redundant updates
const lastSyncedPhoneRef = useRef<string | null>(null);
// Event-driven sync function
const syncPhoneFields = (locationValue: unknown) => {
const parsed = PhoneLocationSchema.safeParse(locationValue);
if (!parsed.success) return;
const { optionValue } = parsed.data;
const phone = (optionValue ?? "").trim();
// Skip if empty or same as last sync (avoid redundant updates during typing)
if (!phone || phone === lastSyncedPhoneRef.current) return;
// Copy phone to other phone fields (only if user hasn't manually touched them)
otherPhoneFieldNames.forEach((name) => {
const targetTouched = !!(formState.touchedFields as TouchedFields)?.responses?.[name];
if (!targetTouched) {
setValue(`responses.${name}`, phone, {
shouldDirty: false,
shouldValidate: false,
});
}
});
lastSyncedPhoneRef.current = phone;
};
const getPriceFormattedLabel = (label: string, price: number) =>
`${label} (${Intl.NumberFormat(i18n.language, {
style: "currency",
@@ -205,6 +250,11 @@ export const BookingFields = ({
field={{ ...fieldWithPrice, hidden }}
readOnly={readOnly}
key={index}
{...(field.name === SystemField.Enum.location && {
onValueChange: ({ value }) => {
syncPhoneFields(value);
},
})}
/>
);
})}
@@ -25,7 +25,6 @@ const renderLabel = (field: Partial<RhfFormField>) => {
if (field.labelAsSafeHtml) {
return (
<span
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: markdownToSafeHTML(field.labelAsSafeHtml) }}
/>
);
@@ -66,10 +65,12 @@ export const FormBuilderField = ({
field,
readOnly,
className,
onValueChange,
}: {
field: RhfFormFields[number];
readOnly: boolean;
className: string;
onValueChange?: (args: { name: string; value: unknown; prevValue: unknown }) => void;
}) => {
const { t } = useLocale();
const { control, formState } = useFormContext();
@@ -87,15 +88,18 @@ export const FormBuilderField = ({
// Make it a variable
name={`responses.${field.name}`}
render={({ field: { value, onChange }, fieldState: { error } }) => {
const setAndNotify = (val: unknown) => {
onChange(val);
onValueChange?.({ name: field.name, value: val, prevValue: value });
};
return (
<div>
<ComponentForField
field={{ ...field, label, placeholder, hidden }}
value={value}
readOnly={readOnly || shouldBeDisabled}
setValue={(val: unknown) => {
onChange(val);
}}
setValue={setAndNotify}
noLabel={noLabel}
translatedDefaultLabel={translatedDefaultLabel}
/>