2023-06-06 16:06:13 +00:00
|
|
|
import React, { useState } from "react";
|
2023-03-17 05:10:38 +00:00
|
|
|
|
2023-06-16 13:30:04 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import Link from "next/link";
|
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
// react hook form
|
|
|
|
import { useForm } from "react-hook-form";
|
2023-06-06 16:06:13 +00:00
|
|
|
// components
|
|
|
|
import { EmailResetPasswordForm } from "components/account";
|
2023-03-17 05:10:38 +00:00
|
|
|
// ui
|
2023-07-17 07:30:44 +00:00
|
|
|
import { Input, PrimaryButton } from "components/ui";
|
2022-11-19 14:21:26 +00:00
|
|
|
// types
|
2023-01-26 18:12:20 +00:00
|
|
|
type EmailPasswordFormValues = {
|
2022-11-19 14:21:26 +00:00
|
|
|
email: string;
|
|
|
|
password?: string;
|
|
|
|
medium?: string;
|
|
|
|
};
|
|
|
|
|
2023-06-16 13:30:04 +00:00
|
|
|
type Props = {
|
|
|
|
onSubmit: (formData: EmailPasswordFormValues) => Promise<void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const EmailPasswordForm: React.FC<Props> = ({ onSubmit }) => {
|
2023-06-06 16:06:13 +00:00
|
|
|
const [isResettingPassword, setIsResettingPassword] = useState(false);
|
|
|
|
|
2023-06-16 13:30:04 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const isSignUpPage = router.pathname === "/sign-up";
|
2023-06-06 16:06:13 +00:00
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
2023-01-26 18:12:20 +00:00
|
|
|
formState: { errors, isSubmitting, isValid, isDirty },
|
|
|
|
} = useForm<EmailPasswordFormValues>({
|
2022-11-19 14:21:26 +00:00
|
|
|
defaultValues: {
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
medium: "email",
|
|
|
|
},
|
|
|
|
mode: "onChange",
|
|
|
|
reValidateMode: "onChange",
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-07-17 07:30:44 +00:00
|
|
|
<h1 className="text-center text-2xl sm:text-2.5xl font-semibold text-custom-text-100">
|
|
|
|
{isResettingPassword
|
|
|
|
? "Reset your password"
|
|
|
|
: isSignUpPage
|
|
|
|
? "Sign up on Plane"
|
|
|
|
: "Sign in to Plane"}
|
|
|
|
</h1>
|
2023-06-06 16:06:13 +00:00
|
|
|
{isResettingPassword ? (
|
|
|
|
<EmailResetPasswordForm setIsResettingPassword={setIsResettingPassword} />
|
|
|
|
) : (
|
2023-07-17 07:30:44 +00:00
|
|
|
<form
|
|
|
|
className="space-y-4 mt-10 w-full sm:w-[360px] mx-auto"
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
>
|
|
|
|
<div className="space-y-1">
|
2023-06-06 16:06:13 +00:00
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
type="email"
|
|
|
|
name="email"
|
|
|
|
register={register}
|
|
|
|
validations={{
|
2023-07-17 07:30:44 +00:00
|
|
|
required: "Email address is required",
|
2023-06-06 16:06:13 +00:00
|
|
|
validate: (value) =>
|
|
|
|
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
|
|
|
|
value
|
2023-07-17 07:30:44 +00:00
|
|
|
) || "Email address is not valid",
|
2023-06-06 16:06:13 +00:00
|
|
|
}}
|
|
|
|
error={errors.email}
|
2023-07-17 07:30:44 +00:00
|
|
|
placeholder="Enter your email address..."
|
2023-07-18 09:30:06 +00:00
|
|
|
className="border-custom-border-300 h-[46px]"
|
2023-06-06 16:06:13 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2023-07-17 07:30:44 +00:00
|
|
|
<div className="space-y-1">
|
2023-06-06 16:06:13 +00:00
|
|
|
<Input
|
|
|
|
id="password"
|
|
|
|
type="password"
|
|
|
|
name="password"
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: "Password is required",
|
|
|
|
}}
|
|
|
|
error={errors.password}
|
2023-07-17 07:30:44 +00:00
|
|
|
placeholder="Enter your password..."
|
2023-07-18 09:30:06 +00:00
|
|
|
className="border-custom-border-300 h-[46px]"
|
2023-06-06 16:06:13 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2023-07-17 07:30:44 +00:00
|
|
|
<div className="text-right text-xs">
|
|
|
|
{isSignUpPage ? (
|
|
|
|
<Link href="/">
|
|
|
|
<a className="text-custom-text-200 hover:text-custom-primary-100">
|
|
|
|
Already have an account? Sign in.
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => setIsResettingPassword(true)}
|
|
|
|
className="text-custom-text-200 hover:text-custom-primary-100"
|
|
|
|
>
|
|
|
|
Forgot your password?
|
|
|
|
</button>
|
|
|
|
)}
|
2023-06-06 16:06:13 +00:00
|
|
|
</div>
|
2023-07-17 07:30:44 +00:00
|
|
|
<div>
|
|
|
|
<PrimaryButton
|
2023-06-06 16:06:13 +00:00
|
|
|
type="submit"
|
2023-07-18 09:30:06 +00:00
|
|
|
className="w-full text-center h-[46px]"
|
2023-06-06 16:06:13 +00:00
|
|
|
disabled={!isValid && isDirty}
|
|
|
|
loading={isSubmitting}
|
|
|
|
>
|
2023-06-16 13:30:04 +00:00
|
|
|
{isSignUpPage
|
|
|
|
? isSubmitting
|
|
|
|
? "Signing up..."
|
2023-07-17 07:30:44 +00:00
|
|
|
: "Sign up"
|
2023-06-16 13:30:04 +00:00
|
|
|
: isSubmitting
|
|
|
|
? "Signing in..."
|
2023-07-17 07:30:44 +00:00
|
|
|
: "Sign in"}
|
|
|
|
</PrimaryButton>
|
2023-06-16 13:30:04 +00:00
|
|
|
{!isSignUpPage && (
|
|
|
|
<Link href="/sign-up">
|
2023-07-17 07:30:44 +00:00
|
|
|
<a className="block text-custom-text-200 hover:text-custom-primary-100 text-xs mt-4">
|
2023-06-16 13:30:04 +00:00
|
|
|
Don{"'"}t have an account? Sign up.
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
)}
|
2022-11-19 14:21:26 +00:00
|
|
|
</div>
|
2023-06-06 16:06:13 +00:00
|
|
|
</form>
|
|
|
|
)}
|
2022-11-19 14:21:26 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|