/* eslint-disable jsx-a11y/no-autofocus */
import * as React from "react";
import * as Validations from "~/common/validations";
import * as System from "~/components/system";
import * as Strings from "~/common/strings";
import Field from "~/components/core/Field";
import { AnimateSharedLayout, motion } from "framer-motion";
import { useForm } from "~/common/hooks";
import { SignUpPopover, Verification, AuthCheckBox } from "~/components/core/Auth/components";
const useTwitterLinking = () => {
// NOTE(amine): can be either 'account' | 'email' | 'verificatiom'
const [scene, setScene] = React.useState("account");
const handlers = React.useMemo(
() => ({
goToEmailScene: () => setScene("email"),
goToVerificationScene: () => setScene("verification"),
}),
[]
);
return { ...handlers, scene };
};
const MotionLayout = ({ children, ...props }) => (
{children}
);
const handleValidation = async ({ username, password, acceptTerms }, errors) => {
if (!Validations.username(username)) errors.username = "Invalid username";
if (!Validations.legacyPassword(password)) errors.password = "Incorrect password";
if (!acceptTerms) errors.acceptTerms = "Must accept terms and conditions";
return errors;
};
export default function TwitterLinking({
linkAccount,
linkAccountWithVerification,
createVerification,
}) {
const { scene, goToVerificationScene, goToEmailScene } = useTwitterLinking();
const {
getFieldProps,
getFormProps,
values: { username },
isSubmitting,
} = useForm({
initialValues: { username: "", password: "", acceptTerms: false },
validate: handleValidation,
onSubmit: async ({ username, password }) => {
const response = await linkAccount({ username, password });
if (response.shouldMigrate) {
goToEmailScene();
}
},
});
const {
getFormProps: getEmailFormProps,
getFieldProps: getEmailFieldProps,
isSubmitting: isEmailFormSubmitting,
} = useForm({
initialValues: { email: "" },
validate: ({ email }, errors) => {
if (Strings.isEmpty(email)) {
errors.email = "Please provide an email";
} else if (!Validations.email(email)) {
errors.email = "Invalid email address";
}
return errors;
},
onSubmit: async ({ email }) => {
const response = await createVerification({ email });
if (!response) return;
goToVerificationScene();
},
});
if (scene === "verification") {
const handleVerification = async ({ pin }) => {
await linkAccountWithVerification({ pin });
};
return ;
}
if (scene === "email") {
return (
);
}
return (
);
}