Files
meanTorrent/modules/users/client/controllers/admin/user.client.controller.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

(function () {
'use strict';
angular
.module('users.admin')
.controller('UserController', UserController);
UserController.$inject = ['$scope', '$state', '$window', 'Authentication', 'userResolve', 'Notification'];
function UserController($scope, $state, $window, Authentication, user, Notification) {
var vm = this;
vm.authentication = Authentication;
vm.user = user;
vm.remove = remove;
vm.update = update;
vm.isContextUserSelf = isContextUserSelf;
function remove(user) {
if ($window.confirm('Are you sure you want to delete this user?')) {
if (user) {
user.$remove();
vm.users.splice(vm.users.indexOf(user), 1);
Notification.success('User deleted successfully!');
} else {
vm.user.$remove(function () {
$state.go('admin.users');
Notification.success({ message: '<i class="glyphicon glyphicon-ok"></i> User deleted successfully!' });
});
}
}
}
function update(isValid) {
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'vm.userForm');
return false;
}
var user = vm.user;
user.$update(function () {
$state.go('admin.user', {
userId: user._id
});
Notification.success({ message: '<i class="glyphicon glyphicon-ok"></i> User saved successfully!' });
}, function (errorResponse) {
Notification.error({ message: errorResponse.data.message, title: '<i class="glyphicon glyphicon-remove"></i> User update error!' });
});
}
function isContextUserSelf() {
return vm.user.username === vm.authentication.user.username;
}
}
}());