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');
|
2016-04-07 19:03:49 +02:00
|
|
|
const events = require('../events.js');
|
2016-04-01 00:20:34 +02:00
|
|
|
const topNavController = require('../controllers/top_nav_controller.js');
|
|
|
|
const RegistrationView = require('../views/registration_view.js');
|
2016-04-06 21:49:26 +02:00
|
|
|
const UserView = require('../views/user_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-04-06 21:49:26 +02:00
|
|
|
this.userView = new UserView();
|
|
|
|
}
|
|
|
|
|
|
|
|
registerRoutes() {
|
|
|
|
page('/register', () => { this.createUserRoute(); });
|
|
|
|
page('/users', () => { this.listUsersRoute(); });
|
|
|
|
page(
|
|
|
|
'/user/:name',
|
2016-04-06 22:34:21 +02:00
|
|
|
(ctx, next) => { this.loadUserRoute(ctx, next); },
|
|
|
|
(ctx, next) => { this.showUserRoute(ctx, next); });
|
2016-04-06 21:49:26 +02:00
|
|
|
page(
|
|
|
|
'/user/:name/edit',
|
2016-04-06 22:34:21 +02:00
|
|
|
(ctx, next) => { this.loadUserRoute(ctx, next); },
|
|
|
|
(ctx, next) => { this.editUserRoute(ctx, next); });
|
|
|
|
page.exit('/user/', (ctx, next) => { this.user = null; });
|
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');
|
2016-04-01 13:09:07 +02:00
|
|
|
this.registrationView.render({register: (...args) => {
|
|
|
|
return this._register(...args);
|
|
|
|
}});
|
2016-04-01 00:20:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_register(name, password, email) {
|
|
|
|
const data = {
|
2016-04-08 10:01:32 +02:00
|
|
|
name: name,
|
|
|
|
password: password,
|
|
|
|
email: email
|
2016-04-01 00:20:34 +02:00
|
|
|
};
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
api.post('/users/', data).then(() => {
|
2016-04-08 10:01:32 +02:00
|
|
|
api.login(name, password, false).then(() => {
|
2016-04-01 00:20:34 +02:00
|
|
|
resolve();
|
|
|
|
page('/');
|
2016-04-07 19:03:49 +02:00
|
|
|
events.notify(events.Success, 'Welcome aboard!');
|
2016-04-01 00:20:34 +02:00
|
|
|
}).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
|
|
|
}
|
|
|
|
|
2016-04-06 22:34:21 +02:00
|
|
|
loadUserRoute(ctx, next) {
|
|
|
|
if (ctx.state.user) {
|
|
|
|
next();
|
|
|
|
} else if (this.user && this.user.name == ctx.params.name) {
|
|
|
|
ctx.state.user = this.user;
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
api.get('/user/' + ctx.params.name).then(response => {
|
|
|
|
ctx.state.user = response.user;
|
|
|
|
ctx.save();
|
|
|
|
this.user = response.user;
|
|
|
|
next();
|
|
|
|
}).catch(response => {
|
|
|
|
this.userView.empty();
|
2016-04-07 19:03:49 +02:00
|
|
|
events.notify(events.Error, response.description);
|
2016-04-06 22:34:21 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_show(user, section) {
|
|
|
|
const isPrivate = api.isLoggedIn() && user.name == api.userName;
|
|
|
|
if (isPrivate) {
|
2016-04-01 00:20:34 +02:00
|
|
|
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
|
|
|
}
|
2016-04-06 22:34:21 +02:00
|
|
|
this.userView.render({
|
|
|
|
user: user, section: section, isPrivate: isPrivate});
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-04-06 22:34:21 +02:00
|
|
|
showUserRoute(ctx, next) {
|
|
|
|
this._show(ctx.state.user, 'summary');
|
|
|
|
}
|
|
|
|
|
|
|
|
editUserRoute(ctx, next) {
|
|
|
|
this._show(ctx.state.user, 'edit');
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
module.exports = new UsersController();
|