From 80b650c2a989a1daf5d4b2515a2ae8cbc24ee714 Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Thu, 25 Oct 2018 11:27:54 +0200 Subject: [PATCH] Delete reports when downvoting them --- src/store/actions.js | 11 +++++++++-- src/store/mutations-types.js | 1 + src/store/mutations.js | 6 ++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/store/actions.js b/src/store/actions.js index 32189ef..7ac315d 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -4,6 +4,7 @@ import { distance } from '@/tools'; import i18n from '@/i18n'; import { + DELETE_REPORT, HAS_VIBRATED_ONCE, INTRO_WAS_SEEN, INTRO_WAS_UNSEEN, @@ -27,7 +28,7 @@ export function fetchReports({ commit, state }) { commit(IS_LOADING); return api.getActiveReports() .then((reports) => { - // Filter reports which are too far + // Filter reports which are too far or have too many downvotes const reportsToCommit = reports.filter( (report) => { if (report.attributes.downvotes >= constants.REPORT_DOWNVOTES_THRESHOLD) { @@ -56,7 +57,13 @@ export function downvote({ commit }, { id }) { // Hide details commit(SHOW_REPORT_DETAILS, { id: null, userAsked: null }); return api.downvote(id) - .then(report => commit(PUSH_REPORT, { report })); + .then((report) => { + if (report.attributes.downvotes >= constants.REPORT_DOWNVOTES_THRESHOLD) { + commit(DELETE_REPORT, { report }); + } else { + commit(PUSH_REPORT, { report }); + } + }); } export function upvote({ commit }, { id }) { diff --git a/src/store/mutations-types.js b/src/store/mutations-types.js index f265044..cf4617d 100644 --- a/src/store/mutations-types.js +++ b/src/store/mutations-types.js @@ -1,3 +1,4 @@ +export const DELETE_REPORT = 'DELETE_REPORT'; export const HAS_VIBRATED_ONCE = 'HAS_VIBRATED_ONCE'; export const INTRO_WAS_SEEN = 'INTRO_WAS_SEEN'; export const INTRO_WAS_UNSEEN = 'INTRO_WAS_UNSEEN'; diff --git a/src/store/mutations.js b/src/store/mutations.js index 6ae21d7..d252d13 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -128,6 +128,12 @@ export const initialState = { }; export const mutations = { + [types.DELETE_REPORT](state, { report }) { + const reportIndex = state.reports.findIndex(item => item.id === report.id); + if (reportIndex !== -1) { + Vue.delete(state.reports, reportIndex); + } + }, [types.HAS_VIBRATED_ONCE](state) { state.hasVibratedOnce = true; },