2023-12-06 11:12:57 +00:00
|
|
|
import React, { useEffect } from "react";
|
|
|
|
import Link from "next/link";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
import { XCircle } from "lucide-react";
|
|
|
|
// services
|
2024-03-19 14:38:35 +00:00
|
|
|
import { Button, Input } from "@plane/ui";
|
|
|
|
import { checkEmailValidity } from "@/helpers/string.helper";
|
|
|
|
import { AuthService } from "@/services/authentication.service";
|
2023-12-06 11:12:57 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
// ui
|
|
|
|
// helpers
|
|
|
|
// types
|
|
|
|
import { IPasswordSignInData } from "types/auth";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
email: string;
|
|
|
|
updateEmail: (email: string) => void;
|
|
|
|
handleSignInRedirection: () => Promise<void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
type TPasswordFormValues = {
|
|
|
|
email: string;
|
|
|
|
password: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: TPasswordFormValues = {
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
|
|
|
export const SelfHostedSignInForm: React.FC<Props> = (props) => {
|
|
|
|
const { email, updateEmail, handleSignInRedirection } = props;
|
|
|
|
// toast alert
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
// form info
|
|
|
|
const {
|
|
|
|
control,
|
|
|
|
formState: { dirtyFields, errors, isSubmitting },
|
|
|
|
handleSubmit,
|
|
|
|
setFocus,
|
|
|
|
} = useForm<TPasswordFormValues>({
|
|
|
|
defaultValues: {
|
|
|
|
...defaultValues,
|
|
|
|
email,
|
|
|
|
},
|
|
|
|
mode: "onChange",
|
|
|
|
reValidateMode: "onChange",
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleFormSubmit = async (formData: TPasswordFormValues) => {
|
|
|
|
const payload: IPasswordSignInData = {
|
|
|
|
email: formData.email,
|
|
|
|
password: formData.password,
|
|
|
|
};
|
|
|
|
|
|
|
|
updateEmail(formData.email);
|
|
|
|
|
|
|
|
await authService
|
|
|
|
.passwordSignIn(payload)
|
|
|
|
.then(async () => await handleSignInRedirection())
|
|
|
|
.catch((err) =>
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: err?.error ?? "Something went wrong. Please try again.",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setFocus("email");
|
|
|
|
}, [setFocus]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-12-10 10:18:10 +00:00
|
|
|
<h1 className="sm:text-2.5xl text-center text-2xl font-semibold text-onboarding-text-100">
|
2023-12-06 11:12:57 +00:00
|
|
|
Get on your flight deck
|
|
|
|
</h1>
|
2023-12-10 10:18:10 +00:00
|
|
|
<form onSubmit={handleSubmit(handleFormSubmit)} className="mx-auto mt-11 space-y-4 sm:w-96">
|
2023-12-06 11:12:57 +00:00
|
|
|
<div>
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="email"
|
|
|
|
rules={{
|
|
|
|
required: "Email is required",
|
|
|
|
validate: (value) => checkEmailValidity(value) || "Email is invalid",
|
|
|
|
}}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="relative flex items-center rounded-md bg-onboarding-background-200">
|
2023-12-06 11:12:57 +00:00
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
type="email"
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
hasError={Boolean(errors.email)}
|
2024-01-19 15:25:03 +00:00
|
|
|
placeholder="name@company.com"
|
2023-12-10 10:18:10 +00:00
|
|
|
className="h-[46px] w-full border border-onboarding-border-100 pr-12 placeholder:text-onboarding-text-400"
|
2023-12-06 11:12:57 +00:00
|
|
|
/>
|
|
|
|
{value.length > 0 && (
|
|
|
|
<XCircle
|
2023-12-10 10:18:10 +00:00
|
|
|
className="absolute right-3 h-5 w-5 stroke-custom-text-400 hover:cursor-pointer"
|
2023-12-06 11:12:57 +00:00
|
|
|
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-10 10:18:10 +00:00
|
|
|
className="h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 placeholder:text-onboarding-text-400"
|
2023-12-06 11:12:57 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Button type="submit" variant="primary" className="w-full" size="xl" loading={isSubmitting}>
|
2023-12-12 12:10:02 +00:00
|
|
|
Continue
|
2023-12-06 11:12:57 +00:00
|
|
|
</Button>
|
|
|
|
<p className="text-xs text-onboarding-text-200">
|
|
|
|
When you click the button above, you agree with our{" "}
|
|
|
|
<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>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|