Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #190 from joyent/dustin/v4/fixes-and-improvements
Browse files Browse the repository at this point in the history
Bug fixes for reloading the User Management table after edits are made
  • Loading branch information
dustinryerson authored Jan 6, 2020
2 parents a22441c + f174a49 commit 2b770ca
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 143 deletions.
28 changes: 12 additions & 16 deletions src/views/UserManagement/UserActionModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ import {
demoteUser,
forcePasswordChange,
getUserTokens,
getUsers,
promoteUser,
} from '@api/users.js';
import { mapActions, mapState } from 'vuex';
Expand Down Expand Up @@ -207,7 +208,7 @@ export default {
};
},
methods: {
...mapActions(['clearUserAuthTokens']),
...mapActions(['clearUserAuthTokens', 'setUsers']),
closeModal() {
this.hasNoTokens = false;
Expand All @@ -225,15 +226,15 @@ export default {
if (action === 'reset-pwd') {
forcePasswordChange(userId).then(() => {
this.triggerSuccess(userId);
this.triggerSuccess();
});
} else if (action === 'promote') {
promoteUser(userId).then(() => {
this.triggerSuccess(userId);
this.triggerSuccess();
});
} else if (action === 'demote') {
demoteUser(userId).then(() => {
this.triggerSuccess(userId);
this.triggerSuccess();
});
} else if (action === 'deactivate') {
const params = {};
Expand All @@ -246,7 +247,7 @@ export default {
deactivateUser(userId, params).then(() => {
this.deactivateConfirmed = false;
this.triggerSuccess(userId, true);
this.triggerSuccess();
});
} else if (action === 'delete-auth-tokens') {
const params = { api_only: 1 };
Expand All @@ -256,29 +257,24 @@ export default {
this.clearUserAuthTokens();
}
this.triggerSuccess(userId);
this.triggerSuccess();
});
} else if (action === 'delete-login-tokens') {
const params = { login_only: 1 };
deleteUserTokens(userId, params).then(() => {
this.triggerSuccess(userId);
this.triggerSuccess();
});
}
},
triggerSuccess(userId, deactivate = false) {
triggerSuccess() {
this.$nextTick(() => {
this.success = true;
});
if (deactivate) {
EventBus.$emit('action-success', {
userId,
action: 'deactivate',
});
} else {
EventBus.$emit('action-success', { userId });
}
getUsers().then(response => {
this.setUsers(response.data);
});
},
},
computed: {
Expand Down
26 changes: 1 addition & 25 deletions src/views/UserManagement/UserManagement.vue
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ import UserModal from './UserModal.vue';
import UserActionModal from './UserActionModal.vue';
import Spinner from '@src/views/components/Spinner.vue';
import UsersTable from './UsersTable.vue';
import { getUser, getUsers } from '@api/users.js';
import { getUsers } from '@api/users.js';
import { EventBus } from '@src/eventBus.js';
import { mapActions, mapState } from 'vuex';
Expand Down Expand Up @@ -311,30 +311,6 @@ export default {
this.action = '';
}
);
EventBus.$on('action-success', actionData => {
const userId = actionData.userId;
const users = this.users;
if (actionData.action !== 'deactivate') {
getUser(userId).then(response => {
const newUser = response.data;
if (actionData.action && actionData.action === 'create') {
users.push(newUser);
this.setUsers(users);
} else {
const index = this.getIndex(users, userId);
users.splice(index, 1, newUser);
this.setUsers(users);
}
});
} else if (actionData && actionData.action === 'deactivate') {
const index = this.getIndex(users, userId);
users.splice(index, 1);
this.setUsers(users);
}
});
},
};
</script>
201 changes: 99 additions & 102 deletions src/views/UserManagement/UserModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ import isEmpty from 'lodash/isEmpty';
import ItemsPanel from './ItemsPanel.vue';
import { mapActions, mapState } from 'vuex';
import { EventBus } from '@src/eventBus.js';
import { createUser, editUser } from '@api/users.js';
import { createUser, editUser, getUsers } from '@api/users.js';
import { addUserToBuild, getBuilds, removeUserFromBuild } from '@api/builds.js';
import {
addUserToOrganization,
Expand Down Expand Up @@ -362,7 +362,7 @@ export default {
};
},
methods: {
...mapActions(['setBuilds', 'setOrganizations']),
...mapActions(['setBuilds', 'setOrganizations', 'setUsers']),
async advanceStep() {
if (this.step === 1 && this.validateForm()) {
if (this.validateForm()) {
Expand All @@ -376,100 +376,15 @@ export default {
}
}
},
async updateUserItems(itemType) {
// Only fire this on non-one-off edits
EventBus.$emit('get-selected-items');
this.isLoading = true;
const userId = this.user.id;
if (itemType === 'builds') {
const selectedBuilds = this.selectedBuilds;
if (selectedBuilds.length) {
for (let i = 0; i < selectedBuilds.length; i++) {
const build = selectedBuilds[i];
await addUserToBuild(build.id, userId, build.role);
}
}
if (this.user && this.user.builds && this.user.builds.length) {
const userBuilds = this.user.builds;
const selectedBuildIds = selectedBuilds.map(
build => build.id
);
for (let i = 0; i < userBuilds.length; i++) {
const build = userBuilds[i];
if (selectedBuildIds.indexOf(build.id) === -1) {
await removeUserFromBuild(build.id, userId);
}
}
}
if (this.addToOrganizations) {
this.step++;
} else {
EventBus.$emit('action-success', {
userId: this.user.id,
action: this.action,
});
this.closeModal();
}
} else {
const selectedOrganizations = this.selectedOrganizations;
if (selectedOrganizations.length) {
for (let i = 0; i < selectedOrganizations.length; i++) {
const organization = selectedOrganizations[i];
await addUserToOrganization(
organization.id,
organization.role,
userId
);
}
}
if (
this.user &&
this.user.organizations &&
this.user.organizations.length
) {
const userOrganizations = this.user.organizations;
const selectedOrganizationIds = selectedOrganizations.map(
organization => organization.id
);
for (let i = 0; i < userOrganizations.length; i++) {
const organization = userOrganizations[i];
if (
selectedOrganizationIds.indexOf(organization.id) ===
-1
) {
await removeUserFromOrganization(
organization.id,
userId
);
}
}
}
EventBus.$emit('action-success', {
userId: this.user.id,
action: this.action,
closeModal() {
if (this.reloadUserList) {
getUsers().then(response => {
this.setUsers(response.data);
});
this.closeModal();
}
this.isLoading = false;
},
closeModal() {
this.isActive = false;
EventBus.$emit('close-user-modal');
this.isActive = false;
},
createNewUser() {
this.isLoading = true;
Expand All @@ -486,13 +401,9 @@ export default {
createUser(user)
.then(response => {
this.user = response.data;
this.reloadUserList = true;
this.isLoading = false;
this.step = 2;
EventBus.$emit('action-success', {
userId: this.user.id,
action: 'create',
});
})
.catch(error => {
if (error.status === 409) {
Expand Down Expand Up @@ -533,13 +444,9 @@ export default {
editUser(editedUser)
.then(response => {
this.user = response.data;
this.reloadUserList = true;
this.isLoading = false;
this.step = 2;
EventBus.$emit('action-success', {
userId: this.user.id,
action: 'edit',
});
})
.catch(error => {
if (error.status === 500) {
Expand All @@ -562,6 +469,96 @@ export default {
passwordMismatch: false,
};
},
async updateUserItems(itemType) {
EventBus.$emit('get-selected-items');
this.isLoading = true;
const userId = this.user.id;
if (itemType === 'builds') {
const selectedBuilds = this.selectedBuilds;
if (selectedBuilds.length) {
for (let i = 0; i < selectedBuilds.length; i++) {
const build = selectedBuilds[i];
await addUserToBuild(build.id, userId, build.role);
}
if (
this.user &&
this.user.builds &&
this.user.builds.length
) {
const userBuilds = this.user.builds;
const selectedBuildIds = selectedBuilds.map(
build => build.id
);
for (let i = 0; i < userBuilds.length; i++) {
const build = userBuilds[i];
if (selectedBuildIds.indexOf(build.id) === -1) {
await removeUserFromBuild(build.id, userId);
}
}
}
this.reloadUserList = true;
}
if (this.addToOrganizations) {
this.isLoading = false;
this.step++;
return;
}
} else {
const selectedOrganizations = this.selectedOrganizations;
if (selectedOrganizations.length) {
for (let i = 0; i < selectedOrganizations.length; i++) {
const organization = selectedOrganizations[i];
await addUserToOrganization(
organization.id,
organization.role,
userId
);
}
if (
this.user &&
this.user.organizations &&
this.user.organizations.length
) {
const userOrganizations = this.user.organizations;
const selectedOrganizationIds = selectedOrganizations.map(
organization => organization.id
);
for (let i = 0; i < userOrganizations.length; i++) {
const organization = userOrganizations[i];
if (
selectedOrganizationIds.indexOf(
organization.id
) === -1
) {
await removeUserFromOrganization(
organization.id,
userId
);
}
}
}
this.reloadUserList = true;
}
}
this.closeModal();
this.isLoading = false;
},
validEmail() {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(this.email);
Expand Down

0 comments on commit 2b770ca

Please sign in to comment.