2024-01-19 15:25:03 +00:00
|
|
|
import React from "react";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
2023-11-29 13:37:33 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
import { XCircle } from "lucide-react";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { IEmailCheckData } from "@plane/types";
|
2023-11-29 13:37:33 +00:00
|
|
|
// services
|
2024-03-06 08:48:41 +00:00
|
|
|
import { Button, Input, TOAST_TYPE, setToast } from "@plane/ui";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { checkEmailValidity } from "@/helpers/string.helper";
|
|
|
|
import { AuthService } from "@/services/auth.service";
|
|
|
|
// ui
|
2023-11-29 13:37:33 +00:00
|
|
|
// helpers
|
|
|
|
// types
|
|
|
|
|
|
|
|
type Props = {
|
2024-01-19 15:25:03 +00:00
|
|
|
onSubmit: (isPasswordAutoset: boolean) => void;
|
2023-11-29 13:37:33 +00:00
|
|
|
updateEmail: (email: string) => void;
|
|
|
|
};
|
|
|
|
|
2023-12-06 08:52:59 +00:00
|
|
|
type TEmailFormValues = {
|
2023-11-29 13:37:33 +00:00
|
|
|
email: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
2024-01-19 15:25:03 +00:00
|
|
|
export const SignInEmailForm: React.FC<Props> = observer((props) => {
|
|
|
|
const { onSubmit, updateEmail } = props;
|
2024-01-04 10:57:17 +00:00
|
|
|
// hooks
|
2023-11-29 13:37:33 +00:00
|
|
|
const {
|
|
|
|
control,
|
2023-12-06 08:52:59 +00:00
|
|
|
formState: { errors, isSubmitting, isValid },
|
2023-11-29 13:37:33 +00:00
|
|
|
handleSubmit,
|
2023-12-06 08:52:59 +00:00
|
|
|
} = useForm<TEmailFormValues>({
|
2023-11-29 13:37:33 +00:00
|
|
|
defaultValues: {
|
|
|
|
email: "",
|
|
|
|
},
|
|
|
|
mode: "onChange",
|
|
|
|
reValidateMode: "onChange",
|
|
|
|
});
|
|
|
|
|
2023-12-06 08:52:59 +00:00
|
|
|
const handleFormSubmit = async (data: TEmailFormValues) => {
|
2023-11-29 13:37:33 +00:00
|
|
|
const payload: IEmailCheckData = {
|
2023-12-06 08:52:59 +00:00
|
|
|
email: data.email,
|
2023-11-29 13:37:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// update the global email state
|
2023-12-06 08:52:59 +00:00
|
|
|
updateEmail(data.email);
|
2023-11-29 13:37:33 +00:00
|
|
|
|
|
|
|
await authService
|
|
|
|
.emailCheck(payload)
|
2024-01-19 15:25:03 +00:00
|
|
|
.then((res) => onSubmit(res.is_password_autoset))
|
2023-11-29 13:37:33 +00:00
|
|
|
.catch((err) =>
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2023-11-29 13:37:33 +00:00
|
|
|
title: "Error!",
|
|
|
|
message: err?.error ?? "Something went wrong. Please try again.",
|
|
|
|
})
|
2023-12-06 08:52:59 +00:00
|
|
|
);
|
2023-11-29 13:37:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-12-10 10:18:10 +00:00
|
|
|
<h1 className="sm:text-2.5xl text-center text-2xl font-medium text-onboarding-text-100">
|
2024-01-19 15:25:03 +00:00
|
|
|
Welcome back, let{"'"}s get you on board
|
2023-11-29 13:37:33 +00:00
|
|
|
</h1>
|
2023-12-10 10:18:10 +00:00
|
|
|
<p className="mt-2.5 text-center text-sm text-onboarding-text-200">
|
2024-01-19 15:25:03 +00:00
|
|
|
Get back to your issues, projects and workspaces.
|
2023-11-29 13:37:33 +00:00
|
|
|
</p>
|
|
|
|
|
2023-12-10 10:18:10 +00:00
|
|
|
<form onSubmit={handleSubmit(handleFormSubmit)} className="mx-auto mt-8 space-y-4 sm:w-96">
|
2023-11-29 13:37:33 +00:00
|
|
|
<div className="space-y-1">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="email"
|
|
|
|
rules={{
|
|
|
|
required: "Email is required",
|
|
|
|
validate: (value) => checkEmailValidity(value) || "Email is invalid",
|
|
|
|
}}
|
2024-01-19 15:25:03 +00:00
|
|
|
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-11-29 13:37:33 +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"
|
2024-01-19 15:25:03 +00:00
|
|
|
autoFocus
|
2023-11-29 13:37:33 +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-11-29 13:37:33 +00:00
|
|
|
onClick={() => onChange("")}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-12-06 08:52:59 +00:00
|
|
|
<Button type="submit" variant="primary" className="w-full" size="xl" disabled={!isValid} loading={isSubmitting}>
|
|
|
|
Continue
|
|
|
|
</Button>
|
2023-11-29 13:37:33 +00:00
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
);
|
2024-01-04 10:57:17 +00:00
|
|
|
});
|