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';
|
2020-04-13 21:16:07 +02:00
|
|
|
import UserEmailReturning from 'component/userEmailReturning';
|
|
|
|
import UserSignInPassword from 'component/userSignInPassword';
|
2019-09-26 18:28:08 +02:00
|
|
|
import Spinner from 'component/spinner';
|
2019-08-27 16:43:42 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
user: ?User,
|
2020-04-13 21:16:07 +02:00
|
|
|
history: { push: string => void },
|
2019-09-26 18:07:11 +02:00
|
|
|
location: { search: string },
|
2020-04-13 21:16:07 +02:00
|
|
|
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) {
|
2020-04-13 21:16:07 +02:00
|
|
|
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);
|
2020-04-13 21:16:07 +02:00
|
|
|
const [emailOnlyLogin, setEmailOnlyLogin] = React.useState(false);
|
2019-09-26 18:07:11 +02:00
|
|
|
const hasVerifiedEmail = user && user.has_verified_email;
|
2020-04-13 21:16:07 +02:00
|
|
|
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(() => {
|
2020-04-13 21:16:07 +02:00
|
|
|
if (hasVerifiedEmail || (!showEmail && !showPassword && !showLoading)) {
|
|
|
|
history.push(redirect || '/');
|
2019-09-27 20:56:15 +02:00
|
|
|
}
|
2020-04-13 21:16:07 +02:00
|
|
|
}, [showEmail, showPassword, showLoading, hasVerifiedEmail]);
|
2020-02-27 20:00:23 +01:00
|
|
|
|
2020-04-13 21:16:07 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (emailToVerify && emailOnlyLogin) {
|
|
|
|
doUserSignIn(emailToVerify);
|
2019-09-26 18:28:08 +02:00
|
|
|
}
|
2020-04-13 21:16:07 +02:00
|
|
|
}, [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);
|