Commit f7aa75f5 authored by Mark Florian's avatar Mark Florian

Extract base filter constant

This extracts the base filter string `"all"` to a constant, and adds
a utility function for checking whether an option is a base filter.

Part of the [Instance Security Dashboard MVC][1].

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