File size: 6,244 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
import { FormLabel } from '@automattic/components';
import { Spinner, Button } from '@wordpress/components';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { useRef, useState, useEffect } from 'react';
import { connect } from 'react-redux';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormTextInput from 'calypso/components/forms/form-text-input';
import LoggedOutForm from 'calypso/components/logged-out-form';
import Notice from 'calypso/components/notice';
import { sendEmailLogin } from 'calypso/state/auth/actions';
import { getCurrentUser } from 'calypso/state/current-user/selectors';
import { hideMagicLoginRequestNotice } from 'calypso/state/login/magic-login/actions';
import { getLastCheckedUsernameOrEmail } from 'calypso/state/login/selectors';
import getCurrentQueryArguments from 'calypso/state/selectors/get-current-query-arguments';
import getInitialQueryArguments from 'calypso/state/selectors/get-initial-query-arguments';
import getMagicLoginRequestEmailError from 'calypso/state/selectors/get-magic-login-request-email-error';
import isFetchingMagicLoginEmail from 'calypso/state/selectors/is-fetching-magic-login-email';
import { useLoginContext } from '../login-context';
import VerifyLoginCode from './verify-login-code';
const RequestLoginCode = ( {
currentUser,
emailRequested,
isFetching,
redirectTo,
requestError,
userEmail,
flow,
onReady,
onPublicTokenReceived,
createAccountForNewUser,
sendEmailLogin: sendEmail,
hideMagicLoginRequestNotice: hideRequestNotice,
translate,
shouldShowLoadingEllipsis,
publicToken,
tosComponent,
} ) => {
const [ usernameOrEmail, setUsernameOrEmail ] = useState( userEmail || '' );
const usernameOrEmailRef = useRef( null );
const { setHeaders } = useLoginContext();
useEffect( () => {
if ( onReady ) {
onReady();
}
}, [ onReady, setHeaders, translate ] );
useEffect( () => {
setHeaders( {
heading: translate( 'Email me a login code' ),
subHeading: translate(
"We'll send you an email with a code that will log you in right away."
),
} );
}, [] );
const onUsernameOrEmailFieldChange = ( event ) => {
setUsernameOrEmail( event.target.value );
if ( requestError ) {
hideRequestNotice();
}
};
const onNoticeDismiss = () => {
hideRequestNotice();
};
const handleSendEmail = ( email = usernameOrEmail ) => {
if ( ! email.length ) {
return;
}
sendEmail( email, {
redirectTo,
requestLoginEmailFormFlow: true,
createAccount: createAccountForNewUser,
tokenType: 'code', // Request code instead of link
...( flow ? { flow } : {} ),
onSuccess: ( response ) => {
if ( response && response.public_token ) {
if ( onPublicTokenReceived ) {
onPublicTokenReceived( response.public_token );
}
}
},
} );
};
const onSubmit = ( event ) => {
event.preventDefault();
handleSendEmail();
};
if ( shouldShowLoadingEllipsis ) {
return <Spinner className="magic-login__loading-spinner--jetpack" />;
}
// Show verification code form if email has been requested and we have a public token
if ( emailRequested && publicToken ) {
return (
<VerifyLoginCode
publicToken={ publicToken }
usernameOrEmail={ usernameOrEmail }
onResendEmail={ onSubmit }
/>
);
}
const submitEnabled =
usernameOrEmail.length && ! isFetching && ! emailRequested && ! requestError;
const errorText =
typeof requestError?.message === 'string' && requestError?.message.length
? requestError?.message
: translate( 'Unable to complete request' );
return (
<div className="magic-login__form">
<LoggedOutForm className="magic-login__form-form" onSubmit={ onSubmit }>
{ currentUser && currentUser.username && (
<Notice
showDismiss={ false }
className="magic-login__form-header-notice"
status="is-info"
theme="light"
text={ translate( 'You are already logged in as user: %(user)s', {
args: {
user: currentUser.username,
},
} ) }
></Notice>
) }
<FormLabel htmlFor="usernameOrEmail">
{ translate( 'Email Address or Username' ) }
</FormLabel>
<FormFieldset className="magic-login__email-fields">
<FormTextInput
autoCapitalize="off"
autoComplete="username"
className="magic-login__request-login-email-form-fields"
disabled={ isFetching || emailRequested }
id="usernameOrEmail"
name="usernameOrEmail"
value={ usernameOrEmail }
onChange={ onUsernameOrEmailFieldChange }
placeholder={ translate( 'Email or Username' ) }
ref={ usernameOrEmailRef }
/>
{ tosComponent }
{ requestError && (
<Notice
onDismissClick={ onNoticeDismiss }
className="magic-login__request-login-email-form-notice"
status="is-error"
text={ errorText }
/>
) }
<div className="magic-login__form-action">
<Button
variant="primary"
disabled={ ! submitEnabled && ! isFetching }
isBusy={ isFetching }
type="submit"
__next40pxDefaultSize
>
{ isFetching ? translate( 'Sending code…' ) : translate( 'Send Code' ) }
</Button>
</div>
</FormFieldset>
</LoggedOutForm>
</div>
);
};
RequestLoginCode.propTypes = {
// mapped to state
currentUser: PropTypes.object,
emailRequested: PropTypes.bool,
isFetching: PropTypes.bool,
redirectTo: PropTypes.string,
requestError: PropTypes.object,
userEmail: PropTypes.string,
flow: PropTypes.string,
// mapped to dispatch
sendEmailLogin: PropTypes.func.isRequired,
hideMagicLoginRequestNotice: PropTypes.func.isRequired,
onReady: PropTypes.func,
onPublicTokenReceived: PropTypes.func,
};
const mapState = ( state ) => ( {
currentUser: getCurrentUser( state ),
isFetching: isFetchingMagicLoginEmail( state ),
requestError: getMagicLoginRequestEmailError( state ),
userEmail:
getLastCheckedUsernameOrEmail( state ) ||
getCurrentQueryArguments( state ).email_address ||
getInitialQueryArguments( state ).email_address,
} );
const mapDispatch = {
sendEmailLogin,
hideMagicLoginRequestNotice,
};
export default connect( mapState, mapDispatch )( localize( RequestLoginCode ) );
|