Commit 2bc06dec authored by Jose Ivan Vargas's avatar Jose Ivan Vargas

Merge branch '335066-dependency-list-multiple-default-sort-orders' into 'master'

Sort according to the field in Dependency List

See merge request gitlab-org/gitlab!66672
parents 808eaaa0 8832e172
...@@ -3,11 +3,12 @@ import { GlButton, GlDropdown, GlDropdownItem, GlIcon, GlTooltipDirective } from ...@@ -3,11 +3,12 @@ import { GlButton, GlDropdown, GlDropdownItem, GlIcon, GlTooltipDirective } from
import { mapActions, mapState } from 'vuex'; import { mapActions, mapState } from 'vuex';
import { __ } from '~/locale'; import { __ } from '~/locale';
import { DEPENDENCY_LIST_TYPES } from '../store/constants'; import { DEPENDENCY_LIST_TYPES } from '../store/constants';
import { SORT_FIELDS, SORT_ORDER } from '../store/modules/list/constants'; import { SORT_FIELDS, SORT_ASCENDING } from '../store/modules/list/constants';
export default { export default {
i18n: { i18n: {
sortDirectionLabel: __('Sort direction'), sortDirectionLabel: __('Sort direction'),
sortFields: SORT_FIELDS,
}, },
name: 'DependenciesActions', name: 'DependenciesActions',
components: { components: {
...@@ -27,11 +28,6 @@ export default { ...@@ -27,11 +28,6 @@ export default {
Object.values(DEPENDENCY_LIST_TYPES).some(({ namespace }) => value === namespace), Object.values(DEPENDENCY_LIST_TYPES).some(({ namespace }) => value === namespace),
}, },
}, },
data() {
return {
sortFields: SORT_FIELDS,
};
},
computed: { computed: {
...mapState({ ...mapState({
sortField(state) { sortField(state) {
...@@ -45,10 +41,10 @@ export default { ...@@ -45,10 +41,10 @@ export default {
}, },
}), }),
sortFieldName() { sortFieldName() {
return this.sortFields[this.sortField]; return this.$options.i18n.sortFields[this.sortField];
}, },
sortOrderIcon() { sortOrderIcon() {
return this.sortOrder === SORT_ORDER.ascending ? 'sort-lowest' : 'sort-highest'; return this.sortOrder === SORT_ASCENDING ? 'sort-lowest' : 'sort-highest';
}, },
}, },
methods: { methods: {
...@@ -71,7 +67,11 @@ export default { ...@@ -71,7 +67,11 @@ export default {
<div class="btn-toolbar"> <div class="btn-toolbar">
<div class="btn-group flex-grow-1 mr-2"> <div class="btn-group flex-grow-1 mr-2">
<gl-dropdown :text="sortFieldName" class="flex-grow-1 text-center" right> <gl-dropdown :text="sortFieldName" class="flex-grow-1 text-center" right>
<gl-dropdown-item v-for="(name, id) in sortFields" :key="id" @click="setSortField(id)"> <gl-dropdown-item
v-for="(name, id) in $options.i18n.sortFields"
:key="id"
@click="setSortField(id)"
>
<span class="d-flex"> <span class="d-flex">
<gl-icon <gl-icon
class="flex-shrink-0 gl-mr-2" class="flex-shrink-0 gl-mr-2"
......
import { __, s__ } from '~/locale'; import { __, s__ } from '~/locale';
export const SORT_FIELD_NAME = 'name';
export const SORT_FIELD_PACKAGER = 'packager';
export const SORT_FIELD_SEVERITY = 'severity';
export const DEFAULT_SORT_FIELD = SORT_FIELD_SEVERITY;
export const SORT_FIELDS = { export const SORT_FIELDS = {
name: s__('Dependencies|Component name'), [SORT_FIELD_NAME]: s__('Dependencies|Component name'),
packager: s__('Dependencies|Packager'), [SORT_FIELD_PACKAGER]: s__('Dependencies|Packager'),
severity: s__('Vulnerability|Severity'), [SORT_FIELD_SEVERITY]: s__('Vulnerability|Severity'),
}; };
export const SORT_ORDER = { export const SORT_ASCENDING = 'asc';
ascending: 'asc', export const SORT_DESCENDING = 'desc';
descending: 'desc',
export const SORT_ORDERS = {
[SORT_FIELD_NAME]: SORT_ASCENDING,
[SORT_FIELD_PACKAGER]: SORT_ASCENDING,
[SORT_FIELD_SEVERITY]: SORT_DESCENDING,
}; };
export const REPORT_STATUS = { export const REPORT_STATUS = {
......
import { REPORT_STATUS, SORT_ORDER } from './constants'; import { REPORT_STATUS, SORT_ORDERS, SORT_ASCENDING, SORT_DESCENDING } from './constants';
import * as types from './mutation_types'; import * as types from './mutation_types';
export default { export default {
...@@ -36,9 +36,9 @@ export default { ...@@ -36,9 +36,9 @@ export default {
}, },
[types.SET_SORT_FIELD](state, payload) { [types.SET_SORT_FIELD](state, payload) {
state.sortField = payload; state.sortField = payload;
state.sortOrder = SORT_ORDERS[payload];
}, },
[types.TOGGLE_SORT_ORDER](state) { [types.TOGGLE_SORT_ORDER](state) {
state.sortOrder = state.sortOrder = state.sortOrder === SORT_ASCENDING ? SORT_DESCENDING : SORT_ASCENDING;
state.sortOrder === SORT_ORDER.ascending ? SORT_ORDER.descending : SORT_ORDER.ascending;
}, },
}; };
import { FILTER, REPORT_STATUS, SORT_ORDER } from './constants'; import { FILTER, REPORT_STATUS, SORT_ORDERS, DEFAULT_SORT_FIELD } from './constants';
export default () => ({ export default () => ({
endpoint: '', endpoint: '',
...@@ -15,6 +15,6 @@ export default () => ({ ...@@ -15,6 +15,6 @@ export default () => ({
generatedAt: '', generatedAt: '',
}, },
filter: FILTER.all, filter: FILTER.all,
sortField: 'severity', sortField: DEFAULT_SORT_FIELD,
sortOrder: SORT_ORDER.descending, sortOrder: SORT_ORDERS[DEFAULT_SORT_FIELD],
}); });
...@@ -4,7 +4,7 @@ import { sortBy } from 'lodash'; ...@@ -4,7 +4,7 @@ import { sortBy } from 'lodash';
import * as actions from 'ee/dependencies/store/modules/list/actions'; import * as actions from 'ee/dependencies/store/modules/list/actions';
import { import {
FILTER, FILTER,
SORT_ORDER, SORT_DESCENDING,
FETCH_ERROR_MESSAGE, FETCH_ERROR_MESSAGE,
} from 'ee/dependencies/store/modules/list/constants'; } from 'ee/dependencies/store/modules/list/constants';
import * as types from 'ee/dependencies/store/modules/list/mutation_types'; import * as types from 'ee/dependencies/store/modules/list/mutation_types';
...@@ -194,7 +194,7 @@ describe('Dependencies actions', () => { ...@@ -194,7 +194,7 @@ describe('Dependencies actions', () => {
describe('given params', () => { describe('given params', () => {
const paramsGiven = { const paramsGiven = {
sort_by: 'packager', sort_by: 'packager',
sort: SORT_ORDER.descending, sort: SORT_DESCENDING,
page: 4, page: 4,
filter: FILTER.vulnerable, filter: FILTER.vulnerable,
}; };
......
import { REPORT_STATUS, SORT_ORDER } from 'ee/dependencies/store/modules/list/constants'; import {
REPORT_STATUS,
SORT_ASCENDING,
SORT_DESCENDING,
} from 'ee/dependencies/store/modules/list/constants';
import * as types from 'ee/dependencies/store/modules/list/mutation_types'; import * as types from 'ee/dependencies/store/modules/list/mutation_types';
import mutations from 'ee/dependencies/store/modules/list/mutations'; import mutations from 'ee/dependencies/store/modules/list/mutations';
import getInitialState from 'ee/dependencies/store/modules/list/state'; import getInitialState from 'ee/dependencies/store/modules/list/state';
...@@ -86,24 +90,30 @@ describe('Dependencies mutations', () => { ...@@ -86,24 +90,30 @@ describe('Dependencies mutations', () => {
}); });
describe(types.SET_SORT_FIELD, () => { describe(types.SET_SORT_FIELD, () => {
it('sets the sort field', () => { it.each`
const field = 'foo'; field | order
${'name'} | ${SORT_ASCENDING}
${'packager'} | ${SORT_ASCENDING}
${'severity'} | ${SORT_DESCENDING}
${'foo'} | ${undefined}
`('sets the sort field to $field and sort order to $order', ({ field, order }) => {
mutations[types.SET_SORT_FIELD](state, field); mutations[types.SET_SORT_FIELD](state, field);
expect(state.sortField).toBe(field); expect(state.sortField).toBe(field);
expect(state.sortOrder).toBe(order);
}); });
}); });
describe(types.TOGGLE_SORT_ORDER, () => { describe(types.TOGGLE_SORT_ORDER, () => {
it('toggles the sort order', () => { it('toggles the sort order', () => {
const sortState = { sortOrder: SORT_ORDER.ascending }; const sortState = { sortOrder: SORT_ASCENDING };
mutations[types.TOGGLE_SORT_ORDER](sortState); mutations[types.TOGGLE_SORT_ORDER](sortState);
expect(sortState.sortOrder).toBe(SORT_ORDER.descending); expect(sortState.sortOrder).toBe(SORT_DESCENDING);
mutations[types.TOGGLE_SORT_ORDER](sortState); mutations[types.TOGGLE_SORT_ORDER](sortState);
expect(sortState.sortOrder).toBe(SORT_ORDER.ascending); expect(sortState.sortOrder).toBe(SORT_ASCENDING);
}); });
}); });
}); });
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