import Button from '@/Components/Button/Button' import { WebApplication } from '@/Application/WebApplication' import { PurchaseFlowPane } from '@/Controllers/PurchaseFlow/PurchaseFlowPane' import { observer } from 'mobx-react-lite' import { ChangeEventHandler, FunctionComponent, useCallback, useEffect, useRef, useState } from 'react' import FloatingLabelInput from '@/Components/Input/FloatingLabelInput' import { isEmailValid } from '@/Utils' import { BlueDotIcon, CircleIcon, DiamondIcon, CreateAccountIllustration } from '@standardnotes/icons' import { useCaptcha } from '@/Hooks/useCaptcha' import { AccountMenuPane } from '../../AccountMenu/AccountMenuPane' import { isErrorResponse } from '@standardnotes/snjs' import { c } from 'ttag' type Props = { application: WebApplication } const CreateAccount: FunctionComponent = ({ application }) => { const { setCurrentPane } = application.purchaseFlowController const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [confirmPassword, setConfirmPassword] = useState('') const [isCreatingAccount, setIsCreatingAccount] = useState(false) const [isEmailInvalid, setIsEmailInvalid] = useState(false) const [isPasswordNotMatching, setIsPasswordNotMatching] = useState(false) const [hvmToken, setHVMToken] = useState('') const [captchaURL, setCaptchaURL] = useState('') const register = useCallback(() => { setIsCreatingAccount(true) application .register(email, password, hvmToken) .then(() => { application.accountMenuController.closeAccountMenu() application.accountMenuController.setCurrentPane(AccountMenuPane.GeneralMenu) application.purchaseFlowController.closePurchaseFlow() }) .catch((err) => { console.error(err) application.alerts.alert(err as string).catch(console.error) }) .finally(() => { setIsCreatingAccount(false) }) }, [application, email, hvmToken, password]) const captchaIframe = useCaptcha(captchaURL, (token) => { setHVMToken(token) setCaptchaURL('') }) useEffect(() => { if (!hvmToken) { return } register() }, [hvmToken, register]) const checkIfCaptchaRequiredAndRegister = useCallback(() => { application .getCaptchaUrl() .then((response) => { if (isErrorResponse(response)) { throw new Error() } const { captchaUIUrl } = response.data if (captchaUIUrl) { setCaptchaURL(captchaUIUrl) } else { setCaptchaURL('') register() } }) .catch((error) => { console.error(error) setCaptchaURL('') register() }) }, [application, register]) const emailInputRef = useRef(null) const passwordInputRef = useRef(null) const confirmPasswordInputRef = useRef(null) useEffect(() => { if (emailInputRef.current) { emailInputRef.current?.focus() } }, []) const handleEmailChange: ChangeEventHandler = (e) => { setEmail(e.target.value) setIsEmailInvalid(false) } const handlePasswordChange: ChangeEventHandler = (e) => { setPassword(e.target.value) } const handleConfirmPasswordChange: ChangeEventHandler = (e) => { setConfirmPassword(e.target.value) setIsPasswordNotMatching(false) } const handleSignInInstead = () => { setCurrentPane(PurchaseFlowPane.SignIn) } const subscribeWithoutAccount = () => { void application.purchaseFlowController.openPurchaseWebpage() } const handleCreateAccount = async () => { if (!email) { emailInputRef?.current?.focus() return } if (!isEmailValid(email)) { setIsEmailInvalid(true) emailInputRef?.current?.focus() return } if (!password) { passwordInputRef?.current?.focus() return } if (!confirmPassword) { confirmPasswordInputRef?.current?.focus() return } if (password !== confirmPassword) { setConfirmPassword('') setIsPasswordNotMatching(true) confirmPasswordInputRef?.current?.focus() return } checkIfCaptchaRequiredAndRegister() } const CreateAccountForm = (
{isEmailInvalid ?
{c('Error').t`Please provide a valid email.`}
: null} {isPasswordNotMatching ? (
{c('Error').t`Passwords don't match. Please try again.`}
) : null}
) return (
{ // translator: Full sentence: "Create your free account"

{c('Title').t`Create your free account`}

} { // translator: Full sentence: "Create your free account to continue to Standard Notes."
{c('Info').t`to continue to Standard Notes.`}
} {captchaURL ? captchaIframe : CreateAccountForm}
{!application.isNativeIOS() && ( )}
) } export default observer(CreateAccount)