2023-12-04 09:34:04 +00:00
|
|
|
import React, { useState } from "react";
|
2023-12-01 10:20:01 +00:00
|
|
|
import Link from "next/link";
|
2023-11-29 13:37:33 +00:00
|
|
|
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
|
2023-12-06 08:52:59 +00:00
|
|
|
import { IPasswordSignInData } from "types/auth";
|
2023-11-29 13:37:33 +00:00
|
|
|
// constants
|
|
|
|
import { ESignInSteps } from "components/account";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
email: string;
|
|
|
|
updateEmail: (email: string) => void;
|
|
|
|
handleStepChange: (step: ESignInSteps) => void;
|
|
|
|
handleSignInRedirection: () => Promise<void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
type TPasswordFormValues = {
|
|
|
|
email: string;
|
|
|
|
password: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: TPasswordFormValues = {
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
|
|
|
export const PasswordForm: React.FC<Props> = (props) => {
|
|
|
|
const { email, updateEmail, handleStepChange, handleSignInRedirection } = props;
|
2023-12-04 09:34:04 +00:00
|
|
|
// states
|
2023-12-06 08:52:59 +00:00
|
|
|
const [isSendingUniqueCode, setIsSendingUniqueCode] = useState(false);
|
2023-12-04 09:34:04 +00:00
|
|
|
const [isSendingResetPasswordLink, setIsSendingResetPasswordLink] = useState(false);
|
2023-11-29 13:37:33 +00:00
|
|
|
// toast alert
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
// form info
|
|
|
|
const {
|
|
|
|
control,
|
|
|
|
formState: { dirtyFields, errors, isSubmitting, isValid },
|
|
|
|
getValues,
|
|
|
|
handleSubmit,
|
|
|
|
setError,
|
|
|
|
} = useForm<TPasswordFormValues>({
|
|
|
|
defaultValues: {
|
|
|
|
...defaultValues,
|
|
|
|
email,
|
|
|
|
},
|
|
|
|
mode: "onChange",
|
|
|
|
reValidateMode: "onChange",
|
|
|
|
});
|
|
|
|
|
2023-12-06 08:52:59 +00:00
|
|
|
const handleFormSubmit = async (formData: TPasswordFormValues) => {
|
|
|
|
updateEmail(formData.email);
|
|
|
|
|
2023-11-29 13:37:33 +00:00
|
|
|
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.",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-12-06 08:52:59 +00:00
|
|
|
const handleForgotPassword = async () => {
|
|
|
|
const emailFormValue = getValues("email");
|
2023-11-29 13:37:33 +00:00
|
|
|
|
2023-12-06 08:52:59 +00:00
|
|
|
const isEmailValid = checkEmailValidity(emailFormValue);
|
|
|
|
|
|
|
|
if (!isEmailValid) {
|
|
|
|
setError("email", { message: "Email is invalid" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsSendingResetPasswordLink(true);
|
|
|
|
|
|
|
|
authService
|
|
|
|
.sendResetPasswordLink({ email: emailFormValue })
|
|
|
|
.then(() => handleStepChange(ESignInSteps.SET_PASSWORD_LINK))
|
2023-11-29 13:37:33 +00:00
|
|
|
.catch((err) =>
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: err?.error ?? "Something went wrong. Please try again.",
|
|
|
|
})
|
2023-12-06 08:52:59 +00:00
|
|
|
)
|
|
|
|
.finally(() => setIsSendingResetPasswordLink(false));
|
2023-11-29 13:37:33 +00:00
|
|
|
};
|
|
|
|
|
2023-12-06 08:52:59 +00:00
|
|
|
const handleSendUniqueCode = async () => {
|
2023-11-29 13:37:33 +00:00
|
|
|
const emailFormValue = getValues("email");
|
|
|
|
|
|
|
|
const isEmailValid = checkEmailValidity(emailFormValue);
|
|
|
|
|
|
|
|
if (!isEmailValid) {
|
|
|
|
setError("email", { message: "Email is invalid" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-06 08:52:59 +00:00
|
|
|
setIsSendingUniqueCode(true);
|
2023-12-04 09:34:04 +00:00
|
|
|
|
2023-12-06 08:52:59 +00:00
|
|
|
await authService
|
|
|
|
.generateUniqueCode({ email: emailFormValue })
|
|
|
|
.then(() => handleStepChange(ESignInSteps.USE_UNIQUE_CODE_FROM_PASSWORD))
|
2023-11-29 13:37:33 +00:00
|
|
|
.catch((err) =>
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: err?.error ?? "Something went wrong. Please try again.",
|
|
|
|
})
|
2023-12-04 09:34:04 +00:00
|
|
|
)
|
2023-12-06 08:52:59 +00:00
|
|
|
.finally(() => setIsSendingUniqueCode(false));
|
2023-11-29 13:37:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<h1 className="text-center text-2xl sm:text-2.5xl font-semibold text-onboarding-text-100">
|
|
|
|
Get on your flight deck
|
|
|
|
</h1>
|
|
|
|
<form onSubmit={handleSubmit(handleFormSubmit)} className="mt-11 sm:w-96 mx-auto space-y-4">
|
|
|
|
<div>
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="email"
|
|
|
|
rules={{
|
|
|
|
required: "Email is required",
|
|
|
|
validate: (value) => checkEmailValidity(value) || "Email is invalid",
|
|
|
|
}}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<div className="flex items-center relative rounded-md bg-onboarding-background-200">
|
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
type="email"
|
|
|
|
value={value}
|
2023-12-06 08:52:59 +00:00
|
|
|
onChange={onChange}
|
2023-11-29 13:37:33 +00:00
|
|
|
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 && (
|
|
|
|
<XCircle
|
|
|
|
className="h-5 w-5 absolute stroke-custom-text-400 hover:cursor-pointer right-3"
|
|
|
|
onClick={() => onChange("")}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="password"
|
|
|
|
rules={{
|
|
|
|
required: dirtyFields.email ? false : "Password is required",
|
|
|
|
}}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<Input
|
|
|
|
type="password"
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
hasError={Boolean(errors.password)}
|
|
|
|
placeholder="Enter password"
|
2023-12-06 08:52:59 +00:00
|
|
|
className="w-full h-[46px] placeholder:text-onboarding-text-400 border border-onboarding-border-100 pr-12 !bg-onboarding-background-200"
|
2023-11-29 13:37:33 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2023-12-01 10:20:01 +00:00
|
|
|
<div className="w-full text-right">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={handleForgotPassword}
|
2023-12-04 09:34:04 +00:00
|
|
|
className={`text-xs font-medium ${
|
|
|
|
isSendingResetPasswordLink ? "text-onboarding-text-300" : "text-custom-primary-100"
|
|
|
|
}`}
|
|
|
|
disabled={isSendingResetPasswordLink}
|
2023-12-01 10:20:01 +00:00
|
|
|
>
|
2023-12-06 08:52:59 +00:00
|
|
|
{isSendingResetPasswordLink ? "Sending link" : "Forgot your password?"}
|
2023-12-01 10:20:01 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
2023-11-29 13:37:33 +00:00
|
|
|
</div>
|
2023-12-06 08:52:59 +00:00
|
|
|
<div className="grid sm:grid-cols-2 gap-2.5">
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
onClick={handleSendUniqueCode}
|
|
|
|
variant="primary"
|
|
|
|
className="w-full"
|
|
|
|
size="xl"
|
|
|
|
loading={isSendingUniqueCode}
|
|
|
|
>
|
|
|
|
{isSendingUniqueCode ? "Sending code" : "Use unique code"}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
variant="outline-primary"
|
|
|
|
className="w-full"
|
|
|
|
size="xl"
|
|
|
|
disabled={!isValid}
|
|
|
|
loading={isSubmitting}
|
|
|
|
>
|
|
|
|
Go to workspace
|
|
|
|
</Button>
|
|
|
|
</div>
|
2023-12-01 10:20:01 +00:00
|
|
|
<p className="text-xs text-onboarding-text-200">
|
2023-12-06 08:52:59 +00:00
|
|
|
When you click <span className="text-custom-primary-100">Go to workspace</span> above, you agree with our{" "}
|
2023-12-01 10:20:01 +00:00
|
|
|
<Link href="https://plane.so/terms-and-conditions" target="_blank" rel="noopener noreferrer">
|
|
|
|
<span className="font-semibold underline">terms and conditions of service.</span>
|
|
|
|
</Link>
|
|
|
|
</p>
|
2023-11-29 13:37:33 +00:00
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|