From 4ad6de5f32960ef4bf2980f6cb6fddb0bdef0bd2 Mon Sep 17 00:00:00 2001 From: Stephen Cameron Date: Mon, 2 Sep 2019 17:42:41 +0200 Subject: [PATCH] WIP #2962 --- resources/js/components/ProductsFilters.vue | 89 ++++++++++++++++----- resources/views/pages/products.blade.php | 6 +- 2 files changed, 75 insertions(+), 20 deletions(-) diff --git a/resources/js/components/ProductsFilters.vue b/resources/js/components/ProductsFilters.vue index de7bd06..d14fe75 100644 --- a/resources/js/components/ProductsFilters.vue +++ b/resources/js/components/ProductsFilters.vue @@ -14,7 +14,7 @@
  • @@ -40,7 +40,7 @@
    -
    +
    @@ -48,33 +48,24 @@
    - -
    - QUERYSTRING: {{ filter_querystring }} -
    -
    - + {{ resultsCount }}
    @@ -90,12 +81,19 @@ export default { props: { + productType: { + required: true, + }, filterData: { required: true, }, resultData: { required: true, }, + translations: { + type: Object, + required: true, + } }, data: () => ({ filters: {}, @@ -103,7 +101,22 @@ }), computed: { - filter_querystring() { + + resultsCount() { + let count = Object.keys(this.matches.hits).length; + let results = ''; + + // Handle pluralisation and localisation + if (count === 1) { + results = this.translations.result; + } else { + results = this.translations.results; + } + + return `${count} ${results}`; + }, + + filterQuerystring() { let filter_list = []; @@ -115,7 +128,7 @@ }, }, - mounted() { + created() { // Initially, it will be all the IDs from the server this.matches = this.resultData; @@ -131,6 +144,44 @@ methods: { + updateFilters() { + let $this = this; + let endpoint = `/ajax/filtercatalog/?productType=${this.productType}&${this.filterQuerystring}`; + + axios.get(endpoint) + .then(function (response) { + $this.matches = response.data.results; + $this.filterProducts(); + }) + .catch(function (error) { + console.error('Error filtering products', error); + }); + }, + + filterProducts() { + let matches = Object.values(this.matches.hits); + + this.$slots.default[0].children.forEach(child => { + if (child.tag) { // Ensure it's a tag and not an empty text element + let product = child.elm; + let ID = parseInt(product.dataset.productId); + if (matches.includes(ID)) { + product.classList.remove('hidden'); + } else { + product.classList.add('hidden'); + } + } + }); + }, + + removeFilter(filterID, optionID) { + let index = this.filters[filterID].indexOf(optionID); + + if (index !== -1) { + this.filters[filterID].splice(index, 1); + this.updateFilters(); + } + }, }, } diff --git a/resources/views/pages/products.blade.php b/resources/views/pages/products.blade.php index 891fcae..6105722 100644 --- a/resources/views/pages/products.blade.php +++ b/resources/views/pages/products.blade.php @@ -4,7 +4,11 @@ @intro(['padding' => 'pb-1v']) - + __('résultat'), 'results' => __('résultats')])'> {{-- Product Grid --}} -- 2.39.5