diff --git a/lib/ops/report-store-ops.js b/lib/ops/report-store-ops.js --- a/lib/ops/report-store-ops.js +++ b/lib/ops/report-store-ops.js @@ -46,4 +46,26 @@ return processedReports; } -export { processReportStoreOperations }; +function convertReportsToReplaceReportOps( + reports: $ReadOnlyArray, +): $ReadOnlyArray { + return reports.map(report => ({ + type: 'replace_report', + payload: { report }, + })); +} + +function convertReportsToRemoveReportsOperation( + reports: $ReadOnlyArray, +): RemoveQueuedReportsOperation { + return { + type: 'remove_reports', + payload: { ids: reports.map(report => report.id) }, + }; +} + +export { + processReportStoreOperations, + convertReportsToReplaceReportOps, + convertReportsToRemoveReportsOperation, +}; diff --git a/lib/reducers/report-store-reducer.js b/lib/reducers/report-store-reducer.js --- a/lib/reducers/report-store-reducer.js +++ b/lib/reducers/report-store-reducer.js @@ -12,6 +12,11 @@ logInActionTypes, } from '../actions/user-actions.js'; import type { ReportStoreOperation } from '../ops/report-store-ops.js'; +import { + convertReportsToRemoveReportsOperation, + convertReportsToReplaceReportOps, + processReportStoreOperations, +} from '../ops/report-store-ops.js'; import { isStaff } from '../shared/staff-utils.js'; import type { BaseAction } from '../types/redux-types.js'; import { @@ -34,6 +39,10 @@ reportStore: ReportStore, reportStoreOperations: $ReadOnlyArray, } { + const newReports = newInconsistencies.filter(report => + isReportEnabled(report, state.enabledReports), + ); + const updatedReports = newInconsistencies.length > 0 ? [...state.queuedReports, ...newInconsistencies].filter(report => @@ -43,15 +52,29 @@ if (action.type === updateReportsEnabledActionType) { const newEnabledReports = { ...state.enabledReports, ...action.payload }; - const filteredReports = updatedReports.filter(report => + const newFilteredReports = newReports.filter(report => isReportEnabled(report, newEnabledReports), ); + const reportsToRemove = state.queuedReports.filter( + report => !isReportEnabled(report, newEnabledReports), + ); + + const reportStoreOperations: $ReadOnlyArray = [ + convertReportsToRemoveReportsOperation(reportsToRemove), + ...convertReportsToReplaceReportOps(newFilteredReports), + ]; + + const queuedReports = processReportStoreOperations( + state.queuedReports, + reportStoreOperations, + ); + return { reportStore: { - queuedReports: filteredReports, + queuedReports, enabledReports: newEnabledReports, }, - reportStoreOperations: [], + reportStoreOperations, }; } else if ( action.type === logOutActionTypes.success ||