2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-28 22:33:20 +02:00
|
|
|
const page = require('page');
|
|
|
|
|
2016-03-19 21:37:04 +01:00
|
|
|
class UsersController {
|
2016-03-28 22:33:20 +02:00
|
|
|
constructor(
|
|
|
|
api, topNavigationController, authController, registrationView) {
|
|
|
|
this.api = api;
|
2016-03-19 21:37:04 +01:00
|
|
|
this.topNavigationController = topNavigationController;
|
|
|
|
this.authController = authController;
|
|
|
|
this.registrationView = registrationView;
|
|
|
|
}
|
|
|
|
|
|
|
|
listUsersRoute() {
|
|
|
|
this.topNavigationController.activate('users');
|
|
|
|
}
|
|
|
|
|
|
|
|
createUserRoute() {
|
|
|
|
this.topNavigationController.activate('register');
|
|
|
|
this.registrationView.render({
|
2016-03-28 22:33:20 +02:00
|
|
|
register: (userName, userPassword, userEmail) => {
|
|
|
|
const data = {
|
|
|
|
'name': userName,
|
|
|
|
'password': userPassword,
|
|
|
|
'email': userEmail
|
|
|
|
};
|
|
|
|
// TODO: reduce callback hell
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.api.post('/users/', data)
|
|
|
|
.then(() => {
|
|
|
|
this.authController.login(userName, userPassword)
|
|
|
|
.then(() => {
|
|
|
|
resolve();
|
|
|
|
page('/');
|
|
|
|
})
|
|
|
|
.catch(response => {
|
|
|
|
reject(response.description);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(response => {
|
|
|
|
reject(response.description);
|
|
|
|
});
|
|
|
|
});
|
2016-03-19 21:37:04 +01:00
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
showUserRoute(user) {
|
|
|
|
if (this.authController.isLoggedIn() &&
|
|
|
|
user == this.authController.getCurrentUser().name) {
|
|
|
|
this.topNavigationController.activate('account');
|
|
|
|
} else {
|
|
|
|
this.topNavigationController.activate('users');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
editUserRoute(user) {
|
|
|
|
this.topNavigationController.activate('users');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = UsersController;
|