import React from "react"; import { Controller, useForm } from "react-hook-form"; import { XCircle } from "lucide-react"; // services import { AuthService } from "services/auth.service"; // hooks import useToast from "hooks/use-toast"; // ui import { Button, Input } from "@plane/ui"; // helpers import { checkEmailValidity } from "helpers/string.helper"; // types import { IEmailCheckData, IPasswordSignInData } from "types/auth"; // constants import { ESignInSteps } from "components/account"; type Props = { email: string; updateEmail: (email: string) => void; handleStepChange: (step: ESignInSteps) => void; handleSignInRedirection: () => Promise; }; type TPasswordFormValues = { email: string; password: string; }; const defaultValues: TPasswordFormValues = { email: "", password: "", }; const authService = new AuthService(); export const PasswordForm: React.FC = (props) => { const { email, updateEmail, handleStepChange, handleSignInRedirection } = props; // toast alert const { setToastAlert } = useToast(); // form info const { control, formState: { dirtyFields, errors, isSubmitting, isValid }, getValues, handleSubmit, reset, setError, } = useForm({ defaultValues: { ...defaultValues, email, }, mode: "onChange", reValidateMode: "onChange", }); const handlePasswordSignIn = async (formData: TPasswordFormValues) => { const payload: IPasswordSignInData = { email: formData.email, password: formData.password, }; await authService .passwordSignIn(payload) .then(async () => await handleSignInRedirection()) .catch((err) => setToastAlert({ type: "error", title: "Error!", message: err?.error ?? "Something went wrong. Please try again.", }) ); }; const handleEmailCheck = async (formData: TPasswordFormValues) => { const payload: IEmailCheckData = { email: formData.email, type: "password", }; await authService .emailCheck(payload) .then((res) => { if (res.is_password_autoset) handleStepChange(ESignInSteps.SET_PASSWORD_LINK); else reset({ email: formData.email, password: "", }); }) .catch((err) => setToastAlert({ type: "error", title: "Error!", message: err?.error ?? "Something went wrong. Please try again.", }) ); }; const handleFormSubmit = async (formData: TPasswordFormValues) => { if (dirtyFields.email) await handleEmailCheck(formData); else await handlePasswordSignIn(formData); }; const handleForgotPassword = async () => { const emailFormValue = getValues("email"); const isEmailValid = checkEmailValidity(emailFormValue); if (!isEmailValid) { setError("email", { message: "Email is invalid" }); return; } authService .sendResetPasswordLink({ email: emailFormValue }) .then(() => handleStepChange(ESignInSteps.SET_PASSWORD_LINK)) .catch((err) => setToastAlert({ type: "error", title: "Error!", message: err?.error ?? "Something went wrong. Please try again.", }) ); }; return ( <>

Get on your flight deck

checkEmailValidity(value) || "Email is invalid", }} render={({ field: { value, onChange } }) => (
{ updateEmail(e.target.value); onChange(e.target.value); }} onBlur={() => { if (dirtyFields.email) handleEmailCheck(getValues()); }} hasError={Boolean(errors.email)} placeholder="orville.wright@firstflight.com" className="w-full h-[46px] placeholder:text-onboarding-text-400 border border-onboarding-border-100 pr-12" /> {value.length > 0 && ( onChange("")} /> )}
)} />
( )} />
); };