diff --git a/src/api/__mocks__/users.js b/src/api/__mocks__/users.js index 016772a7..07aca91e 100644 --- a/src/api/__mocks__/users.js +++ b/src/api/__mocks__/users.js @@ -48,7 +48,7 @@ export async function searchUsers(query, filters, authHost, token, page = 1) { }}) } -export async function addRight(nickname, right, authHost, token) { +export async function addRight(nicknames, right, authHost, token) { return Promise.resolve({ data: { [`is_${right}`]: true } }) diff --git a/src/api/users.js b/src/api/users.js index 1e97da92..9351cc45 100644 --- a/src/api/users.js +++ b/src/api/users.js @@ -2,12 +2,13 @@ import request from '@/utils/request' import { getToken } from '@/utils/auth' import { baseName } from './utils' -export async function addRight(nickname, right, authHost, token) { +export async function addRight(nicknames, right, authHost, token) { return await request({ baseURL: baseName(authHost), - url: `/api/pleroma/admin/users/${nickname}/permission_group/${right}`, + url: `/api/pleroma/admin/users/permission_group/${right}`, method: 'post', - headers: authHeaders(token) + headers: authHeaders(token), + data: { nicknames } }) } @@ -21,12 +22,13 @@ export async function createNewAccount(nickname, email, password, authHost, toke }) } -export async function deleteRight(nickname, right, authHost, token) { +export async function deleteRight(nicknames, right, authHost, token) { return await request({ baseURL: baseName(authHost), - url: `/api/pleroma/admin/users/${nickname}/permission_group/${right}`, + url: `/api/pleroma/admin/users/permission_group/${right}`, method: 'delete', - headers: authHeaders(token) + headers: authHeaders(token), + data: { nicknames } }) } diff --git a/src/store/modules/users.js b/src/store/modules/users.js index b3d35108..f087d930 100644 --- a/src/store/modules/users.js +++ b/src/store/modules/users.js @@ -76,6 +76,14 @@ const users = { } }, actions: { + async AddRight({ dispatch, getters, state }, { users, right }) { + const usersNicknames = users.map(user => user.nickname) + await addRight(usersNicknames, right, getters.authHost, getters.token) + + dispatch('FetchUsers', { page: state.currentPage }) + // const updatedUser = { ...user, roles: { ...user.roles, [right]: !user.roles[right] }} + // commit('SWAP_USER', updatedUser) + }, async AddTag({ commit, getters }, { users, tag }) { const nicknames = users.map(user => user.nickname) await tagUser(nicknames, [tag], getters.authHost, getters.token) @@ -90,6 +98,14 @@ const users = { await createNewAccount(nickname, email, password, getters.authHost, getters.token) dispatch('FetchUsers', { page: state.currentPage }) }, + async DeleteRight({ dispatch, getters, state }, { users, right }) { + const usersNicknames = users.map(user => user.nickname) + await deleteRight(usersNicknames, right, getters.authHost, getters.token) + + dispatch('FetchUsers', { page: state.currentPage }) + // const updatedUser = { ...user, roles: { ...user.roles, [right]: !user.roles[right] }} + // commit('SWAP_USER', updatedUser) + }, async DeleteUser({ commit, getters, state }, user) { const { data } = await deleteUser(user.nickname, getters.authHost, getters.token) const users = state.fetchedUsers.filter(user => user.nickname !== data) @@ -146,14 +162,6 @@ const users = { const currentFilters = { ...defaultFilters, ...filters } commit('SET_USERS_FILTERS', currentFilters) dispatch('SearchUsers', { query: state.searchQuery, page: 1 }) - }, - async ToggleRight({ commit, getters }, { user, right }) { - user.roles[right] - ? await deleteRight(user.nickname, right, getters.authHost, getters.token) - : await addRight(user.nickname, right, getters.authHost, getters.token) - - const updatedUser = { ...user, roles: { ...user.roles, [right]: !user.roles[right] }} - commit('SWAP_USER', updatedUser) } } } diff --git a/src/views/users/index.vue b/src/views/users/index.vue index 062b23c8..cce607dd 100644 --- a/src/views/users/index.vue +++ b/src/views/users/index.vue @@ -308,7 +308,9 @@ export default { : this.$store.dispatch('AddTag', { users: [user], tag }) }, toggleUserRight(user, right) { - this.$store.dispatch('ToggleRight', { user, right }) + user.roles[right] + ? this.$store.dispatch('DeleteRight', { users: [user], right }) + : this.$store.dispatch('AddRight', { users: [user], right }) } } }