From b13d323af87b42688c5915e7186ff093df08323a Mon Sep 17 00:00:00 2001 From: Lukas Hirt Date: Wed, 9 Sep 2020 13:15:37 +0200 Subject: [PATCH] Add delete action --- ui/components/App.vue | 13 +++++++++-- ui/store/index.js | 52 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/ui/components/App.vue b/ui/components/App.vue index 403a8e870e..1a22af88b2 100644 --- a/ui/components/App.vue +++ b/ui/components/App.vue @@ -80,7 +80,14 @@ export default { }, actions () { - const actions = [] + let actions = [] + const permanentActions = [ + { + id: 'accounts-actions-dropdown-action-delete', + label: this.$gettext('Delete'), + handler: this.deleteAccounts + } + ] const numberOfDisabledAccounts = this.selectedAccounts.filter(account => !account.accountEnabled).length const isAnyAccountDisabled = numberOfDisabledAccounts > 0 const isAnyAccountEnabled = numberOfDisabledAccounts < this.numberOfSelectedAccounts @@ -101,6 +108,8 @@ export default { }) } + actions = actions.concat(permanentActions) + return actions }, @@ -117,7 +126,7 @@ export default { } }, methods: { - ...mapActions('Accounts', ['initialize', 'toggleAccountStatus']), + ...mapActions('Accounts', ['initialize', 'toggleAccountStatus', 'deleteAccounts']), ...mapMutations('Accounts', ['RESET_ACCOUNTS_SELECTION']), setAccountCreationProgress (isInProgress) { diff --git a/ui/store/index.js b/ui/store/index.js index 3dde6ebd13..ff797b1600 100644 --- a/ui/store/index.js +++ b/ui/store/index.js @@ -1,5 +1,10 @@ /* eslint-disable camelcase */ -import { AccountsService_ListAccounts, AccountsService_UpdateAccount, AccountsService_CreateAccount } from '../client/accounts' +import { + AccountsService_ListAccounts, + AccountsService_UpdateAccount, + AccountsService_CreateAccount, + AccountsService_DeleteAccount +} from '../client/accounts' import { RoleService_ListRoles } from '../client/settings' /* eslint-enable camelcase */ import { injectAuthToken } from '../helpers/auth' @@ -60,6 +65,12 @@ const mutations = { PUSH_NEW_ACCOUNT (state, account) { state.accounts.push(account) + }, + + DELETE_ACCOUNT (state, accountId) { + const accountIndex = state.accounts.findIndex(account => account.id === accountId) + + state.accounts.splice(accountIndex, 1) } } @@ -197,6 +208,45 @@ const actions = { status: 'danger' }, { root: true }) } + }, + + async deleteAccounts ({ rootGetters, state, commit, dispatch }) { + const failedAccounts = [] + + injectAuthToken(rootGetters.user.token) + + for (const account of state.selectedAccounts) { + const response = await AccountsService_DeleteAccount({ + $domain: rootGetters.configuration.server, + body: { + id: account.id + } + }) + + if (response.status === 201) { + commit('DELETE_ACCOUNT', account.id) + } else { + failedAccounts.push({ account: account.diisplayName, statusText: response.statusText }) + } + } + + if (failedAccounts.length === 1) { + dispatch('showMessage', { + title: 'Failed to delete account', + desc: failedAccounts[0].statusText, + status: 'danger' + }, { root: true }) + } + + if (failedAccounts.length > 1) { + dispatch('showMessage', { + title: 'Failed to delete accounts', + desc: 'Could not delete multiple accounts', + status: 'danger' + }, { root: true }) + } + + commit('RESET_ACCOUNTS_SELECTION') } }