Commit 575544f6 authored by Fatih Acet's avatar Fatih Acet

IssueNotesRefactor: Slightly refactor toggleAward to remove action.

parent 9869b6b3
...@@ -139,10 +139,9 @@ export default { ...@@ -139,10 +139,9 @@ export default {
return title; return title;
}, },
handleAward(awardName, isAwarded) { handleAward(awardName) {
const data = { const data = {
endpoint: this.toggleAwardPath, endpoint: this.toggleAwardPath,
action: isAwarded ? 'remove' : 'add',
noteId: this.noteId, noteId: this.noteId,
awardName, awardName,
}; };
...@@ -166,7 +165,7 @@ export default { ...@@ -166,7 +165,7 @@ export default {
v-for="(awardList, awardName) in groupedAwards" v-for="(awardList, awardName) in groupedAwards"
:class="getAwardClassBindings(awardList, awardName)" :class="getAwardClassBindings(awardList, awardName)"
:title="awardTitle(awardList)" :title="awardTitle(awardList)"
@click="handleAward(awardName, amIAwarded(awardList))" @click="handleAward(awardName)"
class="btn award-control has-tooltip" class="btn award-control has-tooltip"
data-placement="bottom" data-placement="bottom"
type="button"> type="button">
......
...@@ -85,26 +85,23 @@ const mutations = { ...@@ -85,26 +85,23 @@ const mutations = {
storeState.notes.push(noteData); storeState.notes.push(noteData);
}, },
toggleAward(storeState, data) { toggleAward(storeState, data) {
const { awardName, note, action } = data; const { awardName, note } = data;
const { id, name, username } = window.gl.currentUserData; const { id, name, username } = window.gl.currentUserData;
let index = -1;
if (action === 'add') { note.award_emoji.forEach((a, i) => {
if (a.name === awardName && a.user.id === id) {
index = i;
}
});
if (index > -1) { // if I am awarded, remove my award
note.award_emoji.splice(index, 1);
} else {
note.award_emoji.push({ note.award_emoji.push({
name: awardName, name: awardName,
user: { id, name, username }, user: { id, name, username },
}); });
} else if (action === 'remove') {
let index = -1;
note.award_emoji.forEach((a, i) => {
if (a.name === awardName && a.user.id === id) {
index = i;
}
});
if (index > -1) {
note.award_emoji.splice(index, 1);
}
} }
}, },
}; };
...@@ -189,14 +186,14 @@ const actions = { ...@@ -189,14 +186,14 @@ const actions = {
}); });
}, },
toggleAward(context, data) { toggleAward(context, data) {
const { endpoint, awardName, action, noteId } = data; const { endpoint, awardName, noteId } = data;
const note = context.getters.notesById[noteId]; const note = context.getters.notesById[noteId];
return service return service
.toggleAward(endpoint, { name: awardName }) .toggleAward(endpoint, { name: awardName })
.then(res => res.json()) .then(res => res.json())
.then(() => { .then(() => {
context.commit('toggleAward', { awardName, note, action }); context.commit('toggleAward', { awardName, note });
}); });
}, },
}; };
......
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