EditUserCtrl

angular.module('8secondz.CmsAddons.EditUser', [ '8secondz.services.Account', 'dashboard.services.GeneralModel', '8secondz.Config', 'dashboard.services.Cache', 'ui.router' ]) .config(function config($stateProvider) { $stateProvider .state('dashboard.model.action.add-user', { url: '/add-user', controller: 'EditUserCtrl', templateUrl: '/cms-addons/custom/users/edit/EditUser.html', data: { pageTitle: 'New User | CMS' } }); $stateProvider .state('dashboard.model.action.edit-user', { url: '/edit-user/:id', controller: 'EditUserCtrl', templateUrl: '/cms-addons/custom/users/edit/EditUser.html', data: { pageTitle: 'Edit User | CMS' } }); }) .controller('EditUserCtrl', function($scope, $controller, $state, $stateParams, AccountService, GeneralModelService, Config, $modal, CacheService){ const self = this; this.init = () => { angular.extend(self, $controller('ModelEditCtrl', { $scope: $scope })); $scope.model = Config.serverParams.models[$scope.action.options.model]; $scope.hideFields = this.hideFields; }; $scope.$watch('data', () => { if($scope.isEdit){ $scope.modelDisplay = [ 'email', 'username', 'birthDate', 'created', 'gender', 'country', 'city', { property: 'videos', label: 'Videos', type: 'Number', readonly: true }, { property: 'wins', label: 'Wins', type: 'Number', readonly: true }, { property: 'likes', label: 'Likes', type: 'Number', readonly: true }, { property: 'followers', label: 'Followers', type: 'Number', readonly: true }, { property: 'followings', label: 'Followings', type: 'Number', readonly: true }, 'status' ]; $scope.model.properties['birthDate'].display.readonly = true; var userID = $stateParams.id; var params = { 'filter[include]': ['FriendNotification'], 'filter[counts]': ['Video', 'VideoLike', 'Competition'] }; GeneralModelService.get('Accounts', userID, params) .then((user) => { $scope.data.followers = _.filter(user.FriendNotification, {type: 'follower'}).length || 0; $scope.data.followings = _.filter(user.FriendNotification, {type: 'follower'}).length || 0; $scope.data.videos = user.VideoCount || 0; $scope.data.likes = user.VideoLikeCount || 0; $scope.data.wins = user.CompetitionCount || 0; }); }else{ $scope.modelDisplay = ['email', 'username', 'password', 'birthDate', 'gender', 'city', 'country']; $scope.model.properties['birthDate'].display.readonly = false; } }); $scope.clickDelete = (data) => { $scope.alertTitle = 'Delete User' ; $scope.alertMessage = `Are you sure you want to delete user ${data.email}?`; $scope.alertType = 'confirm'; const modalInstance = $modal.open({ templateUrl: 'app/dashboard/alert/Alert.html', controller: 'AlertCtrl', size: 'sm', scope: $scope }); $scope.okHandler = () => { GeneralModelService.remove('Accounts', data.id).then(() => { $state.go('dashboard.model.action.users'); }); } }; $scope.clickSave = (data) => { data.status = data.status || 'enabled'; data.emailVerified = data.emailVerified || false; var id = data.id; if(!$scope.isEdit){ id = null; delete data.created; data.profileCompleted = false; this.save(id, data); }else{ delete data.password; delete data.lastUpdated; this.update(id, data); } }; $scope.toggleStatus = (status, data) => { $scope.data.status = status === 'enabled' ? 'disabled' : 'enabled'; this.update(data.id, data); }; this.save = (id, data) => { GeneralModelService.save('Account', id, data).then(response => { const modalInstance = this.showModal(); modalInstance.result.finally(() => { CacheService.clear($scope.action.options.model); $state.go('dashboard.model.action.users'); }); }).catch(err => { this.showModal(err.name || 'Error', err.message || 'Something went wrong.'); }); }; this.update = (id, data) => { AccountService.updateUserById(id, data).then((data) => { const modalInstance = this.showModal(); modalInstance.result.finally(() => { CacheService.clear($scope.action.options.model); }); return; }); }; $scope.toggleStatus = (status, data) => { $scope.data.status = status === 'enabled' ? 'disabled' : 'enabled'; this.update(data.id, data); }; // this.hideFields = (key) => { // return ['id', 'credentials', 'description', 'emailVerified', 'entryToken', 'facebookId', 'challenges', // 'lastUpdated', 'phoneNumber', 'mediaId', 'password', 'realm', 'twitterId', 'verificationToken', 'profileCompleted'] > -1; // }; this.showModal = (title, message) => { $scope.alertTitle = title || 'SUCCESS'; $scope.alertMessage = message || `${$scope.data.username} successfully ${$scope.isEdit ? 'updated' : 'saved'}.`; $scope.allowAlertClose = true; return $modal.open({ templateUrl: 'app/dashboard/alert/Alert.html', controller: 'AlertCtrl', size: 'sm', scope: $scope }); }; self.init(); });

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.