Changeset View
Changeset View
Standalone View
Standalone View
lib/reducers/report-store-reducer.js
// @flow | // @flow | ||||
import { | import { | ||||
sendReportActionTypes, | sendReportActionTypes, | ||||
sendReportsActionTypes, | sendReportsActionTypes, | ||||
queueReportsActionType, | queueReportsActionType, | ||||
} from '../actions/report-actions.js'; | } from '../actions/report-actions.js'; | ||||
import { siweAuthActionTypes } from '../actions/siwe-actions.js'; | import { siweAuthActionTypes } from '../actions/siwe-actions.js'; | ||||
import { | import { | ||||
logOutActionTypes, | logOutActionTypes, | ||||
deleteAccountActionTypes, | deleteAccountActionTypes, | ||||
logInActionTypes, | logInActionTypes, | ||||
} from '../actions/user-actions.js'; | } from '../actions/user-actions.js'; | ||||
import type { ReportStoreOperation } from '../ops/report-store-ops.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 { isStaff } from '../shared/staff-utils.js'; | ||||
import type { BaseAction } from '../types/redux-types.js'; | import type { BaseAction } from '../types/redux-types.js'; | ||||
import { | import { | ||||
type ReportStore, | type ReportStore, | ||||
defaultEnabledReports, | defaultEnabledReports, | ||||
defaultDevEnabledReports, | defaultDevEnabledReports, | ||||
type ClientReportCreationRequest, | type ClientReportCreationRequest, | ||||
} from '../types/report-types.js'; | } from '../types/report-types.js'; | ||||
import { setNewSessionActionType } from '../utils/action-utils.js'; | import { setNewSessionActionType } from '../utils/action-utils.js'; | ||||
import { isDev } from '../utils/dev-utils.js'; | import { isDev } from '../utils/dev-utils.js'; | ||||
import { isReportEnabled } from '../utils/report-utils.js'; | import { isReportEnabled } from '../utils/report-utils.js'; | ||||
export const updateReportsEnabledActionType = 'UPDATE_REPORTS_ENABLED'; | export const updateReportsEnabledActionType = 'UPDATE_REPORTS_ENABLED'; | ||||
export default function reduceReportStore( | export default function reduceReportStore( | ||||
state: ReportStore, | state: ReportStore, | ||||
action: BaseAction, | action: BaseAction, | ||||
newInconsistencies: $ReadOnlyArray<ClientReportCreationRequest>, | newInconsistencies: $ReadOnlyArray<ClientReportCreationRequest>, | ||||
): { | ): { | ||||
reportStore: ReportStore, | reportStore: ReportStore, | ||||
reportStoreOperations: $ReadOnlyArray<ReportStoreOperation>, | reportStoreOperations: $ReadOnlyArray<ReportStoreOperation>, | ||||
} { | } { | ||||
const newReports = newInconsistencies.filter(report => | |||||
isReportEnabled(report, state.enabledReports), | |||||
); | |||||
const updatedReports = | const updatedReports = | ||||
newInconsistencies.length > 0 | newInconsistencies.length > 0 | ||||
? [...state.queuedReports, ...newInconsistencies].filter(report => | ? [...state.queuedReports, ...newInconsistencies].filter(report => | ||||
isReportEnabled(report, state.enabledReports), | isReportEnabled(report, state.enabledReports), | ||||
) | ) | ||||
: state.queuedReports; | : state.queuedReports; | ||||
if (action.type === updateReportsEnabledActionType) { | if (action.type === updateReportsEnabledActionType) { | ||||
const newEnabledReports = { ...state.enabledReports, ...action.payload }; | const newEnabledReports = { ...state.enabledReports, ...action.payload }; | ||||
const filteredReports = updatedReports.filter(report => | const newFilteredReports = newReports.filter(report => | ||||
isReportEnabled(report, newEnabledReports), | isReportEnabled(report, newEnabledReports), | ||||
); | ); | ||||
const reportsToRemove = state.queuedReports.filter( | |||||
report => !isReportEnabled(report, newEnabledReports), | |||||
); | |||||
const reportStoreOperations: $ReadOnlyArray<ReportStoreOperation> = [ | |||||
convertReportsToRemoveReportsOperation(reportsToRemove), | |||||
...convertReportsToReplaceReportOps(newFilteredReports), | |||||
]; | |||||
const queuedReports = processReportStoreOperations( | |||||
state.queuedReports, | |||||
reportStoreOperations, | |||||
); | |||||
return { | return { | ||||
reportStore: { | reportStore: { | ||||
queuedReports: filteredReports, | queuedReports, | ||||
enabledReports: newEnabledReports, | enabledReports: newEnabledReports, | ||||
}, | }, | ||||
reportStoreOperations: [], | reportStoreOperations, | ||||
}; | }; | ||||
} else if ( | } else if ( | ||||
action.type === logOutActionTypes.success || | action.type === logOutActionTypes.success || | ||||
action.type === deleteAccountActionTypes.success || | action.type === deleteAccountActionTypes.success || | ||||
(action.type === setNewSessionActionType && | (action.type === setNewSessionActionType && | ||||
action.payload.sessionChange.cookieInvalidated) | action.payload.sessionChange.cookieInvalidated) | ||||
) { | ) { | ||||
return { | return { | ||||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |