client/views: move notifications to controllers

This commit is contained in:
rr- 2016-04-08 13:17:00 +02:00
parent 0ceaa8da42
commit 5bd7f1fa6c
7 changed files with 44 additions and 40 deletions

View file

@ -33,7 +33,10 @@ class AuthController {
resolve();
page('/');
events.notify(events.Success, 'Logged in');
}).catch(errorMessage => { reject(errorMessage); });
}).catch(errorMessage => {
reject(errorMessage);
events.notify(events.Error, errorMessage);
});
});
}});
}
@ -47,13 +50,8 @@ class AuthController {
passwordResetRoute() {
topNavController.activate('login');
this.passwordResetView.render({
proceed: nameOrEmail => {
api.logout();
return new Promise((resolve, reject) => {
api.get('/password-reset/' + nameOrEmail)
.then(() => { resolve(); })
.catch(errorMessage => { reject(errorMessage); });
});
proceed: (...args) => {
return this._passwordReset(...args);
}});
}
@ -76,6 +74,23 @@ class AuthController {
events.notify(events.Error, response.description);
});
}
_passwordReset(nameOrEmail) {
api.logout();
return new Promise((resolve, reject) => {
api.get('/password-reset/' + nameOrEmail)
.then(() => {
resolve();
events.notify(
events.Success,
'E-mail has been sent. To finish the procedure, ' +
'please click the link it contains.');
}).catch(response => {
reject();
events.notify(events.Error, response.description);
});
});
}
}
module.exports = new AuthController();

View file

@ -72,11 +72,13 @@ class UsersController {
resolve();
page('/');
events.notify(events.Success, 'Welcome aboard!');
}).catch(response => {
reject(response.description);
}).catch(errorMessage => {
reject();
events.notify(events.Error, errorMessage);
});
}).catch(response => {
reject(response.description);
reject();
events.notify(events.Error, response.description);
});
});
}
@ -103,10 +105,9 @@ class UsersController {
newPassword || api.userPassword,
false)
.then(next)
.catch(response => {
.catch(errorMessage => {
reject();
events.notify(
events.Error, response.description);
events.notify(events.Error, errorMessage);
});
} else {
next();

View file

@ -27,3 +27,13 @@ if (!Object.entries) {
// fix iterating over NodeList in Chrome and Opera
NodeList.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
// non standard
Promise.prototype.always = function(onResolveOrReject) {
return this.then(
onResolveOrReject,
reason => {
onResolveOrReject(reason);
throw reason;
});
};

View file

@ -31,13 +31,7 @@ class LoginView extends BaseView {
userNameField.value,
passwordField.value,
rememberUserField.checked)
.then(() => {
this.enableForm(form);
})
.catch(errorMessage => {
this.enableForm(form);
events.notify(events.Error, errorMessage);
});
.always(() => { this.enableForm(form); });
});
this.showView(target, source);

View file

@ -23,16 +23,7 @@ class PasswordResetView extends BaseView {
this.clearMessages();
this.disableForm(form);
ctx.proceed(userNameOrEmailField.value)
.then(() => {
events.notify(
events.Success,
'E-mail has been sent. To finish the procedure, ' +
'please click the link it contains.');
})
.catch(errorMessage => {
this.enableForm(form);
events.notify(events.Error, errorMessage);
});
.catch(() => { this.enableForm(form); });
});
this.showView(target, source);

View file

@ -31,13 +31,7 @@ class RegistrationView extends BaseView {
userNameField.value,
passwordField.value,
emailField.value)
.then(() => {
this.enableForm(form);
})
.catch(errorMessage => {
this.enableForm(form);
events.notify(events.Error, errorMessage);
});
.always(() => { this.enableForm(form); });
});
this.showView(target, source);

View file

@ -48,8 +48,7 @@ class UserEditView extends BaseView {
passwordField.value,
emailField.value,
rankField.value)
.then(user => { this.enableForm(form); })
.catch(() => { this.enableForm(form); });
.always(() => { this.enableForm(form); });
});
this.showView(target, source);