lbry-desktop/ui/component/userEmailReturning/view.jsx

153 lines
4.9 KiB
React
Raw Permalink Normal View History

// @flow
import { SITE_NAME, SIMPLE_SITE } from 'config';
import * as PAGES from 'constants/pages';
import React, { useState } from 'react';
import { FormField, Form } from 'component/common/form';
import Button from 'component/button';
import { EMAIL_REGEX } from 'constants/email';
import { useHistory } from 'react-router-dom';
import UserEmailVerify from 'component/userEmailVerify';
import Card from 'component/common/card';
import Nag from 'component/common/nag';
import classnames from 'classnames';
import LoginGraphic from 'component/loginGraphic';
type Props = {
user: ?User,
errorMessage: ?string,
emailToVerify: ?string,
emailDoesNotExist: boolean,
doClearEmailEntry: () => void,
doUserSignIn: (string, ?string) => void,
2021-03-19 21:52:59 +01:00
doUserCheckIfEmailExists: (string) => void,
doSetWalletSyncPreference: (boolean) => void,
2020-09-04 17:02:30 +02:00
doSetClientSetting: (string, boolean, ?boolean) => void,
isPending: boolean,
};
function UserEmailReturning(props: Props) {
2020-04-20 19:13:41 +02:00
const {
user,
2020-04-20 19:13:41 +02:00
errorMessage,
doUserCheckIfEmailExists,
emailToVerify,
doClearEmailEntry,
emailDoesNotExist,
doSetWalletSyncPreference,
isPending,
2020-04-20 19:13:41 +02:00
} = props;
const { push, location } = useHistory();
const urlParams = new URLSearchParams(location.search);
const emailFromUrl = urlParams.get('email');
const emailExistsFromUrl = urlParams.get('email_exists');
const defaultEmail = emailFromUrl ? decodeURIComponent(emailFromUrl) : '';
const hasPasswordSet = user && user.password_set;
const [email, setEmail] = useState(defaultEmail);
2020-04-20 19:13:41 +02:00
const [syncEnabled, setSyncEnabled] = useState(true);
const valid = email.match(EMAIL_REGEX);
const showEmailVerification = emailToVerify || hasPasswordSet;
function handleSubmit() {
2020-04-20 19:13:41 +02:00
// @if TARGET='app'
doSetWalletSyncPreference(syncEnabled);
2020-04-20 19:13:41 +02:00
// @endif
doUserCheckIfEmailExists(email);
}
function handleChangeToSignIn() {
doClearEmailEntry();
let url = `/$/${PAGES.AUTH}`;
const urlParams = new URLSearchParams(location.search);
urlParams.delete('email_exists');
urlParams.delete('email');
if (email) {
urlParams.set('email', encodeURIComponent(email));
}
push(`${url}?${urlParams.toString()}`);
}
return (
<div
className={classnames('main__sign-in', {
'main__sign-up--graphic': SIMPLE_SITE && !showEmailVerification,
})}
>
{showEmailVerification ? (
<UserEmailVerify />
) : (
<Card
2020-08-24 19:59:37 +02:00
title={__('Log in to %SITE_NAME%', { SITE_NAME })}
actions={
<div>
<Form onSubmit={handleSubmit} className="section">
<FormField
autoFocus={!emailExistsFromUrl}
placeholder={__('yourstruly@example.com')}
type="email"
id="username"
autoComplete="on"
name="sign_in_email"
label={__('Email')}
value={email}
2021-03-19 21:52:59 +01:00
onChange={(e) => setEmail(e.target.value)}
/>
2020-04-20 19:13:41 +02:00
{/* @if TARGET='app' */}
<FormField
type="checkbox"
name="sync_checkbox"
label={
<React.Fragment>
{__('Backup your account and wallet data.')}{' '}
<Button button="link" href="https://lbry.com/faq/account-sync" label={__('Learn More')} />
</React.Fragment>
}
checked={syncEnabled}
onChange={() => setSyncEnabled(!syncEnabled)}
/>
{/* @endif */}
<div className="section__actions">
<Button
autoFocus={emailExistsFromUrl}
button="primary"
type="submit"
2020-08-24 19:59:37 +02:00
label={__('Log In')}
disabled={!email || !valid || isPending}
/>
2020-08-21 06:24:02 +02:00
<Button button="link" onClick={handleChangeToSignIn} label={__('Sign Up')} />
</div>
</Form>
</div>
}
nag={
2021-03-19 21:52:59 +01:00
<>
{!emailDoesNotExist && emailExistsFromUrl && (
2020-08-24 19:59:37 +02:00
<Nag type="helpful" relative message={__('That email is already in use. Did you mean to log in?')} />
)}
{emailDoesNotExist && (
<Nag
type="helpful"
relative
2020-08-21 06:24:02 +02:00
message={__("We can't find that email. Did you mean to sign up?")}
actionText={__('Sign Up')}
/>
)}
{!emailExistsFromUrl && !emailDoesNotExist && errorMessage && (
<Nag type="error" relative message={errorMessage} />
)}
2021-03-19 21:52:59 +01:00
</>
}
secondPane={SIMPLE_SITE && <LoginGraphic />}
/>
)}
</div>
);
}
export default UserEmailReturning;