Ivan - Add send-recover-password api call logic [skip ci]

This commit is contained in:
ivan 2016-08-01 13:38:15 -03:00
parent ad1e45c301
commit 96fe51d08a
6 changed files with 68 additions and 12 deletions

View File

@ -4,6 +4,7 @@ const UserActions = Reflux.createActions([
'checkLoginStatus',
'login',
'logout',
'sendRecover',
'recoverPassword'
]);

View File

@ -24,6 +24,7 @@ let MainHomePageLoginWidget = React.createClass({
return {
sideToShow: 'front',
loginFormErrors: {},
recoverFormErrors: {},
recoverSent: false
};
},
@ -60,16 +61,16 @@ let MainHomePageLoginWidget = React.createClass({
renderPasswordRecovery() {
return (
<Widget className="main-home-page__widget login-widget_password" title={i18n('RECOVER_PASSWORD')} ref="recoverWidget">
<Form className="login-widget__form" onSubmit={this.handleForgotPasswordSubmit}>
<Form className="login-widget__form" ref="recoverForm" onSubmit={this.handleForgotPasswordSubmit} errors={this.state.recoverFormErrors} onValidateErrors={this.handleRecoverFormErrorsValidation}>
<div className="login-widget__inputs">
<Input placeholder="email" name="email" className="login-widget__input" validation="EMAIL"/>
<Input placeholder="email" name="email" className="login-widget__input" validation="EMAIL" required/>
</div>
<div className="login-widget__submit-button">
<Button type="primary">Recover my password</Button>
<Button type="primary">{i18n('RECOVER_PASSWORD')}</Button>
</div>
</Form>
<Button className="login-widget__forgot-password" type="link" onClick={this.handleBackToLoginClick} onMouseDown={(event) => {event.preventDefault()}}>
{'Back to login form'}
{i18n('BACK_LOGIN_FORM')}
</Button>
{this.renderRecoverStatus()}
</Widget>
@ -94,10 +95,8 @@ let MainHomePageLoginWidget = React.createClass({
UserActions.login(formState);
},
handleForgotPasswordSubmit() {
this.setState({
recoverSent: true
});
handleForgotPasswordSubmit(formState) {
UserActions.sendRecover(formState);
},
handleLoginFormErrorsValidation(errors) {
@ -106,6 +105,12 @@ let MainHomePageLoginWidget = React.createClass({
});
},
handleRecoverFormErrorsValidation(errors) {
this.setState({
recoverFormErrors: errors
});
},
handleForgotPasswordClick() {
this.setState({
sideToShow: 'back'
@ -125,9 +130,26 @@ let MainHomePageLoginWidget = React.createClass({
password: i18n('ERROR_PASSWORD')
}
}, function () {
this.refs.loginForm.refs.password.focus()
this.refs.loginForm.refs.password.focus();
}.bind(this));
}
if (event === 'SEND_RECOVER_FAIL') {
this.setState({
recoverFormErrors: {
email: i18n('EMAIL_NOT_EXIST')
}
}, function () {
this.refs.recoverForm.refs.email.focus();
}.bind(this));
}
if (event === 'SEND_RECOVER_SUCCESS') {
this.setState({
recoverSent: true
});
}
},
moveFocusToCurrentSide() {

View File

@ -47,6 +47,25 @@ module.exports = [
};
}
},
{
path: 'user/send-recover-password',
time: 100,
response: function (data) {
if (data.email.length > 10) {
return {
status: 'success',
data: {}
};
} else {
return {
status: 'fail',
message: 'Email not exists',
data: {}
};
}
}
},
{
path: 'user/recover-password',
time: 100,

View File

@ -4,11 +4,13 @@ export default {
'SIGN_UP': 'Sign up',
'FORGOT_PASSWORD': 'Forgot your password?',
'RECOVER_PASSWORD': 'Recover Password',
'RECOVER_SENT': 'An email with password recover instructions has been sent.',
'RECOVER_SENT': 'An email with recover instructions has been sent.',
'NEW_PASSWORD': 'New password',
'REPEAT_NEW_PASSWORD': 'Repeat new password',
'VALID_RECOVER': 'Password recovered successfully',
'INVALID_RECOVER': 'Invalid recover data',
'BACK_LOGIN_FORM': 'Back to login form',
'EMAIL_NOT_EXIST': 'Email does not exist',
//ERRORS
'ERROR_EMPTY': 'Invalid value',

View File

@ -3,7 +3,7 @@ import Validator from 'lib-app/validations/validator';
class EmailValidator extends Validator {
validate(value, form) {
if (!value.length) return this.getError('ERROR_EMPTY');
if (value.length < 6) return this.getError('ERROR_EMAIL');
if (value.indexOf('@') === -1) return this.getError('ERROR_EMAIL');
}
}

View File

@ -14,6 +14,7 @@ const UserStore = Reflux.createStore({
this.listenTo(UserActions.login, this.loginUser);
this.listenTo(UserActions.logout, this.logoutUser);
this.listenTo(UserActions.recoverPassword, this.recoverPassword);
this.listenTo(UserActions.sendRecover, this.sendRecoverPassword);
},
initSession() {
@ -54,13 +55,24 @@ const UserStore = Reflux.createStore({
});
},
sendRecoverPassword(recoverData) {
return API.call({
path: 'user/send-recover-password',
data: recoverData
}).then(() => {
this.trigger('SEND_RECOVER_SUCCESS');
}, () => {
this.trigger('SEND_RECOVER_FAIL')
});
},
recoverPassword(recoverData) {
return API.call({
path: 'user/recover-password',
data: recoverData
}).then(() => {
this.trigger('VALID_RECOVER');
//setTimeout(CommonActions.loggedOut, 2000);
setTimeout(CommonActions.loggedOut, 1000);
}, () => {
this.trigger('INVALID_RECOVER')
});