Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/components/MessagesList/MessagesGroup/Message/Message.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,10 @@ describe('Message.vue', () => {
test('dispatches store action upon picking an emoji from the emojipicker', () => {
const addReactionToMessageAction = jest.fn()
const userHasReactedGetter = jest.fn().mockReturnValue(() => false)
const reactionsLoadedGetter = jest.fn().mockReturnValue(() => true)
testStoreConfig.modules.quoteReplyStore.actions.addReactionToMessage = addReactionToMessageAction
testStoreConfig.modules.messagesStore.getters.userHasReacted = userHasReactedGetter
testStoreConfig.modules.messagesStore.getters.reactionsLoaded = reactionsLoadedGetter

store = new Store(testStoreConfig)

Expand All @@ -821,11 +823,6 @@ describe('Message.vue', () => {
stubs: {
EmojiPicker,
},
data() {
return {
detailedReactionsRequested: true,
}
},
})

const emojiPicker = wrapper.findComponent(EmojiPicker)
Expand All @@ -844,20 +841,17 @@ describe('Message.vue', () => {
test('dispatches store action to remove an emoji upon clicking reaction button', async () => {
const removeReactionFromMessageAction = jest.fn()
const userHasReactedGetter = jest.fn().mockReturnValue(() => true)
const reactionsLoadedGetter = jest.fn().mockReturnValue(() => true)
testStoreConfig.modules.quoteReplyStore.actions.removeReactionFromMessage = removeReactionFromMessageAction
testStoreConfig.modules.messagesStore.getters.userHasReacted = userHasReactedGetter
testStoreConfig.modules.messagesStore.getters.reactionsLoaded = reactionsLoadedGetter

store = new Store(testStoreConfig)

const wrapper = shallowMount(Message, {
localVue,
store,
propsData: messageProps,
data() {
return {
detailedReactionsRequested: true,
}
},
})

// Click reaction button upon having already reacted
Expand Down
19 changes: 14 additions & 5 deletions src/components/MessagesList/MessagesGroup/Message/Message.vue
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ export default {
isActionMenuOpen: false,
isEmojiPickerOpen: false,
isReactionsMenuOpen: false,
detailedReactionsRequested: false,
detailedReactionsLoading: false,
}
},

Expand Down Expand Up @@ -568,6 +568,10 @@ export default {
detailedReactions() {
return this.$store.getters.reactions(this.token, this.id)
},

detailedReactionsLoaded() {
return this.$store.getters.reactionsLoaded(this.token, this.id)
},
},

watch: {
Expand Down Expand Up @@ -625,7 +629,7 @@ export default {
},

handleReactionsMouseOver() {
if (this.hasReactions && !this.detailedReactionsRequested) {
if (this.hasReactions && !this.detailedReactionsLoaded) {
this.getReactions()
}
},
Expand All @@ -637,24 +641,29 @@ export default {
},

async getReactions() {
if (this.detailedReactionsLoading) {
// FIXME not sure how to await the other execution
}

try {
/**
* Get reaction details when the message is hovered for the first
* time. After that we rely on system messages to update the
* reactions.
*/
this.detailedReactionsRequested = true
this.detailedReactionsLoading = true
await this.$store.dispatch('getReactions', {
token: this.token,
messageId: this.id,
})
this.detailedReactionsLoading = false
} catch {
this.detailedReactionsRequested = false
this.detailedReactionsLoading = false
}
},

async handleReactionClick(clickedEmoji) {
if (!this.detailedReactionsRequested) {
if (!this.detailedReactionsLoaded) {
await this.getReactions()
}
// Check if current user has already added this reaction to the message
Expand Down
7 changes: 7 additions & 0 deletions src/store/messagesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,13 @@ const actions = {
})
}

if (message.systemMessage === 'reaction' || message.systemMessage === 'reaction_revoked') {
context.commit('resetReactions', {
token: message.token,
messageId: message.parent,
})
}

context.commit('addMessage', message)
},

Expand Down
15 changes: 15 additions & 0 deletions src/store/reactionsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ const getters = {
}
},

reactionsLoaded: (state) => (token, messageId) => {
if (state.reactions?.[token]?.[messageId]) {
return true
} else {
return false
}
},

// Checks if a user has already reacted to a message with a particular reaction
userHasReacted: (state) => (actorType, actorId, token, messageId, reaction) => {
if (!state?.reactions?.[token]?.[messageId]?.[reaction]) {
Expand All @@ -60,6 +68,13 @@ const mutations = {
}
Vue.set(state.reactions[token], messageId, reactions)
},

resetReactions(state, { token, messageId }) {
if (!state.reactions[token]) {
Vue.set(state.reactions, token, {})
}
Vue.delete(state.reactions[token], messageId)
},
}

const actions = {
Expand Down