Skip to content

Commit

Permalink
Mini refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
polosson committed Mar 23, 2021
1 parent 0e6968c commit 95a690c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
47 changes: 26 additions & 21 deletions client/src/pages/Event/Step4/MaterialsList/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Config from '@/config/globalConfig';
import formatAmount from '@/utils/formatAmount';
import isValidInteger from '@/utils/isValidInteger';
import MaterialsFilter from '@/components/MaterialsFilters/MaterialsFilters.vue';
import SwitchToggle from '@/components/SwitchToggle/SwitchToggle.vue';
import Quantity from './Quantity/Quantity.vue';
Expand Down Expand Up @@ -34,26 +35,6 @@ export default {

const hasMaterial = this.initialList.length > 0;

const initFilters = {
onlySelected: hasMaterial,
};

if (this.$route.query.park) {
initFilters.park = this.$route.query.park;
}

if (this.$route.query.category) {
initFilters.category = this.$route.query.category;
}

if (this.$route.query.subCategory) {
initFilters.subCategory = this.$route.query.subCategory;
}

if (this.$route.query.tags) {
initFilters.tags = JSON.parse(this.$route.query.tags);
}

return {
error: null,
renderId: 1,
Expand All @@ -78,7 +59,7 @@ export default {
amount: 'MaterialsList__amount',
actions: 'MaterialsList__actions',
},
initFilters,
initFilters: this.getFilters(),
customFilters: [
{
name: 'park',
Expand Down Expand Up @@ -129,6 +110,30 @@ export default {
}
},

getFilters() {
const filters = {
onlySelected: this.showSelectedOnly,
};

if (this.$route.query.park && isValidInteger(this.$route.query.park)) {
filters.park = parseInt(this.$route.query.park, 10);
}

if (this.$route.query.category) {
filters.category = this.$route.query.category;
}

if (this.$route.query.subCategory) {
filters.subCategory = this.$route.query.subCategory;
}

if (this.$route.query.tags) {
filters.tags = JSON.parse(this.$route.query.tags);
}

return filters;
},

handleToggleSelectedOnly(newValue) {
this.$refs.DataTable.setCustomFilters({ onlySelected: newValue });
this.$refs.DataTable.setLimit(
Expand Down
8 changes: 8 additions & 0 deletions client/src/utils/isValidInteger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const isValidInteger = (value) => {
if (typeof value === 'number') {
return !Number.isNaN(value) && Number.isInteger(parseFloat(value));
}
return /^-?[0-9]+$/.test(value);
};

export default isValidInteger;
21 changes: 21 additions & 0 deletions client/tests/unit/utils/isValidInteger.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import isValidInteger from '@/utils/isValidInteger';

describe('isValidInteger', () => {
it('returns true when value is a valid integer', () => {
expect(isValidInteger(1)).toBe(true);
expect(isValidInteger(-55555)).toBe(true);
expect(isValidInteger('10')).toBe(true);
expect(isValidInteger('10546')).toBe(true);
expect(isValidInteger('-10')).toBe(true);
});

it('returns false when value is not a valid integer', () => {
expect(isValidInteger(10.5)).toBe(false);
expect(isValidInteger(-10.5)).toBe(false);
expect(isValidInteger('10.65')).toBe(false);
expect(isValidInteger('foo 10')).toBe(false);
expect(isValidInteger('Infinity')).toBe(false);
expect(isValidInteger(Infinity)).toBe(false);
expect(isValidInteger('10 546')).toBe(false);
});
});

0 comments on commit 95a690c

Please sign in to comment.