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

59 lines
1.8 KiB
React
Raw Normal View History

2019-08-27 16:43:42 +02:00
// @flow
import React from 'react';
2019-09-26 18:07:11 +02:00
import { withRouter } from 'react-router';
import UserEmailReturning from 'component/userEmailReturning';
import UserSignInPassword from 'component/userSignInPassword';
import Spinner from 'component/spinner';
2019-08-27 16:43:42 +02:00
type Props = {
user: ?User,
history: { push: string => void },
2019-09-26 18:07:11 +02:00
location: { search: string },
userFetchPending: boolean,
doUserSignIn: string => void,
emailToVerify: ?string,
passwordExists: boolean,
2019-08-27 16:43:42 +02:00
};
2019-09-26 18:07:11 +02:00
function UserSignIn(props: Props) {
const { user, location, history, doUserSignIn, userFetchPending, emailToVerify, passwordExists } = props;
2019-09-26 18:07:11 +02:00
const { search } = location;
const urlParams = new URLSearchParams(search);
const [emailOnlyLogin, setEmailOnlyLogin] = React.useState(false);
2019-09-26 18:07:11 +02:00
const hasVerifiedEmail = user && user.has_verified_email;
const redirect = urlParams.get('redirect');
const showLoading = userFetchPending;
const showEmail = !passwordExists || emailOnlyLogin;
const showPassword = !showEmail && emailToVerify && passwordExists;
2019-09-26 18:07:11 +02:00
2019-09-27 20:56:15 +02:00
React.useEffect(() => {
if (hasVerifiedEmail || (!showEmail && !showPassword && !showLoading)) {
history.push(redirect || '/');
2019-09-27 20:56:15 +02:00
}
}, [showEmail, showPassword, showLoading, hasVerifiedEmail]);
React.useEffect(() => {
if (emailToVerify && emailOnlyLogin) {
doUserSignIn(emailToVerify);
}
}, [emailToVerify, emailOnlyLogin, doUserSignIn]);
return (
<section>
{(showEmail || showPassword) && (
<div>
{showEmail && <UserEmailReturning />}
{showPassword && <UserSignInPassword onHandleEmailOnly={() => setEmailOnlyLogin(true)} />}
</div>
)}
{!showEmail && !showPassword && showLoading && (
<div className="main--empty">
<Spinner delayed />
</div>
)}
</section>
);
2019-08-27 16:43:42 +02:00
}
2019-09-26 18:07:11 +02:00
export default withRouter(UserSignIn);