Commit 8ed012d0 authored by Yorick Peterse's avatar Yorick Peterse

Merge branch 'master' of gitlab.com:gitlab-org/gitlab

parents dc42afe9 b1453948
...@@ -40,7 +40,7 @@ export default { ...@@ -40,7 +40,7 @@ export default {
this.selectedOptions = xor(this.selectedOptions, [option]); this.selectedOptions = xor(this.selectedOptions, [option]);
} }
this.updateRouteQuery(); this.updateQuerystring();
}, },
}, },
NO_ACTIVITY, NO_ACTIVITY,
......
...@@ -84,7 +84,7 @@ export default { ...@@ -84,7 +84,7 @@ export default {
? without(this.selectedOptions, ...options) ? without(this.selectedOptions, ...options)
: union(this.selectedOptions, options); : union(this.selectedOptions, options);
this.updateRouteQuery(); this.updateQuerystring();
}, },
}, },
}; };
......
...@@ -41,12 +41,8 @@ export default { ...@@ -41,12 +41,8 @@ export default {
selectedOptionsOrAll() { selectedOptionsOrAll() {
return this.selectedOptions.length ? this.selectedOptions : [this.filter.allOption]; return this.selectedOptions.length ? this.selectedOptions : [this.filter.allOption];
}, },
queryObject() {
// This is the object used to update the querystring.
return { [this.filter.id]: this.selectedOptionsOrAll.map((x) => x.id) };
},
filterObject() { filterObject() {
// This is the object used by the GraphQL query. // This is passed to the vulnerability list's GraphQL query as a variable.
return { [this.filter.id]: this.selectedOptions.map((x) => x.id) }; return { [this.filter.id]: this.selectedOptions.map((x) => x.id) };
}, },
filteredOptions() { filteredOptions() {
...@@ -54,13 +50,13 @@ export default { ...@@ -54,13 +50,13 @@ export default {
option.name.toLowerCase().includes(this.searchTerm.toLowerCase()), option.name.toLowerCase().includes(this.searchTerm.toLowerCase()),
); );
}, },
routeQueryIds() { querystringIds() {
const ids = this.$route?.query[this.filter.id] || []; const ids = this.$route?.query[this.filter.id] || [];
return Array.isArray(ids) ? ids : [ids]; return Array.isArray(ids) ? ids : [ids];
}, },
routeQueryOptions() { querystringOptions() {
const options = this.options.filter((x) => this.routeQueryIds.includes(x.id)); const options = this.options.filter((x) => this.querystringIds.includes(x.id));
const hasAllId = this.routeQueryIds.includes(this.filter.allOption.id); const hasAllId = this.querystringIds.includes(this.filter.allOption.id);
if (options.length && !hasAllId) { if (options.length && !hasAllId) {
return options; return options;
...@@ -78,32 +74,31 @@ export default { ...@@ -78,32 +74,31 @@ export default {
}, },
}, },
created() { created() {
this.selectedOptions = this.routeQueryOptions; this.selectedOptions = this.querystringOptions;
// When the user clicks the forward/back browser buttons, update the selected options. // When the user clicks the forward/back browser buttons, update the selected options.
window.addEventListener('popstate', () => { window.addEventListener('popstate', () => {
this.selectedOptions = this.routeQueryOptions; this.selectedOptions = this.querystringOptions;
}); });
}, },
methods: { methods: {
toggleOption(option) { toggleOption(option) {
// Toggle the option's existence in the array. // Toggle the option's existence in the array.
this.selectedOptions = xor(this.selectedOptions, [option]); this.selectedOptions = xor(this.selectedOptions, [option]);
this.updateRouteQuery(); this.updateQuerystring();
}, },
deselectAllOptions() { deselectAllOptions() {
this.selectedOptions = []; this.selectedOptions = [];
this.updateRouteQuery(); this.updateQuerystring();
}, },
updateRouteQuery() { updateQuerystring() {
if (!this.$router) { const options = this.selectedOptionsOrAll.map((x) => x.id);
// To avoid a console error, don't update the querystring if it's the same as the current one.
if (!this.$router || isEqual(this.querystringIds, options)) {
return; return;
} }
const query = { query: { ...this.$route?.query, ...this.queryObject } }; const query = { ...this.$route.query, [this.filter.id]: options };
// To avoid a console error, don't update the querystring if it's the same as the current one. this.$router.push({ query });
if (!isEqual(this.routeQueryIds, this.queryObject[this.filter.id])) {
this.$router.push(query);
}
}, },
isSelected(option) { isSelected(option) {
return this.selectedSet.has(option); return this.selectedSet.has(option);
......
...@@ -186,7 +186,7 @@ describe('Standard Filter component', () => { ...@@ -186,7 +186,7 @@ describe('Standard Filter component', () => {
}); });
describe('filter querystring', () => { describe('filter querystring', () => {
const updateRouteQuery = async (ids) => { const updateQuerystring = async (ids) => {
// window.history.back() won't change the location nor fire the popstate event, so we need // window.history.back() won't change the location nor fire the popstate event, so we need
// to fake it by doing it manually. // to fake it by doing it manually.
router.replace({ query: { [filter.id]: ids } }); router.replace({ query: { [filter.id]: ids } });
...@@ -221,7 +221,7 @@ describe('Standard Filter component', () => { ...@@ -221,7 +221,7 @@ describe('Standard Filter component', () => {
describe('querystring on page load', () => { describe('querystring on page load', () => {
it('selects correct items', () => { it('selects correct items', () => {
updateRouteQuery(optionIdsAt([1, 3, 5, 7])); updateQuerystring(optionIdsAt([1, 3, 5, 7]));
createWrapper(); createWrapper();
expectSelectedItems([1, 3, 5, 7]); expectSelectedItems([1, 3, 5, 7]);
...@@ -229,21 +229,21 @@ describe('Standard Filter component', () => { ...@@ -229,21 +229,21 @@ describe('Standard Filter component', () => {
it('selects only valid items when querystring has valid and invalid IDs', async () => { it('selects only valid items when querystring has valid and invalid IDs', async () => {
const ids = optionIdsAt([2, 4, 6]).concat(['some', 'invalid', 'ids']); const ids = optionIdsAt([2, 4, 6]).concat(['some', 'invalid', 'ids']);
updateRouteQuery(ids); updateQuerystring(ids);
createWrapper(); createWrapper();
expectSelectedItems([2, 4, 6]); expectSelectedItems([2, 4, 6]);
}); });
it('selects default options if querystring only has invalid items', async () => { it('selects default options if querystring only has invalid items', async () => {
updateRouteQuery(['some', 'invalid', 'ids']); updateQuerystring(['some', 'invalid', 'ids']);
createWrapper({ defaultOptions: optionsAt([4, 5, 8]) }); createWrapper({ defaultOptions: optionsAt([4, 5, 8]) });
expectSelectedItems([4, 5, 8]); expectSelectedItems([4, 5, 8]);
}); });
it('selects All option if querystring only has invalid IDs and there are no default options', async () => { it('selects All option if querystring only has invalid IDs and there are no default options', async () => {
updateRouteQuery(['some', 'invalid', 'ids']); updateQuerystring(['some', 'invalid', 'ids']);
createWrapper(); createWrapper();
expectAllOptionSelected(); expectAllOptionSelected();
...@@ -254,7 +254,7 @@ describe('Standard Filter component', () => { ...@@ -254,7 +254,7 @@ describe('Standard Filter component', () => {
it('selects the correct options', async () => { it('selects the correct options', async () => {
createWrapper(); createWrapper();
const indexes = [3, 5, 7]; const indexes = [3, 5, 7];
await updateRouteQuery(optionIdsAt(indexes)); await updateQuerystring(optionIdsAt(indexes));
expectSelectedItems(indexes); expectSelectedItems(indexes);
}); });
...@@ -265,7 +265,7 @@ describe('Standard Filter component', () => { ...@@ -265,7 +265,7 @@ describe('Standard Filter component', () => {
await clickItemAt(3); await clickItemAt(3);
expectSelectedItems([2, 3, 5, 8]); expectSelectedItems([2, 3, 5, 8]);
await updateRouteQuery([]); await updateQuerystring([]);
expectSelectedItems([2, 5, 8]); expectSelectedItems([2, 5, 8]);
}); });
...@@ -275,7 +275,7 @@ describe('Standard Filter component', () => { ...@@ -275,7 +275,7 @@ describe('Standard Filter component', () => {
await clickItemAt(3); await clickItemAt(3);
expectSelectedItems([3]); expectSelectedItems([3]);
await updateRouteQuery([]); await updateQuerystring([]);
expectAllOptionSelected(); expectAllOptionSelected();
}); });
...@@ -283,13 +283,13 @@ describe('Standard Filter component', () => { ...@@ -283,13 +283,13 @@ describe('Standard Filter component', () => {
createWrapper({ defaultOptions: optionsAt([2, 4, 8]) }); createWrapper({ defaultOptions: optionsAt([2, 4, 8]) });
expectSelectedItems([2, 4, 8]); expectSelectedItems([2, 4, 8]);
await updateRouteQuery([filter.allOption.id]); await updateQuerystring([filter.allOption.id]);
expectAllOptionSelected(); expectAllOptionSelected();
}); });
it('selects All option if querystring has all option ID as well as other IDs', async () => { it('selects All option if querystring has all option ID as well as other IDs', async () => {
createWrapper({ defaultOptions: optionsAt([5, 6, 9]) }); createWrapper({ defaultOptions: optionsAt([5, 6, 9]) });
await updateRouteQuery([filter.allOption.id, ...optionIdsAt([1, 2])]); await updateQuerystring([filter.allOption.id, ...optionIdsAt([1, 2])]);
expectAllOptionSelected(); expectAllOptionSelected();
}); });
...@@ -297,7 +297,7 @@ describe('Standard Filter component', () => { ...@@ -297,7 +297,7 @@ describe('Standard Filter component', () => {
it('selects only valid items when querystring has valid and invalid IDs', async () => { it('selects only valid items when querystring has valid and invalid IDs', async () => {
createWrapper(); createWrapper();
const ids = optionIdsAt([3, 7, 9]).concat(['some', 'invalid', 'ids']); const ids = optionIdsAt([3, 7, 9]).concat(['some', 'invalid', 'ids']);
await updateRouteQuery(ids); await updateQuerystring(ids);
expectSelectedItems([3, 7, 9]); expectSelectedItems([3, 7, 9]);
}); });
...@@ -308,7 +308,7 @@ describe('Standard Filter component', () => { ...@@ -308,7 +308,7 @@ describe('Standard Filter component', () => {
await clickItemAt(8); await clickItemAt(8);
expectSelectedItems([1, 3, 4, 8]); expectSelectedItems([1, 3, 4, 8]);
await updateRouteQuery(['some', 'invalid', 'ids']); await updateQuerystring(['some', 'invalid', 'ids']);
expectSelectedItems([1, 3, 4]); expectSelectedItems([1, 3, 4]);
}); });
...@@ -318,7 +318,7 @@ describe('Standard Filter component', () => { ...@@ -318,7 +318,7 @@ describe('Standard Filter component', () => {
await clickItemAt(8); await clickItemAt(8);
expectSelectedItems([8]); expectSelectedItems([8]);
await updateRouteQuery(['some', 'invalid', 'ids']); await updateQuerystring(['some', 'invalid', 'ids']);
expectAllOptionSelected(); expectAllOptionSelected();
}); });
......
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