feat: add customReplyToEmail for EventTypeSettings atom (#23686)

* init: endpoint for fetching verified emails

* fix: enable custom reply to email in frontend

* init endpoint to add verified emails

* add verified emails option for platform in frontend

* fixup: move useGetVerifiedEmails hook to correct folder

* update atoms module

* fixup: teamId should be string

* add methond to fetch team member emails

* update logic to fetch and add emails

* fixup: append client id with email

* fixup: pass teamId for fetching verified emails

* fixup: simplify check for existing emails

* fix: cleanup comments

* fix: implement code rabbit feedback

* fix: add translations

* fixup: update transaltions

* fix: update logic for addVerifiedEmail

* add changesets

* fix: implement PR feedback
This commit is contained in:
Rajiv Sahal authored and GitHub committed 2025-09-09 16:52:28 +03:00
1 parent e86d6169ba
commit cfd1992733
16 files changed
+508 -47

No files matched your search

@@ -0,0 +1,106 @@
import { useState } from "react";
import { VerifyCodeDialog } from "@calcom/features/bookings/components/VerifyCodeDialog";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import classNames from "@calcom/ui/classNames";
import { Button } from "@calcom/ui/components/button";
import { TextField, Label } from "@calcom/ui/components/form";
import { Tooltip } from "@calcom/ui/components/tooltip";
import { useAddVerifiedEmail } from "../../../platform/atoms/event-types/hooks/useAddVerifiedEmail";
import { useGetVerifiedEmails } from "../../../platform/atoms/event-types/hooks/useGetVerifiedEmails";
import { useVerifyCode } from "../../../platform/atoms/hooks/useVerifyCode";
import { useVerifyEmail } from "../../../platform/atoms/hooks/useVerifyEmail";
type AddVerifiedEmailProps = {
username?: string;
showToast: (message: string, variant: "success" | "warning" | "error") => void;
};
const AddVerifiedEmail = ({ username, showToast }: AddVerifiedEmailProps) => {
const { t } = useLocale();
const [verifiedEmail, setVerifiedEmail] = useState("");
const [isEmailVerificationModalVisible, setIsEmailVerificationModalVisible] = useState(false);
const isValidEmail = (email: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
const { refetch: refetchVerifiedEmails } = useGetVerifiedEmails();
const { mutateAsync: addVerifiedEmail } = useAddVerifiedEmail({
onSuccess: () => {
refetchVerifiedEmails();
showToast(t("email_verified"), "success");
},
onError: () => {
showToast(t("something_went_wrong"), "error");
},
});
const verifyEmail = useVerifyEmail({
email: verifiedEmail,
name: username || t("there"),
requiresBookerEmailVerification: true,
});
const { handleVerifyEmail } = verifyEmail;
const verifyCode = useVerifyCode({
onSuccess: async () => {
setIsEmailVerificationModalVisible(false);
setVerifiedEmail("");
await addVerifiedEmail({
email: verifiedEmail,
});
},
});
return (
<div>
<div className="border-subtle border border-t-0 p-6">
<Label
className={classNames("text-emphasis mb-2 block text-sm font-medium leading-none")}
htmlFor="add-verified-emails">
{t("add_verified_emails")}
</Label>
<TextField
id="add-verified-emails"
containerClassName={classNames("w-full")}
value={verifiedEmail}
onChange={(e) => setVerifiedEmail(e.target.value)}
data-testid="add-verified-emails"
addOnSuffix={
<Tooltip content={t("add_verified_email")}>
<Button
type="button"
color="minimal"
size="sm"
StartIcon="arrow-right"
onClick={() => {
if (!!verifiedEmail && isValidEmail(verifiedEmail)) {
setIsEmailVerificationModalVisible(true);
handleVerifyEmail();
}
}}
/>
</Tooltip>
}
/>
</div>
<>
<VerifyCodeDialog
isOpenDialog={isEmailVerificationModalVisible}
setIsOpenDialog={setIsEmailVerificationModalVisible}
email={verifiedEmail}
isUserSessionRequiredToVerify={false}
verifyCodeWithSessionNotRequired={verifyCode.verifyCodeWithSessionNotRequired}
verifyCodeWithSessionRequired={verifyCode.verifyCodeWithSessionRequired}
error={verifyCode.error}
resetErrors={verifyCode.resetErrors}
isPending={verifyCode.isPending}
setIsPending={verifyCode.setIsPending}
/>
</>
</div>
);
};
export default AddVerifiedEmail;
@@ -62,6 +62,7 @@ import {
} from "@calcom/ui/components/form";
import { Icon } from "@calcom/ui/components/icon";
import AddVerifiedEmail from "../../AddVerifiedEmail";
import type { CustomEventTypeModalClassNames } from "./CustomEventTypeModal";
import CustomEventTypeModal from "./CustomEventTypeModal";
import type { EmailNotificationToggleCustomClassNames } from "./DisableAllEmailsSetting";
@@ -508,7 +509,7 @@ export const EventAdvancedTab = ({
"allowReschedulingCancelledBookings"
);
const { isLocked, ...eventNameLocked } = shouldLockDisableProps("eventName");
const { isLocked: _isLocked, ...eventNameLocked } = shouldLockDisableProps("eventName");
if (isManagedEventType) {
multiplePrivateLinksLocked.disabled = true;
@@ -1134,52 +1135,53 @@ export const EventAdvancedTab = ({
/>
)}
/>
{!isPlatform && (
<>
<Controller
name="customReplyToEmail"
render={({ field: { value, onChange } }) => (
<>
<SettingsToggle
labelClassName={classNames("text-sm", customClassNames?.customReplyToEmail?.label)}
toggleSwitchAtTheEnd={true}
switchContainerClassName={classNames(
"border-subtle rounded-lg border py-6 px-4 sm:px-6",
!!value && "rounded-b-none",
customClassNames?.customReplyToEmail?.container
)}
descriptionClassName={customClassNames?.customReplyToEmail?.description}
childrenClassName={classNames("lg:ml-0", customClassNames?.customReplyToEmail?.children)}
title={t("custom_reply_to_email_title")}
{...customReplyToEmailLocked}
data-testid="custom-reply-to-email"
description={t("custom_reply_to_email_description")}
checked={!!customReplyToEmail}
onCheckedChange={(e) => {
onChange(
e
? customReplyToEmail || eventType.customReplyToEmail || verifiedEmails?.[0] || null
: null
);
}}>
<div className="border-subtle rounded-b-lg border border-t-0 p-6">
<SelectField
className="w-full"
label={t("custom_reply_to_email_title")}
required={!!customReplyToEmail}
placeholder={t("select_verified_email")}
data-testid="custom-reply-to-email-input"
value={value ? { label: value, value } : undefined}
onChange={(option) => onChange(option?.value || null)}
options={verifiedEmails?.map((email) => ({ label: email, value: email })) || []}
/>
</div>
</SettingsToggle>
</>
)}
/>
</>
)}
<>
<Controller
name="customReplyToEmail"
render={({ field: { value, onChange } }) => (
<>
<SettingsToggle
labelClassName={classNames("text-sm", customClassNames?.customReplyToEmail?.label)}
toggleSwitchAtTheEnd={true}
switchContainerClassName={classNames(
"border-subtle rounded-lg border py-6 px-4 sm:px-6",
!!value && "rounded-b-none",
customClassNames?.customReplyToEmail?.container
)}
descriptionClassName={customClassNames?.customReplyToEmail?.description}
childrenClassName={classNames("lg:ml-0", customClassNames?.customReplyToEmail?.children)}
title={t("custom_reply_to_email_title")}
{...customReplyToEmailLocked}
data-testid="custom-reply-to-email"
description={t("custom_reply_to_email_description")}
checked={!!customReplyToEmail}
onCheckedChange={(e) => {
onChange(
e
? customReplyToEmail || eventType.customReplyToEmail || verifiedEmails?.[0] || null
: null
);
}}>
{isPlatform && (
<AddVerifiedEmail username={eventType.users[0]?.name || "there"} showToast={showToast} />
)}
<div className="border-subtle rounded-b-lg border border-t-0 p-6">
<SelectField
className="w-full"
label={t("custom_reply_to_email_title")}
required={!!customReplyToEmail}
placeholder={t("select_verified_email")}
data-testid="custom-reply-to-email-input"
value={value ? { label: value, value } : undefined}
onChange={(option) => onChange(option?.value || null)}
options={verifiedEmails?.map((email) => ({ label: email, value: email })) || []}
/>
</div>
</SettingsToggle>
</>
)}
/>
</>
<Controller
name="eventTypeColor"
render={() => (
@@ -0,0 +1,64 @@
import { useMutation } from "@tanstack/react-query";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiErrorResponse, ApiResponse } from "@calcom/platform-types";
import { useAtomsContext } from "../../hooks/useAtomsContext";
import { appendClientIdToEmail } from "../../lib/appendClientIdToEmail";
import http from "../../lib/http";
interface IUseAddVerifiedEmail {
onSuccess?: (res: ApiResponse) => void;
onError?: (err: ApiErrorResponse | Error) => void;
}
export const useAddVerifiedEmail = (
{ onSuccess, onError }: IUseAddVerifiedEmail = {
onSuccess: () => {
return;
},
onError: () => {
return;
},
}
) => {
const { clientId } = useAtomsContext();
const verifiedEmailEntry = useMutation<
ApiResponse<{
status: string;
data: {
emailVerified: boolean;
};
}>,
unknown,
{
email: string;
}
>({
mutationFn: (data) => {
const { email } = data;
const emailToSend = appendClientIdToEmail(email, clientId);
return http
.post(`/atoms/emails/verified-emails`, {
email: emailToSend,
})
.then((res) => {
return res.data;
});
},
onSuccess: (data) => {
if (data.status === SUCCESS_STATUS) {
onSuccess?.(data);
} else {
onError?.(data);
}
},
onError: (err) => {
onError?.(err as ApiErrorResponse);
},
});
return verifiedEmailEntry;
};
@@ -0,0 +1,27 @@
import { useQuery } from "@tanstack/react-query";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiResponse, ApiSuccessResponse } from "@calcom/platform-types";
import { useAtomsContext } from "../../hooks/useAtomsContext";
import http from "../../lib/http";
export const QUERY_KEY = "use-get-verified-emails";
export const useGetVerifiedEmails = (teamId?: number) => {
const pathname = `/atoms/emails/verified-emails${!!teamId ? `?teamId=${teamId}` : ""}`;
const { isInit, accessToken } = useAtomsContext();
return useQuery({
queryKey: [QUERY_KEY, teamId],
queryFn: () => {
return http?.get<ApiResponse<string[]>>(pathname).then((res) => {
if (res.data.status === SUCCESS_STATUS) {
return (res.data as ApiSuccessResponse<string[]>).data;
}
throw new Error(res.data.error.message);
});
},
enabled: isInit && !!accessToken,
staleTime: 5000,
});
};
@@ -2,14 +2,18 @@ import type { EventAdvancedBaseProps } from "@calcom/features/eventtypes/compone
import { EventAdvancedTab } from "@calcom/features/eventtypes/components/tabs/advanced/EventAdvancedTab";
import { useConnectedCalendars } from "../../hooks/useConnectedCalendars";
import { useGetVerifiedEmails } from "../hooks/useGetVerifiedEmails";
const EventAdvancedPlatformWrapper = (props: EventAdvancedBaseProps) => {
const { isPending, data: connectedCalendarsQuery, error } = useConnectedCalendars({});
const { data: verifiedEmails } = useGetVerifiedEmails(props.team?.id);
return (
<EventAdvancedTab
{...props}
calendarsQuery={{ data: connectedCalendarsQuery, isPending, error }}
showBookerLayoutSelector={false}
verifiedEmails={verifiedEmails}
/>
);
};
@@ -0,0 +1,4 @@
export function appendClientIdToEmail(email: string, clientId: string): string {
const [localPart, domain] = email.split("@");
return `${localPart}+${clientId}@${domain}`;
}