Skip to content

Commit 729765a

Browse files
committed
Drop the confirmation just disable the button
Much easier
1 parent df0949a commit 729765a

File tree

5 files changed

+63
-74
lines changed

5 files changed

+63
-74
lines changed

components/Global/Elements/BlockButton/BlockButton.test.js

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const propsData = {
1010
foreignEntity: {
1111
_id: 4711,
1212
name: 'author'
13-
}
13+
},
14+
isFollowing: false
1415
}
1516

1617
const mocks = { $t: () => {} }
@@ -23,6 +24,7 @@ describe('BlockButton.vue', () => {
2324

2425
beforeEach(() => {
2526
getters = {
27+
'feathers-vuex-usersettings/isBlacklisted': () => () => false,
2628
'feathers-vuex-usersettings/isPending': () => false,
2729
'feathers-vuex-usersettings/current': () => { return { blacklist: [] } }
2830
}
@@ -70,11 +72,7 @@ describe('BlockButton.vue', () => {
7072

7173
describe('is blacklisted', () => {
7274
beforeEach(() => {
73-
getters['feathers-vuex-usersettings/current'] = () => {
74-
return {
75-
blacklist: [4711]
76-
}
77-
}
75+
getters['feathers-vuex-usersettings/isBlacklisted'] = () => () => true
7876
store = new Vuex.Store({state: {}, getters, actions})
7977
wrapper = shallowMount(BlockButton, { store, localVue, propsData, mocks })
8078
})
@@ -100,39 +98,29 @@ describe('BlockButton.vue', () => {
10098
expect(actions['feathers-vuex-usersettings/toggleBlacklist']).toHaveBeenCalled()
10199
})
102100

103-
describe('given a confirmation callback', () => {
104-
let props
105-
const testCaseAction = () => {
106-
props = Object.assign({}, propsData, {
107-
confirmation: jest.fn()
108-
})
109-
wrapper = mount(BlockButton, { store,
110-
localVue,
111-
mocks: { $t: () => {}, $snackbar: { open () { } } },
112-
propsData: props
101+
describe('isFollowed === true', () => {
102+
beforeEach(() => {
103+
const props = Object.assign({}, propsData, {
104+
isFollowing: true
113105
})
114-
wrapper.find('button').trigger('click')
115-
}
106+
wrapper = shallowMount(BlockButton, { store, localVue, propsData: props, mocks })
107+
})
116108

117-
describe('not blacklisted', () => {
118-
test('click just unblocks without confirmation', () => {
119-
testCaseAction()
120-
expect(props.confirmation).toHaveBeenCalledTimes(1)
121-
})
109+
test('is disabled', () => {
110+
expect(wrapper.find('hc-button-stub').attributes().disabled).toEqual('true')
122111
})
112+
})
123113

124-
describe('is blacklisted', () => {
125-
beforeEach(() => {
126-
getters['feathers-vuex-usersettings/current'] = () => { return { blacklist: [4711] } }
127-
store = new Vuex.Store({
128-
state: {}, getters, actions
129-
})
114+
describe('isFollowed === false', () => {
115+
beforeEach(() => {
116+
const props = Object.assign({}, propsData, {
117+
isFollowing: false
130118
})
119+
wrapper = shallowMount(BlockButton, { store, localVue, propsData: props, mocks })
120+
})
131121

132-
test('click confirms before blocking', () => {
133-
testCaseAction()
134-
expect(props.confirmation).not.toHaveBeenCalled()
135-
})
122+
test('is disabled', () => {
123+
expect(wrapper.find('hc-button-stub').attributes().disabled).toEqual(undefined)
136124
})
137125
})
138126
})

components/Global/Elements/BlockButton/BlockButton.vue

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
22
<hc-button color="button"
3-
:disabled="isPending"
3+
:disabled="isPending || isFollowing"
44
:isLoading="isPending"
5-
@click="click">
5+
@click="toggleBlacklist">
66
<template v-if="isBlacklisted()">
77
<hc-icon icon="ban" :class="['icon-left', 'is-danger']" /> {{ $t('component.blacklist.buttonLabelUnblock') }}
88
</template>
@@ -28,25 +28,18 @@ export default {
2828
type: Object,
2929
required: true
3030
},
31-
confirmation: {
32-
type: Function
31+
isFollowing: {
32+
required: true
3333
}
3434
},
3535
computed: {
3636
...mapGetters({
3737
isPending: 'feathers-vuex-usersettings/isPending',
38-
currentUserSettings: 'feathers-vuex-usersettings/current'
3938
})
4039
},
4140
methods: {
4241
isBlacklisted () {
43-
let { blacklist } = this.currentUserSettings || {}
44-
return blacklist && blacklist.includes(this.foreignEntity._id)
45-
},
46-
async click(){
47-
if (this.confirmation && !this.isBlacklisted()) {
48-
return await this.confirmation(this.toggleBlacklist)
49-
} else return await this.toggleBlacklist()
42+
return this.$store.getters['feathers-vuex-usersettings/isBlacklisted'](this.foreignEntity)
5043
},
5144
async toggleBlacklist() {
5245
let message

components/Global/Elements/Follow/FollowButtons.test.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { shallowMount, mount, createLocalVue } from '@vue/test-utils'
1+
import { shallowMount, createLocalVue } from '@vue/test-utils'
22
import Vuex from 'vuex'
33
import FollowButtons from './FollowButtons'
44

@@ -33,7 +33,7 @@ describe('FollowButtons.vue', () => {
3333
},
3434
'feathers-vuex-usersettings/current': () => { return {} },
3535
'feathers-vuex-usersettings/isPending': () => false,
36-
'auth/user': () => { return currentUser },
36+
'auth/user': () => { return currentUser }
3737
}
3838
actions = {
3939
'connections/syncFollow': () => {
@@ -54,14 +54,31 @@ describe('FollowButtons.vue', () => {
5454
propsData.showButtons = true
5555
})
5656

57-
describe('click', () => {
58-
test('calls `click` method', () => {
59-
const methods = {
60-
click: jest.fn()
61-
}
62-
wrapper = mount(FollowButtons, { store, localVue, propsData, mocks, methods })
63-
wrapper.find('button').trigger('click')
64-
expect(methods.click).toHaveBeenCalled()
57+
describe('entity is not blacklisted', () => {
58+
beforeEach(() => {
59+
getters['feathers-vuex-usersettings/isBlacklisted'] = () => () => false
60+
store = new Vuex.Store({
61+
state: {}, getters, actions
62+
})
63+
})
64+
65+
test('follow button is enabled', () => {
66+
wrapper = shallowMount(FollowButtons, { store, localVue, propsData, mocks })
67+
expect(wrapper.find('hc-button-stub').attributes().disabled).toEqual(undefined)
68+
})
69+
})
70+
71+
describe('entity is blacklisted', () => {
72+
beforeEach(() => {
73+
getters['feathers-vuex-usersettings/isBlacklisted'] = () => () => true
74+
store = new Vuex.Store({
75+
state: {}, getters, actions
76+
})
77+
})
78+
79+
test('follow button is disabled', () => {
80+
wrapper = shallowMount(FollowButtons, { store, localVue, propsData, mocks })
81+
expect(wrapper.find('hc-button-stub').attributes().disabled).toEqual('true')
6582
})
6683
})
6784
})

components/Global/Elements/Follow/FollowButtons.vue

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
<div class="column control has-text-centered">
1919
<hc-button color="button is-fullwidth"
2020
:class="{'is-primary': !follow.isPending && !follow.isFollowing}"
21-
@click="click"
22-
:disabled="follow.isPending"
21+
@click="toggleFollow"
22+
:disabled="follow.isPending || this.isBlacklisted()"
2323
:isLoading="follow.isPending">
2424
<template v-if="follow.isFollowing">
2525
<hc-icon icon="check" class="icon-left" /> {{ $t('component.follow.buttonLabelUnFollow') }}
@@ -30,7 +30,7 @@
3030
</hc-button>
3131
</div>
3232
<div v-if="service === 'users'" class="column is-mobile field has-text-centered">
33-
<hc-block-button :foreignEntity="entity" :confirmation="confirmUnfollow"/>
33+
<hc-block-button :foreignEntity="entity" :isFollowing="follow.isFollowing"/>
3434
</div>
3535
</div>
3636
</div>
@@ -84,22 +84,8 @@
8484
})
8585
},
8686
methods: {
87-
confirmUnfollow(next){
88-
const message = this.$t('component.blacklist.confirmUnfollowMessage', {
89-
name: this.entity.name || this.$t('component.contribution.creatorUnknown')
90-
})
91-
this.$dialog.confirm({
92-
title: this.$t('component.blacklist.confirmUnfollowTitle'),
93-
message,
94-
confirmText: this.$t('button.yes'),
95-
cancelText: this.$t('button.cancel'),
96-
type: 'is-danger',
97-
hasIcon: true,
98-
onConfirm: () => { next() }
99-
})
100-
},
101-
async click(){
102-
return this.toggleFollow();
87+
isBlacklisted(){
88+
return this.$store.getters['feathers-vuex-usersettings/isBlacklisted'](this.entity)
10389
},
10490
async toggleFollow () {
10591
if (this.follow._id) {

store/services/usersettings.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ let servicePlugin = (feathersClient) => {
99
blacklist: []
1010
},
1111
getters: {
12+
isBlacklisted: (state) => (entity) => {
13+
let current = state.copy
14+
let { blacklist } = current || {}
15+
return blacklist && blacklist.includes(entity._id)
16+
},
1217
isPending: (state) => {
1318
return (
1419
state.current ||

0 commit comments

Comments
 (0)