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

Fixes usage of wrong endpoint and renames endpoint functions to be less confusing #306

Merged
merged 1 commit into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/api/__tests__/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('users API', () => {
});
});

describe('editUser', () => {
describe('updateUser', () => {
let data;

beforeEach(() => {
Expand All @@ -105,7 +105,8 @@ describe('users API', () => {

test('should return a status of 200', async () => {
expect.assertions(1);
response = await Users.editUser(user);
const { email, is_admin: isAdmin, name } = user;
response = await Users.updateUser(userId, email, isAdmin, name);
expect(response.status).toEqual(200);
});
});
Expand Down
45 changes: 20 additions & 25 deletions src/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,6 @@ export const deleteUserTokens = (userId, params) => {
});
};

export const editUser = user => {
const data = {};

data.is_admin = user.is_admin;

if (user.email) {
data.email = user.email;
}

if (user.name) {
data.name = user.name;
}

return requestWithToken({
method: 'POST',
url: `/user/${user.id}`,
data,
}).catch(error => {
return Promise.reject(error);
});
};

export const forcePasswordChange = userId => {
return requestWithToken({
method: 'DELETE',
Expand Down Expand Up @@ -137,6 +115,21 @@ export const promoteUser = userId => {
});
};

export const updateCurrentUser = (email, isAdmin, name) => {
return requestWithToken({
method: 'POST',
url: '/user/me',
data: {
email,
is_admin: isAdmin,
name,
},
params: {
send_mail: 1,
},
});
};

export const updatePassword = (password, params) => {
return requestWithToken({
method: 'POST',
Expand All @@ -146,10 +139,10 @@ export const updatePassword = (password, params) => {
});
};

export const updateUser = (email, isAdmin, name) => {
export const updateUser = (userId, email, isAdmin, name) => {
return requestWithToken({
method: 'POST',
url: '/user/me',
url: `/user/${userId}`,
data: {
email,
is_admin: isAdmin,
Expand All @@ -158,6 +151,8 @@ export const updateUser = (email, isAdmin, name) => {
params: {
send_mail: 1,
},
}).catch(error => {
return Promise.reject(error);
});
};

Expand All @@ -169,7 +164,6 @@ export default {
deleteUserToken,
deleteUserTokens,
demoteUser,
editUser,
forcePasswordChange,
getCurrentUser,
getToken,
Expand All @@ -178,5 +172,6 @@ export default {
getUsers,
promoteUser,
updatePassword,
updateCurrentUser,
updateUser,
};
37 changes: 30 additions & 7 deletions src/views/UserManagement/EditUser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
:disabled="!isUserModified || !hasRequiredFields"
@click="
isUserModified && hasRequiredFields && !isLoading
? editUser()
? updateUser()
: null
"
>Save Changes</a
Expand All @@ -122,7 +122,7 @@ import cloneDeep from 'lodash/cloneDeep';
import isEmpty from 'lodash/isEmpty';
import ResetPassword from '../UserManagement/ResetPassword.vue';
import { mapActions, mapState } from 'vuex';
import { updateUser } from '@api/users.js';
import { updateCurrentUser, updateUser } from '@api/users.js';

export default {
components: {
Expand Down Expand Up @@ -163,14 +163,37 @@ export default {
},
methods: {
...mapActions(['setCurrentUser']),
async editUser() {
async updateUser() {
let user;

this.isLoading = true;

try {
const response = await updateUser(this.email, this.isAdmin, this.name);
const user = response.data;
this.setCurrentUser(user);
this.$emit('set-user', { user });
const currentUserId = this.currentUser && this.currentUser.id;
const routeParamUserId =
this.$route && this.$route.params && this.$route.params.id;

if (
currentUserId &&
routeParamUserId &&
currentUserId === routeParamUserId
) {
const response = await updateCurrentUser(
this.email,
this.isAdmin,
this.name
);
user = response.data;
this.setCurrentUser(user);
} else {
const response = await updateUser(
routeParamUserId,
this.email,
this.isAdmin,
this.name
);
user = response.data;
}

this.email = user.email;
this.name = user.name;
Expand Down
74 changes: 52 additions & 22 deletions src/views/UserManagement/UserModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ import isEmpty from 'lodash/isEmpty';
import ItemsPanel from './ItemsPanel.vue';
import { mapActions, mapState } from 'vuex';
import { EventBus } from '@src/eventBus.js';
import { createUser, editUser, getUsers } from '@api/users.js';
import {
createUser,
getUsers,
updateCurrentUser,
updateUser,
} from '@api/users.js';
import { addUserToBuild, getBuilds, removeUserFromBuild } from '@api/builds.js';
import {
addUserToOrganization,
Expand Down Expand Up @@ -271,7 +276,12 @@ export default {
};
},
methods: {
...mapActions(['setBuilds', 'setOrganizations', 'setUsers']),
...mapActions([
'setBuilds',
'setCurrentUser',
'setOrganizations',
'setUsers',
]),
async advanceStep() {
if (this.step === 1 && this.validateForm()) {
if (this.validateForm()) {
Expand Down Expand Up @@ -360,26 +370,46 @@ export default {
this.isLoading = true;

if (this.validateForm()) {
const editedUser = {
id: this.editingUser.id,
email: this.email,
is_admin: this.isAdmin,
name: this.name,
};

editUser(editedUser)
.then(response => {
this.user = response.data;
this.reloadUserList = true;
this.isLoading = false;
this.step = 2;
})
.catch(error => {
if (error.status === 500) {
this.errors.duplicateEmail = true;
if (
this.currentUser &&
this.currentUser.id &&
this.currentUser.id === this.editingUser.id
) {
updateCurrentUser(
this.editingUser.id,
this.email,
this.isAdmin,
this.name
)
.then(response => {
const user = response.data;
this.user = user;
this.setCurrentUser(user);
this.reloadUserList = true;
this.isLoading = false;
}
});
this.step = 2;
})
.catch(error => {
if (error.status === 500) {
this.errors.duplicateEmail = true;
this.isLoading = false;
}
});
} else {
updateUser(this.editingUser.id, this.email, this.isAdmin, this.name)
.then(response => {
this.user = response.data;
this.reloadUserList = true;
this.isLoading = false;
this.step = 2;
})
.catch(error => {
if (error.status === 500) {
this.errors.duplicateEmail = true;
this.isLoading = false;
}
});
}
} else {
this.isLoading = false;
}
Expand Down Expand Up @@ -488,7 +518,7 @@ export default {
},
},
computed: {
...mapState(['builds', 'organizations']),
...mapState(['builds', 'currentUser', 'organizations']),
hasErrors() {
return Object.values(this.errors).some(error => error === true)
? true
Expand Down