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
|
|
|
|
import { Input, SecondaryButton } 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-06-06 16:06:13 +00:00
|
|
|
{isResettingPassword ? (
|
|
|
|
<EmailResetPasswordForm setIsResettingPassword={setIsResettingPassword} />
|
|
|
|
) : (
|
|
|
|
<form className="mt-5 py-5 px-5" onSubmit={handleSubmit(onSubmit)}>
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
type="email"
|
|
|
|
name="email"
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: "Email ID is required",
|
|
|
|
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
|
|
|
|
) || "Email ID is not valid",
|
|
|
|
}}
|
|
|
|
error={errors.email}
|
2023-06-16 13:30:04 +00:00
|
|
|
placeholder="Enter your email ID"
|
2023-06-06 16:06:13 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5">
|
|
|
|
<Input
|
|
|
|
id="password"
|
|
|
|
type="password"
|
|
|
|
name="password"
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: "Password is required",
|
|
|
|
}}
|
|
|
|
error={errors.password}
|
|
|
|
placeholder="Enter your password"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="mt-2 flex items-center justify-between">
|
|
|
|
<div className="ml-auto text-sm">
|
2023-06-16 13:30:04 +00:00
|
|
|
{isSignUpPage ? (
|
|
|
|
<Link href="/">
|
|
|
|
<a className="font-medium text-brand-accent hover:text-brand-accent">
|
|
|
|
Already have an account? Sign in.
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => setIsResettingPassword(true)}
|
|
|
|
className="font-medium text-brand-accent hover:text-brand-accent"
|
|
|
|
>
|
|
|
|
Forgot your password?
|
|
|
|
</button>
|
|
|
|
)}
|
2023-06-06 16:06:13 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5">
|
|
|
|
<SecondaryButton
|
|
|
|
type="submit"
|
|
|
|
className="w-full text-center"
|
|
|
|
disabled={!isValid && isDirty}
|
|
|
|
loading={isSubmitting}
|
|
|
|
>
|
2023-06-16 13:30:04 +00:00
|
|
|
{isSignUpPage
|
|
|
|
? isSubmitting
|
|
|
|
? "Signing up..."
|
|
|
|
: "Sign Up"
|
|
|
|
: isSubmitting
|
|
|
|
? "Signing in..."
|
|
|
|
: "Sign In"}
|
2023-06-06 16:06:13 +00:00
|
|
|
</SecondaryButton>
|
2023-06-16 13:30:04 +00:00
|
|
|
{!isSignUpPage && (
|
|
|
|
<Link href="/sign-up">
|
|
|
|
<a className="block font-medium text-brand-accent hover:text-brand-accent text-sm mt-1">
|
|
|
|
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
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|