Skip to content

Commit

Permalink
finish user state functions
Browse files Browse the repository at this point in the history
  • Loading branch information
v1r0x committed Jul 21, 2017
1 parent 92049f7 commit 4bbaf87
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 68 deletions.
30 changes: 25 additions & 5 deletions app.js
Expand Up @@ -174,15 +174,15 @@ spacialistApp.service('modalFactory', ['$uibModal', function($uibModal) {
});
modalInstance.result.then(function(selectedItem) {}, function() {});
};
this.addUserModal = function(onCreate) {
this.addUserModal = function(onCreate, users) {
var modalInstance = $uibModal.open({
templateUrl: 'layouts/new-user.html',
templateUrl: 'modals/add-user.html',
controller: function($uibModalInstance) {
this.cancel = function(result) {
$uibModalInstance.dismiss('cancel');
};
this.onCreate = function(name, email, password) {
onCreate(name, email, password);
onCreate(name, email, password, users);
$uibModalInstance.dismiss('ok');
};
},
Expand Down Expand Up @@ -1325,7 +1325,6 @@ spacialistApp.config(function($stateProvider, $urlRouterProvider, $authProvider,
})
.state('root.user.edit', {
url: '/edit/{id:[0-9]+}',
component: 'useredit',
resolve: {
user: function(user) {
// TODO other access to user object?
Expand All @@ -1337,7 +1336,28 @@ spacialistApp.config(function($stateProvider, $urlRouterProvider, $authProvider,
return u.id == $transition$.params().id;
});
}
}
},
onEnter: ['selectedUser', '$state', '$uibModal', function(selectedUser, $state, $uibModal) {
$uibModal.open({
templateUrl: "modals/edit-user.html",
controller: ['$scope', 'userService', function($scope, userService) {
var orgUser = selectedUser;
$scope.editUser = angular.copy(orgUser);

$scope.cancel = function() {
$scope.$dismiss();
};

$scope.onEdit = function(editUser) {
userService.editUser(orgUser, editUser).then(function() {
$scope.$close(true);
});
};
}]
}).result.finally(function() {
$state.go('^');
});
}]
})
.state('root.role', {
url: '/role',
Expand Down
43 changes: 27 additions & 16 deletions app/userService.js
@@ -1,4 +1,4 @@
spacialistApp.service('userService', ['httpPostFactory', 'httpGetFactory', 'httpGetPromise', 'httpPutFactory', 'httpPatchFactory', 'httpDeleteFactory', 'modalFactory', 'snackbarService', '$auth', '$state', '$http', '$translate', function(httpPostFactory, httpGetFactory, httpGetPromise, httpPutFactory, httpPatchFactory, httpDeleteFactory, modalFactory, snackbarService, $auth, $state, $http, $translate) {
spacialistApp.service('userService', ['httpPostFactory', 'httpGetFactory', 'httpGetPromise', 'httpPutFactory', 'httpPatchFactory', 'httpPatchPromise', 'httpDeleteFactory', 'modalFactory', 'snackbarService', '$auth', '$state', '$http', '$translate', function(httpPostFactory, httpGetFactory, httpGetPromise, httpPutFactory, httpPatchFactory, httpPatchPromise, httpDeleteFactory, modalFactory, snackbarService, $auth, $state, $http, $translate) {
var user = {};
user.currentUser = {
permissions: {},
Expand Down Expand Up @@ -51,32 +51,43 @@ spacialistApp.service('userService', ['httpPostFactory', 'httpGetFactory', 'http
});
};

user.deleteUser = function(u) {
user.deleteUser = function(u, users) {
httpDeleteFactory('api/user/' + u.id, function(response) {
var index = user.users.indexOf(u);
if(index > -1) user.users.splice(index, 1);
var index = users.indexOf(u);
if(index > -1) users.splice(index, 1);
});
};

user.addUser = function(name, email, password) {
user.openAddUserDialog = function(users) {
modalFactory.addUserModal(user.addUser, users);
}

user.addUser = function(name, email, password, users) {
var formData = new FormData();
formData.append('name', name);
formData.append('email', email);
formData.append('password', password);
httpPostFactory('api/user', formData, function(response) {
user.users.push(response.user);
users.push(response.user);
});
};

user.editUser = function(changes, id, $index) {
user.editUser = function(orgUser, newUser) {
var oldValues = angular.copy(orgUser);
var formData = new FormData();
for(var k in changes) {
if(changes.hasOwnProperty(k)) {
formData.append(k, changes[k]);
for(var k in newUser) {
if(newUser.hasOwnProperty(k)) {
if(newUser[k] != oldValues[k]) {
formData.append(k, newUser[k]);
}
}
}
httpPatchFactory('api/user/' + id, formData, function(response) {
user.users[$index] = response.user;
return httpPatchPromise.getData('api/user/' + orgUser.id, formData).then(function(response) {
for(var k in response.user) {
if(response.user.hasOwnProperty(k)) {
orgUser[k] = response.user[k];
}
}
var content = $translate.instant('snackbar.data-updated.success');
snackbarService.addAutocloseSnack(content, 'success');
});
Expand Down Expand Up @@ -180,19 +191,19 @@ spacialistApp.service('userService', ['httpPostFactory', 'httpGetFactory', 'http
});
};

user.addUserRole = function($item, user_id) {
user.addUserRole = function(item, user_id) {
var formData = new FormData();
formData.append('role_id', $item.id);
formData.append('role_id', item.id);
httpPatchFactory('api/user/' + user_id + '/attachRole', formData, function(response) {
// TODO only remove/add role if function returns no error
var content = $translate.instant('snackbar.data-updated.success');
snackbarService.addAutocloseSnack(content, 'success');
});
};

user.removeUserRole = function($item, user_id) {
user.removeUserRole = function(item, user_id) {
var formData = new FormData();
formData.append('role_id', $item.id);
formData.append('role_id', item.id);
httpPatchFactory('api/user/' + user_id + '/detachRole', formData, function(response) {
// TODO only remove/add role if function returns no error
var content = $translate.instant('snackbar.data-updated.success');
Expand Down
10 changes: 2 additions & 8 deletions components/user.js
Expand Up @@ -5,12 +5,6 @@ spacialistApp.component('user', {
roles: '<',
rolesPerUser: '<'
},
templateUrl: 'user.html'
});

spacialistApp.component('useredit', {
bindings: {
user: '<',
selectedUser: '<'
}
templateUrl: 'user.html',
controller: 'userCtrl'
});
84 changes: 84 additions & 0 deletions controllers/oldUserCtrl.js
@@ -0,0 +1,84 @@
spacialistApp.controller('userCtrl', ['$scope', 'userService', 'mainService', 'analysisService', '$state', 'modalFactory', function($scope, userService, mainService, analysisService, $state, modalFactory) {
$scope.currentUser = userService.currentUser;
$scope.users = userService.users;
$scope.roles = userService.roles;
$scope.permissions = userService.permissions;
$scope.loginError = userService.loginError;
$scope.analysisEntries = analysisService.entries;
$scope.setAnalysisEntry = analysisService.setAnalysisEntry;
$scope.deleteUser = userService.deleteUser;
$scope.toggleEditMode = mainService.toggleEditMode;

$scope.openStartPage = function() {
analysisService.unsetAnalysisEntry();
$state.go('root.spacialist');
};

$scope.loginUser = function(email, password) {
var credentials = {
email: email,
password: password
};
userService.loginUser(credentials);
};

$scope.guestLogin = function() {
var email = 'udontneedtoseehisidentification@rebels.tld';
var pw = 'thesearentthedroidsuarelookingfor';
$scope.loginUser(email, pw);
};

$scope.logoutUser = function() {
userService.logoutUser();
};

$scope.openUserManagement = function() {
$state.go('root.user', {});
};

$scope.openRoleManagement = function() {
$state.go('root.role', {});
};

$scope.addRolePermission = function(item, role) {
userService.addRolePermission(item, role);
};

$scope.removeRolePermission = function(item, role) {
userService.removeRolePermission(item, role);
};

$scope.deleteRole = function(role) {
userService.deleteRole(role);
};

$scope.openAddRoleDialog = function() {
userService.openEditRoleDialog();
};

$scope.openEditRoleDialog = function(role) {
userService.openEditRoleDialog(role);
};

$scope.addUserRole = function($item, user_id) {
userService.addUserRole($item, user_id);
};

$scope.removeUserRole = function($item, user_id) {
userService.removeUserRole($item, user_id);
};

$scope.openAddUserDialog = function() {
modalFactory.addUserModal(userService.addUser);
};

$scope.openEditUserDialog = function(user, $index) {
var values = {
id: user.id,
name: user.name,
email: user.email,
password: ''
};
modalFactory.editUserModal(userService.editUser, values, $index);
};
}]);
43 changes: 10 additions & 33 deletions controllers/userCtrl.js
@@ -1,19 +1,10 @@
spacialistApp.controller('userCtrl', ['$scope', 'userService', 'mainService', 'analysisService', '$state', 'modalFactory', function($scope, userService, mainService, analysisService, $state, modalFactory) {
$scope.currentUser = userService.currentUser;
$scope.users = userService.users;
$scope.roles = userService.roles;
$scope.permissions = userService.permissions;
spacialistApp.controller('userCtrl', ['$scope', 'userService', 'mainService', '$state', 'modalFactory', function($scope, userService, mainService, $state, modalFactory) {
var localUsers = this.users;

$scope.loginError = userService.loginError;
$scope.analysisEntries = analysisService.entries;
$scope.setAnalysisEntry = analysisService.setAnalysisEntry;
$scope.deleteUser = userService.deleteUser;
$scope.toggleEditMode = mainService.toggleEditMode;

$scope.openStartPage = function() {
analysisService.unsetAnalysisEntry();
$state.go('root.spacialist');
};

$scope.loginUser = function(email, password) {
var credentials = {
email: email,
Expand All @@ -32,12 +23,8 @@ spacialistApp.controller('userCtrl', ['$scope', 'userService', 'mainService', 'a
userService.logoutUser();
};

$scope.openUserManagement = function() {
$state.go('root.user', {});
};

$scope.openRoleManagement = function() {
$state.go('root.role', {});
$scope.deleteUser = function(user) {
userService.deleteUser(user, localUsers);
};

$scope.addRolePermission = function(item, role) {
Expand All @@ -60,25 +47,15 @@ spacialistApp.controller('userCtrl', ['$scope', 'userService', 'mainService', 'a
userService.openEditRoleDialog(role);
};

$scope.addUserRole = function($item, user_id) {
userService.addUserRole($item, user_id);
$scope.addUserRole = function(item, user_id) {
userService.addUserRole(item, user_id);
};

$scope.removeUserRole = function($item, user_id) {
userService.removeUserRole($item, user_id);
$scope.removeUserRole = function(item, user_id) {
userService.removeUserRole(item, user_id);
};

$scope.openAddUserDialog = function() {
modalFactory.addUserModal(userService.addUser);
};

$scope.openEditUserDialog = function(user, $index) {
var values = {
id: user.id,
name: user.name,
email: user.email,
password: ''
};
modalFactory.editUserModal(userService.editUser, values, $index);
userService.openAddUserDialog(localUsers);
};
}]);
File renamed without changes.
2 changes: 1 addition & 1 deletion modals/edit-bibliography.html
Expand Up @@ -39,7 +39,7 @@ <h4>{{'literature.new.bibtex-code'|translate}}</h4>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" ng-click="onEdit(editEntry, type.selected)">{{ 'Save' | translate }}</button>
<button type="button" class="btn btn-success" ng-click="onEdit(editUser)">{{ 'Save' | translate }}</button>
<button type="button" class="btn btn-danger"
ng-click="cancel()">{{ 'cancel' | translate }}</button>
</div>
10 changes: 5 additions & 5 deletions layouts/edit-user.html → modals/edit-user.html
Expand Up @@ -12,7 +12,7 @@ <h4>{{'user.edit.heading'|translate}}</h4>
<input class="form-control" type="text" id="name" ng-model="mc.userinfo.name" />
</div>-->
<p class="form-control-static">
{{ mc.userinfo.name }}
{{ editUser.name }}
</p>
</div>
<div class="form-group">
Expand All @@ -23,22 +23,22 @@ <h4>{{'user.edit.heading'|translate}}</h4>
<input class="form-control" type="email" id="email" ng-model="mc.userinfo.email" />
</div>-->
<p class="form-control-static">
{{ mc.userinfo.email }}
{{ editUser.email }}
</p>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="password">
{{'user.edit.new-password'|translate}}:
</label>
<div class="col-md-9">
<input class="form-control" type="password" id="password" ng-model="mc.userinfo.password" />
<input class="form-control" type="password" id="password" ng-model="editUser.password" />
</div>
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" ng-click="mc.onEdit(mc.userinfo)">{{ 'Save' | translate }}</button>
<button type="button" class="btn btn-success" ng-click="onEdit(editUser)">{{ 'Save' | translate }}</button>
<button type="button" class="btn btn-danger"
ng-click="mc.cancel()">{{ 'cancel' | translate }}</button>
ng-click="cancel()">{{ 'cancel' | translate }}</button>
</div>

0 comments on commit 4bbaf87

Please sign in to comment.