import { useState } from "react"; import { useTranslation } from "react-i18next"; import { Link } from "@tanstack/react-router"; import axios from "axios"; import { createNotification } from "@app/components/notifications"; import { useSendVerificationEmail } from "@app/hooks/api"; import { Button, Input } from "../v2"; interface DownloadBackupPDFStepProps { incrementStep: () => void; email: string; setEmail: (value: string) => void; } /** * This is the first step of the sign up process - users need to enter their email * @param {object} obj * @param {string} obj.email - email of a user signing up * @param {function} obj.setEmail - funciton that manages the state of the email variable * @param {function} obj.incrementStep - function to go to the next step of the signup flow * @returns */ export default function EnterEmailStep({ email, setEmail, incrementStep }: DownloadBackupPDFStepProps): JSX.Element { const { mutateAsync, isPending } = useSendVerificationEmail(); const [emailError, setEmailError] = useState(false); const { t } = useTranslation(); /** * Verifies if the entered email "looks" correct */ const emailCheck = async () => { let emailCheckBool = false; if (!email) { setEmailError(true); emailCheckBool = true; } else if (!email.includes("@") || !email.includes(".") || !/[a-z]/.test(email)) { setEmailError(true); emailCheckBool = true; } else { setEmailError(false); } // If everything is correct, go to the next step if (!emailCheckBool) { try { await mutateAsync({ email: email.toLowerCase() }); setEmail(email.toLowerCase()); incrementStep(); } catch (e) { if (axios.isAxiosError(e)) { const { message = "Something went wrong" } = e.response?.data as { message: string }; createNotification({ type: "error", text: message }); } } } }; return (
{t("signup.step1-start")}
Please enter a valid email.
)}