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