Delete reports when downvoting them

This commit is contained in:
Lucas Verney 2018-10-25 11:27:54 +02:00
parent 34e0fec016
commit 80b650c2a9
3 changed files with 16 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import { distance } from '@/tools';
import i18n from '@/i18n'; import i18n from '@/i18n';
import { import {
DELETE_REPORT,
HAS_VIBRATED_ONCE, HAS_VIBRATED_ONCE,
INTRO_WAS_SEEN, INTRO_WAS_SEEN,
INTRO_WAS_UNSEEN, INTRO_WAS_UNSEEN,
@ -27,7 +28,7 @@ export function fetchReports({ commit, state }) {
commit(IS_LOADING); commit(IS_LOADING);
return api.getActiveReports() return api.getActiveReports()
.then((reports) => { .then((reports) => {
// Filter reports which are too far // Filter reports which are too far or have too many downvotes
const reportsToCommit = reports.filter( const reportsToCommit = reports.filter(
(report) => { (report) => {
if (report.attributes.downvotes >= constants.REPORT_DOWNVOTES_THRESHOLD) { if (report.attributes.downvotes >= constants.REPORT_DOWNVOTES_THRESHOLD) {
@ -56,7 +57,13 @@ export function downvote({ commit }, { id }) {
// Hide details // Hide details
commit(SHOW_REPORT_DETAILS, { id: null, userAsked: null }); commit(SHOW_REPORT_DETAILS, { id: null, userAsked: null });
return api.downvote(id) 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 }) { export function upvote({ commit }, { id }) {

View File

@ -1,3 +1,4 @@
export const DELETE_REPORT = 'DELETE_REPORT';
export const HAS_VIBRATED_ONCE = 'HAS_VIBRATED_ONCE'; export const HAS_VIBRATED_ONCE = 'HAS_VIBRATED_ONCE';
export const INTRO_WAS_SEEN = 'INTRO_WAS_SEEN'; export const INTRO_WAS_SEEN = 'INTRO_WAS_SEEN';
export const INTRO_WAS_UNSEEN = 'INTRO_WAS_UNSEEN'; export const INTRO_WAS_UNSEEN = 'INTRO_WAS_UNSEEN';

View File

@ -128,6 +128,12 @@ export const initialState = {
}; };
export const mutations = { 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) { [types.HAS_VIBRATED_ONCE](state) {
state.hasVibratedOnce = true; state.hasVibratedOnce = true;
}, },