Merge branch 'feature/select-multiple-users' of git.pleroma.social:linafilippova/admin-fe; branch 'master' of git.pleroma.social:pleroma/admin-fe into feature/select-multiple-users

This commit is contained in:
Angelina Filippova 2019-05-16 20:59:25 +03:00
commit d3d380a9af
3 changed files with 58 additions and 47 deletions

View file

@ -32,7 +32,7 @@ export async function searchUsers(query, filters, authHost, token, page = 1) {
export async function addRight(nickname, right, authHost, token) {
return await request({
baseURL: baseName(authHost),
url: `/api/pleroma/admin/permission_group/${nickname}/${right}`,
url: `/api/pleroma/admin/users/${nickname}/permission_group/${right}`,
method: 'post',
headers: authHeaders(token)
})
@ -41,7 +41,7 @@ export async function addRight(nickname, right, authHost, token) {
export async function deleteRight(nickname, right, authHost, token) {
return await request({
baseURL: baseName(authHost),
url: `/api/pleroma/admin/permission_group/${nickname}/${right}`,
url: `/api/pleroma/admin/users/${nickname}/permission_group/${right}`,
method: 'delete',
headers: authHeaders(token)
})
@ -50,29 +50,29 @@ export async function deleteRight(nickname, right, authHost, token) {
export async function deleteUser(nickname, authHost, token) {
return await request({
baseURL: baseName(authHost),
url: `/api/pleroma/admin/user.json?nickname=${nickname}`,
url: `/api/pleroma/admin/users?nickname=${nickname}`,
method: 'delete',
headers: authHeaders(token)
})
}
export async function tagUser(nickname, tag, authHost, token) {
export async function tagUser(nicknames, tags, authHost, token) {
return await request({
baseURL: baseName(authHost),
url: '/api/pleroma/admin/users/tag',
method: 'put',
headers: authHeaders(token),
data: { nicknames: [nickname], tags: [tag] }
data: { nicknames, tags }
})
}
export async function untagUser(nickname, tag, authHost, token) {
export async function untagUser(nicknames, tags, authHost, token) {
return await request({
baseURL: baseName(authHost),
url: '/api/pleroma/admin/users/tag',
method: 'delete',
headers: authHeaders(token),
data: { nicknames: [nickname], tags: [tag] }
data: { nicknames, tags }
})
}

View file

@ -25,6 +25,15 @@ const users = {
const updated = state.fetchedUsers.map(user => user.id === updatedUser.id ? updatedUser : user)
state.fetchedUsers = updated.sort((a, b) => a.nickname.localeCompare(b.nickname))
},
SWAP_USERS: (state, users) => {
const usersWithoutSwapped = users.reduce((acc, user) => {
return acc.filter(u => u.id !== user.id)
}, state.fetchedUsers)
state.fetchedUsers = [...usersWithoutSwapped, ...users].sort((a, b) =>
a.nickname.localeCompare(b.nickname)
)
},
SET_COUNT: (state, count) => {
state.totalUsersCount = count
},
@ -42,6 +51,21 @@ const users = {
}
},
actions: {
async AddTag({ commit, getters }, { users, tag }) {
const nicknames = users.map(user => user.nickname)
await tagUser(nicknames, [tag], getters.authHost, getters.token)
commit('SWAP_USERS', users.map(user => ({ ...user, tags: [...user.tags, tag] })))
},
async ClearFilters({ commit, dispatch, state }) {
commit('CLEAR_USERS_FILTERS')
dispatch('SearchUsers', { query: state.searchQuery, page: 1 })
},
async DeleteUser({ commit, getters }, user) {
await deleteUser(user.nickname, getters.authHost, getters.token)
const updatedUser = { ...user, deactivated: true }
commit('SWAP_USER', updatedUser)
},
async FetchUsers({ commit, state, getters }, { page }) {
const filters = Object.keys(state.filters).filter(filter => state.filters[filter]).join()
const response = await fetchUsers(filters, getters.authHost, getters.token, page)
@ -50,10 +74,11 @@ const users = {
loadUsers(commit, page, response.data)
},
async ToggleUserActivation({ commit, getters }, nickname) {
const response = await toggleUserActivation(nickname, getters.authHost, getters.token)
async RemoveTag({ commit, getters }, { users, tag }) {
const nicknames = users.map(user => user.nickname)
await untagUser(nicknames, [tag], getters.authHost, getters.token)
commit('SWAP_USER', response.data)
commit('SWAP_USERS', users.map(user => ({ ...user, tags: user.tags.filter(userTag => userTag !== tag) })))
},
async SearchUsers({ commit, dispatch, state, getters }, { query, page }) {
if (query.length === 0) {
@ -69,6 +94,10 @@ const users = {
loadUsers(commit, page, response.data)
}
},
async ToggleUserActivation({ commit, getters }, nickname) {
const { data } = await toggleUserActivation(nickname, getters.authHost, getters.token)
commit('SWAP_USER', data)
},
async ToggleUsersFilter({ commit, dispatch, state }, filters) {
const defaultFilters = {
local: false,
@ -80,10 +109,6 @@ const users = {
commit('SET_USERS_FILTERS', currentFilters)
dispatch('SearchUsers', { query: state.searchQuery, page: 1 })
},
async ClearFilters({ commit, dispatch, state }) {
commit('CLEAR_USERS_FILTERS')
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)
@ -91,22 +116,6 @@ const users = {
const updatedUser = { ...user, roles: { ...user.roles, [right]: !user.roles[right] }}
commit('SWAP_USER', updatedUser)
},
async DeleteUser({ commit, getters }, user) {
await deleteUser(user.nickname, getters.authHost, getters.token)
const updatedUser = { ...user, deactivated: true }
commit('SWAP_USER', updatedUser)
},
async ToggleTag({ commit, getters }, { user, tag }) {
if (user.tags.includes(tag)) {
await untagUser(user.nickname, tag, getters.authHost, getters.token)
const updatedUser = { ...user, tags: user.tags.filter(userTag => userTag !== tag) }
commit('SWAP_USER', updatedUser)
} else {
await tagUser(user.nickname, tag, getters.authHost, getters.token)
const updatedUser = { ...user, tags: [...user.tags, tag] }
commit('SWAP_USER', updatedUser)
}
}
}
}

View file

@ -188,9 +188,21 @@ export default {
this.$store.dispatch('FetchUsers', { page: 1 })
},
methods: {
activationIcon(status) {
return status ? 'el-icon-error' : 'el-icon-success'
},
clearSelection() {
this.$refs.usersTable.clearSelection()
},
getFirstLetter(str) {
return str.charAt(0).toUpperCase()
},
handleDeactivation({ nickname }) {
this.$store.dispatch('ToggleUserActivation', nickname)
},
handleDeletion(user) {
this.$store.dispatch('DeleteUser', user)
},
handlePageChange(page) {
const searchQuery = this.$store.state.users.searchQuery
if (searchQuery === '') {
@ -202,29 +214,19 @@ export default {
handleSelectionChange(value) {
this.$data.selectedUsers = value
},
showDeactivatedButton(id) {
return this.$store.state.user.id !== id
},
showAdminAction({ local, id }) {
return local && this.showDeactivatedButton(id)
},
activationIcon(status) {
return status ? 'el-icon-error' : 'el-icon-success'
showDeactivatedButton(id) {
return this.$store.state.user.id !== id
},
toggleTag(user, tag) {
user.tags.includes(tag)
? this.$store.dispatch('RemoveTag', { users: [user], tag })
: this.$store.dispatch('AddTag', { users: [user], tag })
},
toggleUserRight(user, right) {
this.$store.dispatch('ToggleRight', { user, right })
},
handleDeletion(user) {
this.$store.dispatch('DeleteUser', user)
},
toggleTag(user, tag) {
this.$store.dispatch('ToggleTag', { user, tag })
},
getFirstLetter(str) {
return str.charAt(0).toUpperCase()
},
clearSelection() {
this.$refs.usersTable.clearSelection()
}
}
}