Extracted actions into seperate mappers function

This commit is contained in:
Angelina Filippova 2019-05-14 16:18:11 +03:00
parent 932d728b8e
commit 90553ba75c

View file

@ -113,80 +113,91 @@ export default {
} }
}, },
methods: { methods: {
mappers() {
return {
grantRight: (right) => () => this.selectedUsers
.filter(user => user.local && !user.roles[right] && this.$store.state.user.id !== user.id)
.map(user => this.$store.dispatch('ToggleRight', { user, right })),
revokeRight: (right) => () => this.selectedUsers
.filter(user => user.local && user.roles[right] && this.$store.state.user.id !== user.id)
.map(user => this.$store.dispatch('ToggleRight', { user, right })),
activate: () => this.selectedUsers
.filter(user => user.deactivated && this.$store.state.user.id !== user.id)
.map(user => this.$store.dispatch('ToggleUserActivation', user.nickname)),
deactivate: () => this.selectedUsers
.filter(user => !user.deactivated && this.$store.state.user.id !== user.id)
.map(user => this.$store.dispatch('ToggleUserActivation', user.nickname)),
remove: () => this.selectedUsers
.filter(user => this.$store.state.user.id !== user.id)
.map(user => this.$store.dispatch('DeleteUser', user)),
addTag: (tag) => () => this.selectedUsers
.filter(user => tag === 'disable_remote_subscription' || tag === 'disable_any_subscription'
? user.local && !user.tags.includes(tag)
: !user.tags.includes(tag)
).map(user => this.$store.dispatch('ToggleTag', { user, tag })),
removeTag: (tag) => () => this.selectedUsers
.filter(user => tag === 'disable_remote_subscription' || tag === 'disable_any_subscription'
? user.local && user.tags.includes(tag)
: user.tags.includes(tag)
).map(user => this.$store.dispatch('ToggleTag', { user, tag }))
}
},
grantRightToMultipleUsers(right) { grantRightToMultipleUsers(right) {
const mapSelectedUsers = () => this.selectedUsers const { grantRight } = this.mappers()
.filter(user => user.local && !user.roles[right] && this.$store.state.user.id !== user.id)
.map(user => this.$store.dispatch('ToggleRight', { user, right }))
this.confirmMessage( this.confirmMessage(
'Are you sure you want to grant ' + right + ' rights to all selected users?', `Are you sure you want to grant ${right} rights to all selected users?`,
mapSelectedUsers grantRight(right)
) )
}, },
revokeRightToMultipleUsers(right) { revokeRightToMultipleUsers(right) {
const mapSelectedUsers = () => this.selectedUsers const { revokeRight } = this.mappers()
.filter(user => user.local && user.roles[right] && this.$store.state.user.id !== user.id)
.map(user => this.$store.dispatch('ToggleRight', { user, right }))
this.confirmMessage( this.confirmMessage(
'Are you sure you want to revoke ' + right + ' rights from all selected users?', `Are you sure you want to revoke ${right} rights from all selected users?`,
mapSelectedUsers revokeRight(right)
) )
}, },
activateMultipleUsers() { activateMultipleUsers() {
const mapSelectedUsers = () => this.selectedUsers const { activate } = this.mappers()
.filter(user => user.deactivated && this.$store.state.user.id !== user.id)
.map(user => this.$store.dispatch('ToggleUserActivation', user.nickname))
this.confirmMessage( this.confirmMessage(
'Are you sure you want to activate accounts of all selected users?', 'Are you sure you want to activate accounts of all selected users?',
mapSelectedUsers activate
) )
}, },
deactivateMultipleUsers() { deactivateMultipleUsers() {
const mapSelectedUsers = () => this.selectedUsers const { deactivate } = this.mappers()
.filter(user => !user.deactivated && this.$store.state.user.id !== user.id)
.map(user => this.$store.dispatch('ToggleUserActivation', user.nickname))
this.confirmMessage( this.confirmMessage(
'Are you sure you want to deactivate accounts of all selected users?', 'Are you sure you want to deactivate accounts of all selected users?',
mapSelectedUsers deactivate
) )
}, },
deleteMultipleUsers() { deleteMultipleUsers() {
const mapSelectedUsers = () => this.selectedUsers const { remove } = this.mappers()
.filter(user => this.$store.state.user.id !== user.id)
.map(user => this.$store.dispatch('DeleteUser', user))
this.confirmMessage( this.confirmMessage(
'Are you sure you want to delete accounts of all selected users?', 'Are you sure you want to delete accounts of all selected users?',
mapSelectedUsers remove
) )
}, },
addTagForMultipleUsers(tag) { addTagForMultipleUsers(tag) {
const mapSelectedUsers = () => this.selectedUsers const { addTag } = this.mappers()
.filter(user => tag === 'disable_remote_subscription' || tag === 'disable_any_subscription'
? user.local && !user.tags.includes(tag)
: !user.tags.includes(tag)
).map(user => this.$store.dispatch('ToggleTag', { user, tag }))
this.confirmMessage( this.confirmMessage(
'Are you sure you want to apply tag to all selected users?', 'Are you sure you want to apply tag to all selected users?',
mapSelectedUsers addTag(tag)
) )
}, },
removeTagFromMultipleUsers(tag) { removeTagFromMultipleUsers(tag) {
const mapSelectedUsers = () => this.selectedUsers const { removeTag } = this.mappers()
.filter(user => tag === 'disable_remote_subscription' || tag === 'disable_any_subscription'
? user.local && user.tags.includes(tag)
: user.tags.includes(tag)
).map(user => this.$store.dispatch('ToggleTag', { user, tag }))
this.confirmMessage( this.confirmMessage(
'Are you sure you want to remove tag from all selected users?', 'Are you sure you want to remove tag from all selected users?',
mapSelectedUsers removeTag(tag)
) )
}, },
confirmMessage(message, mapSelectedUsers) { confirmMessage(message, applyAction) {
this.$confirm(message, { this.$confirm(message, {
confirmButtonText: 'OK', confirmButtonText: 'OK',
cancelButtonText: 'Cancel', cancelButtonText: 'Cancel',
type: 'warning' type: 'warning'
}).then(() => { }).then(() => {
mapSelectedUsers() applyAction()
this.$emit('apply-action') this.$emit('apply-action')
this.$message({ this.$message({
type: 'success', type: 'success',