2020-04-13 21:16:07 +02:00
|
|
|
// @flow
|
2020-07-10 23:04:36 +02:00
|
|
|
import { SETTINGS } from 'lbry-redux';
|
2020-04-13 21:16:07 +02:00
|
|
|
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';
|
|
|
|
|
|
|
|
type Props = {
|
2020-04-20 22:53:40 +02:00
|
|
|
user: ?User,
|
2020-04-13 21:16:07 +02:00
|
|
|
errorMessage: ?string,
|
|
|
|
emailToVerify: ?string,
|
|
|
|
emailDoesNotExist: boolean,
|
|
|
|
doClearEmailEntry: () => void,
|
|
|
|
doUserSignIn: (string, ?string) => void,
|
|
|
|
doUserCheckIfEmailExists: string => void,
|
2020-04-20 19:13:41 +02:00
|
|
|
doSetClientSetting: (string, boolean) => void,
|
2020-07-09 18:50:11 +02:00
|
|
|
isPending: boolean,
|
2020-04-13 21:16:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function UserEmailReturning(props: Props) {
|
2020-04-20 19:13:41 +02:00
|
|
|
const {
|
2020-04-20 22:53:40 +02:00
|
|
|
user,
|
2020-04-20 19:13:41 +02:00
|
|
|
errorMessage,
|
|
|
|
doUserCheckIfEmailExists,
|
|
|
|
emailToVerify,
|
|
|
|
doClearEmailEntry,
|
|
|
|
emailDoesNotExist,
|
|
|
|
doSetClientSetting,
|
2020-07-09 18:50:11 +02:00
|
|
|
isPending,
|
2020-04-20 19:13:41 +02:00
|
|
|
} = props;
|
2020-04-13 21:16:07 +02:00
|
|
|
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) : '';
|
2020-04-20 22:53:40 +02:00
|
|
|
const hasPasswordSet = user && user.password_set;
|
|
|
|
|
2020-04-13 21:16:07 +02:00
|
|
|
const [email, setEmail] = useState(defaultEmail);
|
2020-04-20 19:13:41 +02:00
|
|
|
const [syncEnabled, setSyncEnabled] = useState(true);
|
|
|
|
|
2020-04-13 21:16:07 +02:00
|
|
|
const valid = email.match(EMAIL_REGEX);
|
2020-04-20 22:53:40 +02:00
|
|
|
const showEmailVerification = emailToVerify || hasPasswordSet;
|
2020-04-13 21:16:07 +02:00
|
|
|
|
|
|
|
function handleSubmit() {
|
2020-04-20 19:13:41 +02:00
|
|
|
// @if TARGET='app'
|
|
|
|
doSetClientSetting(SETTINGS.ENABLE_SYNC, syncEnabled);
|
|
|
|
// @endif
|
2020-04-13 21:16:07 +02:00
|
|
|
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="main__sign-in">
|
|
|
|
{showEmailVerification ? (
|
|
|
|
<UserEmailVerify />
|
|
|
|
) : (
|
|
|
|
<Card
|
|
|
|
title={__('Sign In to lbry.tv')}
|
|
|
|
actions={
|
|
|
|
<div>
|
|
|
|
<Form onSubmit={handleSubmit} className="section">
|
|
|
|
<FormField
|
|
|
|
autoFocus={!emailExistsFromUrl}
|
2020-07-03 01:31:46 +02:00
|
|
|
placeholder={__('yourstruly@example.com')}
|
2020-04-13 21:16:07 +02:00
|
|
|
type="email"
|
|
|
|
id="username"
|
|
|
|
autoComplete="on"
|
|
|
|
name="sign_in_email"
|
|
|
|
label={__('Email')}
|
|
|
|
value={email}
|
|
|
|
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 */}
|
|
|
|
|
2020-04-13 21:16:07 +02:00
|
|
|
<div className="section__actions">
|
|
|
|
<Button
|
|
|
|
autoFocus={emailExistsFromUrl}
|
|
|
|
button="primary"
|
|
|
|
type="submit"
|
|
|
|
label={__('Sign In')}
|
2020-07-09 18:50:11 +02:00
|
|
|
disabled={!email || !valid || isPending}
|
2020-04-13 21:16:07 +02:00
|
|
|
/>
|
2020-08-21 06:24:02 +02:00
|
|
|
<Button button="link" onClick={handleChangeToSignIn} label={__('Sign Up')} />
|
2020-04-13 21:16:07 +02:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
nag={
|
|
|
|
<React.Fragment>
|
|
|
|
{!emailDoesNotExist && emailExistsFromUrl && (
|
|
|
|
<Nag type="helpful" relative message={__('That email is already in use. Did you mean to sign 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')}
|
2020-04-13 21:16:07 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{!emailExistsFromUrl && !emailDoesNotExist && errorMessage && (
|
|
|
|
<Nag type="error" relative message={errorMessage} />
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default UserEmailReturning;
|