Skip to content

Commit

Permalink
Use redux to step through magic login flow
Browse files Browse the repository at this point in the history
  • Loading branch information
jblz committed Apr 5, 2017
1 parent da126d4 commit 9d8b038
Show file tree
Hide file tree
Showing 18 changed files with 1,318 additions and 267 deletions.
49 changes: 0 additions & 49 deletions client/login/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import React from 'react';
* Internal dependencies
*/
import config from 'config';
import EmailedLoginLinkSuccessfully from './emailed-login-link-successfully';
import EmailedLoginLinkExpired from './emailed-login-link-expired';
import HandleEmailedLinkForm from './handle-emailed-link-form';
import Main from 'components/main';
import { renderWithReduxStore } from 'lib/react-helpers';
import RequestLoginEmailForm from './request-login-email-form';
import WPLogin from './wp-login';

export default {
Expand All @@ -24,49 +19,5 @@ export default {
context.store
);
}
},

magicLoginRequestEmailForm: function( context ) {
renderWithReduxStore( (
<Main className="login__magic-login-request-form">
<RequestLoginEmailForm />
</Main>
),
document.getElementById( 'primary' ),
context.store
);
},

magicLoginLinkWasSent: function( context ) {
renderWithReduxStore( (
<Main className="login__magic-link-was-sent">
<EmailedLoginLinkSuccessfully />
</Main>
),
document.getElementById( 'primary' ),
context.store
);
},

magicLoginClickHandler: function( context ) {
renderWithReduxStore( (
<Main className="login__magic-login-handle-click">
<HandleEmailedLinkForm />
</Main>
),
document.getElementById( 'primary' ),
context.store
);
},

magicLoginHasExpired: function( context ) {
renderWithReduxStore( (
<Main className="login__magic-link-exipred">
<EmailedLoginLinkExpired />
</Main>
),
document.getElementById( 'primary' ),
context.store
);
}
};
7 changes: 0 additions & 7 deletions client/login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,4 @@ export default () => {
if ( config.isEnabled( 'wp-login' ) ) {
page( '/login', controller.login );
}

if ( config.isEnabled( 'magic-login' ) ) {
page( '/login/link-has-expired', controller.magicLoginHasExpired );
page( '/login/link-was-sent', controller.magicLoginLinkWasSent );
page( '/login/handle-emailed-link', controller.magicLoginClickHandler );
page( '/login/send-me-a-link', controller.magicLoginRequestEmailForm );
}
};
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
* External dependencies
*/
import React from 'react';
import { connect } from 'react-redux';
import { localize } from 'i18n-calypso';
import emailValidator from 'email-validator';

/**
* Internal dependencies
*/
import EmptyContent from 'components/empty-content';
import RedirectWhenLoggedIn from 'components/redirect-when-logged-in';
import { getCurrentQueryArguments } from 'state/ui/selectors';

class EmailedLoginLinkSuccessfully extends React.Component {
render() {
Expand Down Expand Up @@ -50,13 +47,4 @@ class EmailedLoginLinkSuccessfully extends React.Component {
}
}

const mapState = state => {
const queryArguments = getCurrentQueryArguments( state );
const { email } = queryArguments;

return {
emailAddress: emailValidator.validate( email ) ? email : null,
};
};

export default connect( mapState )( localize( EmailedLoginLinkSuccessfully ) );
export default localize( EmailedLoginLinkSuccessfully );
File renamed without changes.
139 changes: 139 additions & 0 deletions client/login/magic-login/request-login-email-form.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**
* External dependencies
*/
import React from 'react';
import { connect } from 'react-redux';

/**
* Internal dependencies
*/
import {
fetchRequestEmail,
hideRequestForm,
hideRequestNotice,
setInputEmailAddress,
} from 'state/login/magic-login/actions';
import {
emailAddressFormInput,
emailAddressFormInputIsValid,
isFetchingEmail,
requestEmailError,
requestedEmailSuccessfully,
} from 'state/login/magic-login/selectors';

import FormButton from 'components/forms/form-button';
import FormFieldset from 'components/forms/form-fieldset';
import FormTextInput from 'components/forms/form-text-input';
import LoggedOutForm from 'components/logged-out-form';
import LoggedOutFormLinks from 'components/logged-out-form/links';
import LoggedOutFormLinkItem from 'components/logged-out-form/link-item';
import Notice from 'components/notice';
import { localize } from 'i18n-calypso';

//@TODO move this to wp-login compoenent
import { getCurrentUser } from 'state/current-user/selectors';

class RequestLoginEmailForm extends React.Component {
onEmailAddressFieldChange = event => {
this.props.setInputEmailAddress( event.target.value );
};

onSubmitBound = event => {
event.preventDefault();
this.props.onSubmit( this.props.emailAddress );
};

render() {
const {
currentUser,
requestError,
isFetching,
isValidEmailAddress,
onNoticeDismiss,
onClickEnterPasswordInstead,
emailRequested,
translate,
} = this.props;

const submitEnabled = (
isValidEmailAddress &&
! isFetching &&
! emailRequested &&
! requestError
);

const errorText = typeof requestError === 'string' && requestError.length
? requestError : translate( 'Unable to complete request' );

return (
<div>
{ requestError &&
<Notice
duration={ 10000 }
text={ errorText }
className="magic-login__request-login-email-form-notice"
showDismiss={ true }
onDismissClick={ onNoticeDismiss }
status="is-error" />
}
<LoggedOutForm onSubmit={ this.onSubmitBound }>
<p>{
translate( 'Get a link sent to the email address associated ' +
'with your account to log in instantly without your password.' )
}</p>
{ currentUser && currentUser.username &&
<p>{
translate( 'NOTE: You are already logged in as user: %(user)s', {
args: {
user: currentUser.username
}
} ) }</p>
}
<FormFieldset>
<FormTextInput
autoCapitalize="off"
autoFocus="true"
name="emailAddress"
placeholder="Email address"
value={ this.props.emailAddress }
onChange={ this.onEmailAddressFieldChange }
/>

<div className="magic-login__form-action">
<FormButton primary disabled={ ! submitEnabled }>
{ translate( 'Request Email' ) }

This comment has been minimized.

Copy link
@yoavf

yoavf Apr 6, 2017

Contributor

This was confusing to our translators, because of the lack of an indefinite article, i.e "Request an Email".
A translator comment would help. (Something like "button label to request to receive an email message")

</FormButton>
</div>
</FormFieldset>
</LoggedOutForm>
<LoggedOutFormLinks>
<LoggedOutFormLinkItem onClick={ onClickEnterPasswordInstead }>
{ translate( 'Enter a password instead' ) }
</LoggedOutFormLinkItem>
</LoggedOutFormLinks>
</div>
);
}
}

const mapState = state => {
return {
currentUser: getCurrentUser( state ),
emailAddress: emailAddressFormInput( state ),
isFetching: isFetchingEmail( state ),
isValidEmailAddress: emailAddressFormInputIsValid( state ),
requestError: requestEmailError( state ),
emailRequested: requestedEmailSuccessfully( state ),
};
};

const mapDispatch = dispatch => {
return {
onClickEnterPasswordInstead: () => dispatch( hideRequestForm() ),
onNoticeDismiss: () => dispatch( hideRequestNotice() ),
onSubmit: emailAddress => dispatch( fetchRequestEmail( emailAddress ) ),
setInputEmailAddress: emailAddress => dispatch( setInputEmailAddress( emailAddress ) ),
};
};

export default connect( mapState, mapDispatch )( localize( RequestLoginEmailForm ) );
Loading

0 comments on commit 9d8b038

Please sign in to comment.