But as you can see, the computed properties repeats the same
switch
- is there a way to change all three properties in one switch statement so if I add further properties or switch cases, I do not have to do it multiple times?
One option is to ensure that the sales type is in (or not in) a white list of types using Array.prototype.includes()
-
A computed property for sales type could be added:
salesType() {
return this.$route?.query?.salesType || '';
}
Notice it used the optional chaining operator ?.
and logical OR ||
to gracefully fall back to an empty string in the event the query string does not contain that key, though maybe it should be handled differently.
Then that can be used to simplify the other methods. For example:
filteredSalesAgreementItems() { switch (this.$route.query.salesType) { case 'bricks': return this.salesAgreementItems.filter(obj => obj.department === 'bricks'); case 'mortars': return this.salesAgreementItems.filter(obj => obj.department === 'mortars'); default: return this.salesAgreementItems; } }
Can be simplified to this:
filteredSalesAgreementItems() {
if (!['bricks', 'mortars'].includes(this.$route.query.salesType)) {
return this.salesAgreementItems;
}
return this.salesAgreementItems.filter(
obj => obj.department === this.$route.query.salesType
);
}
Similarly for subTitle
:
subTitle() {
if (!['bricks', 'mortars'].includes(this.$route.query.salesType)) {
return 'All';
}
const type =return this.$route.query.salesType;
return typesalesType.charAt(0).toUpperCase() + typethis.salesType.subString(1);
},
and for title
:
title() {
if (!['bricks', 'mortars'].includes(this.$route.query.salesType)) {
return 'Bricks & Mortars';
}
const type =return this.$route.query.salesType;
return typesalesType.charAt(0).toUpperCase() + typethis.salesType.subString(1);
},
return this.$route.query.salesType.toTitleCase();