salix/modules/account/front/descriptor/index.js

110 lines
3.0 KiB
JavaScript

import ngModule from '../module';
import Descriptor from 'salix/components/descriptor';
import UserError from 'core/lib/user-error';
class Controller extends Descriptor {
get user() {
return this.entity;
}
set user(value) {
this.entity = value;
}
get entity() {
return super.entity;
}
set entity(value) {
super.entity = value;
this.hasAccount = null;
if (!value) return;
this.$http.get(`UserAccounts/${value.id}/exists`)
.then(res => this.hasAccount = res.data.exists);
}
onDelete() {
return this.$http.delete(`Accounts/${this.id}`)
.then(() => this.$state.go('account.index'))
.then(() => this.vnApp.showSuccess(this.$t('User removed')));
}
onChangePassClick(askOldPass) {
this.$http.get('UserPasswords/findOne')
.then(res => {
this.passRequirements = res.data;
this.askOldPass = askOldPass;
this.$.changePass.show();
});
}
onPassChange() {
if (!this.newPassword)
throw new UserError(`You must enter a new password`);
if (this.newPassword != this.repeatPassword)
throw new UserError(`Passwords don't match`);
let method;
const params = {newPassword: this.newPassword};
if (this.askOldPass) {
method = 'changePassword';
params.oldPassword = this.oldPassword;
} else
method = 'setPassword';
return this.$http.patch(`Accounts/${this.id}/${method}`, params)
.then(() => {
this.emit('change');
this.vnApp.showSuccess(this.$t('Password changed succesfully!'));
});
}
onPassClose() {
this.oldPassword = '';
this.newPassword = '';
this.repeatPassword = '';
this.$.$apply();
}
onEnableAccount() {
return this.$http.post(`UserAccounts`, {id: this.id})
.then(() => this.onSwitchAccount(true));
}
onDisableAccount() {
return this.$http.delete(`UserAccounts/${this.id}`)
.then(() => this.onSwitchAccount(false));
}
onSwitchAccount(enable) {
this.hasAccount = enable;
const message = enable
? 'Account enabled!'
: 'Account disabled!';
this.emit('change');
this.vnApp.showSuccess(this.$t(message));
}
onSetActive(active) {
return this.$http.patch(`Accounts/${this.id}`, {active})
.then(() => {
this.user.active = active;
const message = active
? 'User activated!'
: 'User deactivated!';
this.emit('change');
this.vnApp.showSuccess(this.$t(message));
});
}
}
ngModule.component('vnUserDescriptor', {
template: require('./index.html'),
controller: Controller,
bindings: {
user: '<'
}
});