Commit 9b96cb9f authored by Tim Zallmann's avatar Tim Zallmann

Merge branch '6953-instance-lvl-security-dashboard-extract-base-filter-constant-ee' into 'master'

Extract base filter constant in Securitty Dashboard store's filters module

See merge request gitlab-org/gitlab!17710
parents a140a9d1 f7aa75f5
......@@ -28,21 +28,22 @@ export const REPORT_TYPES = {
sast: s__('ciReport|SAST'),
};
export const ALL = 'all';
export const BASE_FILTERS = {
severity: {
name: s__('ciReport|All severities'),
id: 'all',
id: ALL,
},
confidence: {
name: s__('ciReport|All confidence levels'),
id: 'all',
id: ALL,
},
report_type: {
name: s__('ciReport|All report types'),
id: 'all',
id: ALL,
},
project_id: {
name: s__('ciReport|All projects'),
id: 'all',
id: ALL,
},
};
import { sprintf, __ } from '~/locale';
import { isBaseFilterOption } from './utils';
export const getFilter = state => filterId => state.filters.find(filter => filter.id === filterId);
......@@ -22,13 +23,13 @@ export const getSelectedOptionNames = (state, getters) => filterId => {
/**
* Loops through all the filters and returns all the active ones
* stripping out any that are set to 'all'
* stripping out base filter options.
* @returns Object
* e.g. { type: ['sast'], severity: ['high', 'medium'] }
*/
export const activeFilters = state => {
const filters = state.filters.reduce((acc, filter) => {
acc[filter.id] = [...Array.from(filter.selection)].filter(option => option !== 'all');
acc[filter.id] = [...Array.from(filter.selection)].filter(id => !isBaseFilterOption(id));
return acc;
}, {});
// hide_dismissed is hardcoded as it currently is an edge-case, more info in the MR:
......
import * as types from './mutation_types';
import { ALL } from './constants';
import { isBaseFilterOption } from './utils';
export default {
[types.SET_ALL_FILTERS](state, payload = {}) {
......@@ -12,7 +14,7 @@ export default {
// This prevents us from selecting nothing at all
if (selection.size === 0) {
selection.add('all');
selection.add(ALL);
}
return { ...filter, selection };
......@@ -26,10 +28,10 @@ export default {
if (activeFilter) {
let selection = new Set(activeFilter.selection);
if (optionId === 'all') {
selection = new Set(['all']);
if (isBaseFilterOption(optionId)) {
selection = new Set([ALL]);
} else {
selection.delete('all');
selection.delete(ALL);
if (selection.has(optionId)) {
selection.delete(optionId);
} else {
......@@ -39,7 +41,7 @@ export default {
// This prevents us from selecting nothing at all
if (selection.size === 0) {
selection.add('all');
selection.add(ALL);
}
activeFilter.selection = selection;
}
......
......@@ -10,28 +10,28 @@ export default () => ({
id: 'severity',
options: [BASE_FILTERS.severity, ...optionsObjectToArray(SEVERITY_LEVELS)],
hidden: false,
selection: new Set(['all']),
selection: new Set([BASE_FILTERS.severity.id]),
},
{
name: s__('SecurityDashboard|Confidence'),
id: 'confidence',
options: [BASE_FILTERS.confidence, ...optionsObjectToArray(CONFIDENCE_LEVELS)],
hidden: false,
selection: new Set(['all']),
selection: new Set([BASE_FILTERS.confidence.id]),
},
{
name: s__('SecurityDashboard|Report type'),
id: 'report_type',
options: [BASE_FILTERS.report_type, ...optionsObjectToArray(REPORT_TYPES)],
hidden: false,
selection: new Set(['all']),
selection: new Set([BASE_FILTERS.report_type.id]),
},
{
name: s__('SecurityDashboard|Project'),
id: 'project_id',
options: [BASE_FILTERS.project_id],
hidden: false,
selection: new Set(['all']),
selection: new Set([BASE_FILTERS.project_id.id]),
},
],
hide_dismissed: true,
......
import { ALL } from './constants';
// eslint-disable-next-line import/prefer-default-export
export const isBaseFilterOption = id => id === ALL;
import createState from 'ee/security_dashboard/store/modules/filters/state';
import * as types from 'ee/security_dashboard/store/modules/filters/mutation_types';
import mutations from 'ee/security_dashboard/store/modules/filters/mutations';
import { ALL } from 'ee/security_dashboard/store/modules/filters/constants';
describe('filters module mutations', () => {
let state;
......@@ -32,7 +33,7 @@ describe('filters module mutations', () => {
optionId: criticalOption.id,
});
expect(state.filters[0].selection).toEqual(new Set(['all']));
expect(state.filters[0].selection).toEqual(new Set([ALL]));
});
describe('on subsequent changes', () => {
......@@ -69,7 +70,7 @@ describe('filters module mutations', () => {
it('should set options to `all` if no payload is given', () => {
mutations[types.SET_ALL_FILTERS](state);
const expected = new Set(['all']);
const expected = new Set([ALL]);
state.filters.forEach(filter => {
expect(filter.selection).toEqual(expected);
......@@ -81,7 +82,7 @@ describe('filters module mutations', () => {
[severityFilter.id]: [],
});
const expected = new Set(['all']);
const expected = new Set([ALL]);
expect(state.filters[0].selection).toEqual(expected);
});
......
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