2023-10-04 13:25:29 +00:00
|
|
|
import React from "react";
|
2022-11-19 14:21:26 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
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>;
|
2023-10-04 13:25:29 +00:00
|
|
|
setIsResettingPassword: (value: boolean) => void;
|
2023-06-16 13:30:04 +00:00
|
|
|
};
|
|
|
|
|
2023-10-04 13:25:29 +00:00
|
|
|
export const EmailPasswordForm: React.FC<Props> = (props) => {
|
|
|
|
const { onSubmit, setIsResettingPassword } = props;
|
|
|
|
// form info
|
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-10-04 13:25:29 +00:00
|
|
|
<form
|
|
|
|
className="space-y-4 mt-10 w-full sm:w-[360px] mx-auto"
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
>
|
|
|
|
<div className="space-y-1">
|
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
type="email"
|
|
|
|
name="email"
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: "Email address 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 address is not valid",
|
|
|
|
}}
|
|
|
|
error={errors.email}
|
|
|
|
placeholder="Enter your email address..."
|
|
|
|
className="border-custom-border-300 h-[46px]"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="space-y-1">
|
|
|
|
<Input
|
|
|
|
id="password"
|
|
|
|
type="password"
|
|
|
|
name="password"
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: "Password is required",
|
|
|
|
}}
|
|
|
|
error={errors.password}
|
|
|
|
placeholder="Enter your password..."
|
|
|
|
className="border-custom-border-300 h-[46px]"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="text-right text-xs">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => setIsResettingPassword(true)}
|
|
|
|
className="text-custom-text-200 hover:text-custom-primary-100"
|
|
|
|
>
|
|
|
|
Forgot your password?
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<PrimaryButton
|
|
|
|
type="submit"
|
|
|
|
className="w-full text-center h-[46px]"
|
|
|
|
disabled={!isValid && isDirty}
|
|
|
|
loading={isSubmitting}
|
|
|
|
>
|
|
|
|
{isSubmitting ? "Signing in..." : "Sign in"}
|
|
|
|
</PrimaryButton>
|
|
|
|
</div>
|
|
|
|
</form>
|
2022-11-19 14:21:26 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|