5d65a0f091
* fix: hide branding for teams * fix: remove unused organizationId and username fields from profiles select Addresses Cubic AI review feedback (confidence 9/10) to select only the profile fields that are actually used. The organizationId and username fields were fetched but never referenced in this function. Co-Authored-By: unknown <> * fix: unit tests * fix: add prisma named export to test mock Co-Authored-By: rajiv@cal.com <sahalrajiv6900@gmail.com> * Add tests: packages/features/profile/lib/hideBranding.test.ts Generated by Paragon from proposal for PR #27643 * chore: implement cubic feedback * fix: merge conflicts * fix: unit tests * fixup * refactor: implement DI pattern for event type service * fix: atoms build --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
81 lines
2.6 KiB
TypeScript
81 lines
2.6 KiB
TypeScript
import { useSession } from "next-auth/react";
|
|
import { useState } from "react";
|
|
|
|
import { useBookerStore } from "@calcom/features/bookings/Booker/store";
|
|
import { useDebounce } from "@calcom/lib/hooks/useDebounce";
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
import { trpc } from "@calcom/trpc/react";
|
|
import { showToast } from "@calcom/ui/components/toast";
|
|
|
|
export interface IUseVerifyEmailProps {
|
|
email: string;
|
|
onVerifyEmail?: () => void;
|
|
name?: string | { firstName: string; lastname?: string };
|
|
requiresBookerEmailVerification?: boolean;
|
|
eventTypeId?: number;
|
|
}
|
|
export type UseVerifyEmailReturnType = ReturnType<typeof useVerifyEmail>;
|
|
export const useVerifyEmail = ({
|
|
email,
|
|
name,
|
|
requiresBookerEmailVerification,
|
|
onVerifyEmail,
|
|
eventTypeId,
|
|
}: IUseVerifyEmailProps) => {
|
|
const [isEmailVerificationModalVisible, setEmailVerificationModalVisible] = useState(false);
|
|
const verifiedEmail = useBookerStore((state) => state.verifiedEmail);
|
|
const setVerifiedEmail = useBookerStore((state) => state.setVerifiedEmail);
|
|
const isRescheduling = useBookerStore((state) => Boolean(state.rescheduleUid && state.bookingData));
|
|
const debouncedEmail = useDebounce(email, 600);
|
|
const { data: session } = useSession();
|
|
|
|
const { t, i18n } = useLocale();
|
|
const sendEmailVerificationByCodeMutation = trpc.viewer.auth.sendVerifyEmailCode.useMutation({
|
|
onSuccess: () => {
|
|
setEmailVerificationModalVisible(true);
|
|
showToast(t("email_sent"), "success");
|
|
},
|
|
onError: () => {
|
|
showToast(t("email_not_sent"), "error");
|
|
},
|
|
});
|
|
|
|
const { data: isEmailVerificationRequired } =
|
|
trpc.viewer.public.checkIfUserEmailVerificationRequired.useQuery(
|
|
{
|
|
userSessionEmail: session?.user.email || "",
|
|
email: debouncedEmail,
|
|
},
|
|
{
|
|
enabled: !!debouncedEmail && !isRescheduling,
|
|
}
|
|
);
|
|
|
|
const handleVerifyEmail = () => {
|
|
onVerifyEmail?.();
|
|
|
|
sendEmailVerificationByCodeMutation.mutate({
|
|
email,
|
|
username: typeof name === "string" ? name : name?.firstName,
|
|
language: i18n.language || "en",
|
|
eventTypeId,
|
|
});
|
|
};
|
|
|
|
const isVerificationCodeSending = sendEmailVerificationByCodeMutation.isPending;
|
|
|
|
const renderConfirmNotVerifyEmailButtonCond =
|
|
isRescheduling ||
|
|
(!requiresBookerEmailVerification && !isEmailVerificationRequired) ||
|
|
(email && verifiedEmail && verifiedEmail === email);
|
|
|
|
return {
|
|
handleVerifyEmail,
|
|
isEmailVerificationModalVisible,
|
|
setEmailVerificationModalVisible,
|
|
setVerifiedEmail,
|
|
renderConfirmNotVerifyEmailButtonCond: Boolean(renderConfirmNotVerifyEmailButtonCond),
|
|
isVerificationCodeSending,
|
|
};
|
|
};
|