// @flow import React from 'react'; import { withRouter } from 'react-router'; import UserEmailReturning from 'component/userEmailReturning'; import UserSignInPassword from 'component/userSignInPassword'; import Spinner from 'component/spinner'; type Props = { user: ?User, history: { push: string => void }, location: { search: string }, userFetchPending: boolean, doUserSignIn: string => void, emailToVerify: ?string, passwordExists: boolean, }; function UserSignIn(props: Props) { const { user, location, history, doUserSignIn, userFetchPending, emailToVerify, passwordExists } = props; const { search } = location; const urlParams = new URLSearchParams(search); const [emailOnlyLogin, setEmailOnlyLogin] = React.useState(false); const hasVerifiedEmail = user && user.has_verified_email; const redirect = urlParams.get('redirect'); const showLoading = userFetchPending; const showEmail = !passwordExists || emailOnlyLogin; const showPassword = !showEmail && emailToVerify && passwordExists; React.useEffect(() => { if (hasVerifiedEmail || (!showEmail && !showPassword && !showLoading)) { history.push(redirect || '/'); } }, [showEmail, showPassword, showLoading, hasVerifiedEmail]); React.useEffect(() => { if (emailToVerify && emailOnlyLogin) { doUserSignIn(emailToVerify); } }, [emailToVerify, emailOnlyLogin, doUserSignIn]); return (
{(showEmail || showPassword) && (
{showEmail && } {showPassword && setEmailOnlyLogin(true)} />}
)} {!showEmail && !showPassword && showLoading && (
)}
); } export default withRouter(UserSignIn);