Commit 7b093581 authored by Filipa Lacerda's avatar Filipa Lacerda

[ci skip] Fix emoji being posted twice originating an error

parent d8ebcb74
...@@ -119,6 +119,7 @@ ...@@ -119,6 +119,7 @@
class="note timeline-entry" class="note timeline-entry"
:id="noteAnchorId" :id="noteAnchorId"
:class="classNameBindings" :class="classNameBindings"
:data-award-url="note.toggle_award_path"
:note-id="note.id"> :note-id="note.id">
<div class="timeline-entry-inner"> <div class="timeline-entry-inner">
<div class="timeline-icon"> <div class="timeline-icon">
......
...@@ -52,7 +52,12 @@ ...@@ -52,7 +52,12 @@
// We need to do this otherwise we will render the same emoji over and over again. // We need to do this otherwise we will render the same emoji over and over again.
groupedAwards() { groupedAwards() {
const awards = this.awards.reduce((acc, award) => { const awards = this.awards.reduce((acc, award) => {
Object.assign(acc, {[award.name]: [award]}); if (acc.hasOwnProperty(award.name)) {
acc[award.name].push(award);
} else {
Object.assign(acc, {[award.name]: [award]});
}
return acc; return acc;
}, {}); }, {});
...@@ -67,6 +72,7 @@ ...@@ -67,6 +72,7 @@
orderedAwards.thumbsdown = thumbsdown; orderedAwards.thumbsdown = thumbsdown;
delete awards.thumbsdown; delete awards.thumbsdown;
} }
return Object.assign({}, orderedAwards, awards); return Object.assign({}, orderedAwards, awards);
}, },
isAuthoredByMe() { isAuthoredByMe() {
...@@ -75,7 +81,7 @@ ...@@ -75,7 +81,7 @@
}, },
methods: { methods: {
...mapActions([ ...mapActions([
'toggleAward', 'toggleAwardRequest',
]), ]),
getAwardHTML(name) { getAwardHTML(name) {
return Emoji.glEmojiTag(name); return Emoji.glEmojiTag(name);
...@@ -147,7 +153,7 @@ ...@@ -147,7 +153,7 @@
awardName: awardName === "100" ? 100: awardName, awardName: awardName === "100" ? 100: awardName,
}; };
this.toggleAward(data) this.toggleAwardRequest(data)
.catch(() => Flash('Something went wrong on our end.')); .catch(() => Flash('Something went wrong on our end.'));
}, },
}, },
......
...@@ -99,10 +99,8 @@ ...@@ -99,10 +99,8 @@
bindEventHubListeners() { bindEventHubListeners() {
this.$el.parentElement.addEventListener('toggleAward', (event) => { this.$el.parentElement.addEventListener('toggleAward', (event) => {
const { awardName, noteId } = event.detail; const { awardName, noteId } = event.detail;
const endpoint = this.notesById[noteId].toggle_award_path; this.actionToggleAward({ awardName, noteId })
this.actionToggleAward({ endpoint, awardName, noteId })
.catch((error) => Flash('Something went wrong on our end.'));
}); });
// JQuery is needed here because it is a custom event being dispatched with jQuery. // JQuery is needed here because it is a custom event being dispatched with jQuery.
......
...@@ -174,7 +174,7 @@ export const poll = ({ commit, state, getters }) => { ...@@ -174,7 +174,7 @@ export const poll = ({ commit, state, getters }) => {
if (!Visibility.hidden()) { if (!Visibility.hidden()) {
eTagPoll.makeRequest(); eTagPoll.makeRequest();
} else { } else {
this.service.poll(requestData); service.poll(requestData);
} }
Visibility.change(() => { Visibility.change(() => {
...@@ -186,38 +186,17 @@ export const poll = ({ commit, state, getters }) => { ...@@ -186,38 +186,17 @@ export const poll = ({ commit, state, getters }) => {
}); });
}; };
export const toggleAward = ({ commit, getters, dispatch }, data) => { export const toggleAward = ({ commit, state, getters, dispatch }, { awardName, noteId }) => {
const { endpoint, awardName, noteId, skipMutalityCheck } = data; commit(types.TOGGLE_AWARD, { awardName, note: getters.notesById[noteId] });
const note = getters.notesById[noteId]; };
export const toggleAwardRequest = ({ commit, getters, dispatch }, data) => {
const { endpoint, awardName } = data;
return service return service
.toggleAward(endpoint, { name: awardName }) .toggleAward(endpoint, { name: awardName })
.then(res => res.json()) .then(res => res.json())
.then(() => { .then(() => {
commit(types.TOGGLE_AWARD, { awardName, note }); dispatch('toggleAward', data);
if (!skipMutalityCheck &&
(awardName === constants.EMOJI_THUMBSUP || awardName === constants.EMOJI_THUMBSDOWN)) {
const counterAward = awardName === constants.EMOJI_THUMBSUP ?
constants.EMOJI_THUMBSDOWN :
constants.EMOJI_THUMBSUP;
const targetNote = getters.notesById[noteId];
let noteHasAwardByCurrentUser = false;
targetNote.award_emoji.forEach((a) => {
if (a.name === counterAward && a.user.id === window.gon.current_user_id) {
noteHasAwardByCurrentUser = true;
}
});
if (noteHasAwardByCurrentUser) {
Object.assign(data, { awardName: counterAward });
Object.assign(data, { skipMutalityCheck: true });
dispatch(types.TOGGLE_AWARD, data);
}
}
}); });
}; };
......
...@@ -102,16 +102,13 @@ export default { ...@@ -102,16 +102,13 @@ export default {
[types.TOGGLE_AWARD](state, data) { [types.TOGGLE_AWARD](state, data) {
const { awardName, note } = data; const { awardName, note } = data;
const { id, name, username } = state.userData; const { id, name, username } = state.userData;
let index = -1;
note.award_emoji.forEach((a, i) => { const hasEmojiAwardedByCurrentUser = note.award_emoji
if (a.name === awardName && a.user.id === id) { .filter(emoji => emoji.name === data.awardName && emoji.user.id === id);
index = i;
}
});
if (index > -1) { // If current user has awarded this emoji, remove it. if (hasEmojiAwardedByCurrentUser.length) {
note.award_emoji.splice(index, 1); // If current user has awarded this emoji, remove it.
note.award_emoji.splice(note.award_emoji.indexOf(hasEmojiAwardedByCurrentUser[0]), 1);
} else { } else {
note.award_emoji.push({ note.award_emoji.push({
name: awardName, name: awardName,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment