From 38d10da6649a86bef4ffe9f8f4b86ade06e7ce9f Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Tue, 26 Feb 2019 16:46:41 -0300 Subject: [PATCH] Begins frontend side integration with new items and facets return for elastic search. --- .../filter-types/filter-types-mixin.js | 366 ++++++++++-------- src/js/store/modules/collection/actions.js | 6 +- src/js/store/modules/search/actions.js | 4 + src/js/store/modules/search/getters.js | 4 + src/js/store/modules/search/index.js | 3 +- src/js/store/modules/search/mutations.js | 6 +- 6 files changed, 217 insertions(+), 172 deletions(-) diff --git a/src/classes/filter-types/filter-types-mixin.js b/src/classes/filter-types/filter-types-mixin.js index 6ad96306c..2afc4f6eb 100644 --- a/src/classes/filter-types/filter-types-mixin.js +++ b/src/classes/filter-types/filter-types-mixin.js @@ -1,5 +1,7 @@ import qs from 'qs'; import axios from '../../js/axios/axios'; +import { mapGetters } from 'vuex'; +import { resolve } from 'bluebird'; export const filter_type_mixin = { data () { @@ -27,118 +29,81 @@ export const filter_type_mixin = { this.loadOptions(true); }); }, + computed: { + facetsFromItemSearch() { + return this.getFacets(); + } + }, methods: { + ...mapGetters('search', [ + 'getFacets' + ]), getValuesPlainText(metadatumId, search, isRepositoryLevel, valuesToIgnore, offset, number, isInCheckboxModal, getSelected = '0') { - const source = axios.CancelToken.source(); + if (this.facetsFromItemSearch && this.facetsFromItemSearch.length > 0) { - let currentQuery = JSON.parse(JSON.stringify(this.query)); - if (currentQuery.fetch_only != undefined) { - delete currentQuery.fetch_only; - // for (let key of Object.keys(currentQuery.fetch_only)) { - // if (currentQuery.fetch_only[key] == null) - // delete currentQuery.fetch_only[key]; - // } - } - let query_items = { 'current_query': currentQuery }; + const source = axios.CancelToken.source(); - let url = ''; - if (isRepositoryLevel || this.filter.collection_id == 'filter_in_repository') - url = `/facets/${metadatumId}?getSelected=${getSelected}&`; - else - url = `/collection/${this.collection}/facets/${metadatumId}?getSelected=${getSelected}&`; - - if(offset != undefined && number != undefined){ - url += `offset=${offset}&number=${number}&`; - } + let currentQuery = JSON.parse(JSON.stringify(this.query)); + if (currentQuery.fetch_only != undefined) { + delete currentQuery.fetch_only; + // for (let key of Object.keys(currentQuery.fetch_only)) { + // if (currentQuery.fetch_only[key] == null) + // delete currentQuery.fetch_only[key]; + // } + } + let query_items = { 'current_query': currentQuery }; - if(search && offset != undefined && number != undefined){ - url += `search=${search}&` + qs.stringify(query_items); - } else if(search){ - url += `search=${search}&` + qs.stringify(query_items); - } else { - url += qs.stringify(query_items); - } - - this.isLoadingOptions = true; - - return new Object ({ - request: - axios.tainacan.get(url, { cancelToken: source.token }) - .then(res => { - this.isLoadingOptions = false; + let url = ''; + if (isRepositoryLevel || this.filter.collection_id == 'filter_in_repository') + url = `/facets/${metadatumId}?getSelected=${getSelected}&`; + else + url = `/collection/${this.collection}/facets/${metadatumId}?getSelected=${getSelected}&`; + + if(offset != undefined && number != undefined){ + url += `offset=${offset}&number=${number}&`; + } - let sResults = []; - let opts = []; - - for (let metadata of res.data) { - if (valuesToIgnore != undefined && valuesToIgnore.length > 0) { - let indexToIgnore = valuesToIgnore.findIndex(value => value == metadata.value); - - if (search && isInCheckboxModal) { - sResults.push({ - label: metadata.label, - value: metadata.value, - total_items: metadata.total_items - }); - } else if (indexToIgnore < 0) { - opts.push({ - label: metadata.label, - value: metadata.value, - total_items: metadata.total_items - }); - } - } else { - if (search && isInCheckboxModal) { - sResults.push({ - label: metadata.label, - value: metadata.value, - total_items: metadata.total_items - }); - } else { - opts.push({ - label: metadata.label, - value: metadata.value, - total_items: metadata.total_items - }); - } - } - } - - - this.searchResults = sResults; - - if (opts.length) { - this.options = opts; - } else if(!search) { - this.noMorePage = 1; - } - - if(this.options.length < this.maxNumOptionsCheckboxList && !search){ - this.noMorePage = 1; - } - - if (this.filter.max_options && this.options.length >= this.filter.max_options) { - let showViewAllButton = true; - - if(this.options.length === this.filter.max_options){ - this.options[this.filter.max_options-1].showViewAllButton = showViewAllButton; - } else { - this.options[this.options.length-1].showViewAllButton = showViewAllButton; - } - } - - }) - .catch((thrown) => { - if (axios.isCancel(thrown)) { - console.log('Request canceled: ', thrown.message); - } else { + if(search && offset != undefined && number != undefined){ + url += `search=${search}&` + qs.stringify(query_items); + } else if(search){ + url += `search=${search}&` + qs.stringify(query_items); + } else { + url += qs.stringify(query_items); + } + + this.isLoadingOptions = true; + + return new Object ({ + request: + axios.tainacan.get(url, { cancelToken: source.token }) + .then(res => { this.isLoadingOptions = false; - reject(thrown); - } - }), - source: source - }); + this.prepareOptionsForPlainText(res.data, search, valuesToIgnore, isInCheckboxModal); + }) + .catch((thrown) => { + if (axios.isCancel(thrown)) { + console.log('Request canceled: ', thrown.message); + } else { + this.isLoadingOptions = false; + reject(thrown); + } + }), + source: source + }); + + } else { + let callback = new Promise((resolve) => { + for (const facet in this.facetsFromItemSearch) { + if (facet == this.filter.id) { + this.prepareOptionsForPlainText(this.facetsFromItemSearch[facet], search, valuesToIgnore, isInCheckboxModal); + } + } + }); + return new Object ({ + request: callback + }); + } }, getValuesRelationship(collectionTarget, search, isRepositoryLevel, valuesToIgnore, offset, number, isInCheckboxModal, getSelected = '0') { @@ -177,71 +142,7 @@ export const filter_type_mixin = { axios.tainacan.get(url + '&fetch_only=thumbnail,title,id&' + qs.stringify(query_items)) .then(res => { this.isLoadingOptions = false; - - let sResults = []; - let opts = []; - - if (res.data.length > 0) { - for (let item of res.data) { - if (valuesToIgnore != undefined && valuesToIgnore.length > 0) { - let indexToIgnore = valuesToIgnore.findIndex(value => value == item.value); - - if (search && isInCheckboxModal) { - sResults.push({ - label: item.label, - value: item.value, - total_items: item.total_items - }); - } else if (indexToIgnore < 0) { - opts.push({ - label: item.label, - value: item.value, - img: (item.img ? item.img : this.thumbPlaceholderPath), - total_items: item.total_items - }); - } - } else { - if (search && isInCheckboxModal) { - sResults.push({ - label: item.label, - value: item.value, - img: (item.img ? item.img : this.thumbPlaceholderPath), - total_items: item.total_items - }); - } else { - opts.push({ - label: item.label, - value: item.value, - img: (item.img ? item.img : this.thumbPlaceholderPath), - total_items: item.total_items - }); - } - } - } - } - - this.searchResults = sResults; - - if (opts.length) { - this.options = opts; - } else { - this.noMorePage = 1; - } - - if(this.options.length < this.maxNumOptionsCheckboxList){ - this.noMorePage = 1; - } - - if (this.filter.max_options && this.options.length >= this.filter.max_options) { - let showViewAllButton = true; - - if(this.options.length === this.filter.max_options){ - this.options[this.filter.max_options-1].showViewAllButton = showViewAllButton; - } else { - this.options[this.options.length-1].showViewAllButton = showViewAllButton; - } - } - + this.prepareOptionsForRelationship(res.data, search, valuesToIgnore, isInCheckboxModal); }) .catch((thrown) => { if (axios.isCancel(thrown)) { @@ -253,6 +154,133 @@ export const filter_type_mixin = { }), source: source }); + }, + prepareOptionsForPlainText(metadata, search, valuesToIgnore, isInCheckboxModal) { + + let sResults = []; + let opts = []; + + for (let metadatum of metadata) { + if (valuesToIgnore != undefined && valuesToIgnore.length > 0) { + let indexToIgnore = valuesToIgnore.findIndex(value => value == metadatum.value); + + if (search && isInCheckboxModal) { + sResults.push({ + label: metadatum.label, + value: metadatum.value, + total_items: metadatum.total_items + }); + } else if (indexToIgnore < 0) { + opts.push({ + label: metadatum.label, + value: metadatum.value, + total_items: metadatum.total_items + }); + } + } else { + if (search && isInCheckboxModal) { + sResults.push({ + label: metadatum.label, + value: metadatum.value, + total_items: metadatum.total_items + }); + } else { + opts.push({ + label: metadatum.label, + value: metadatum.value, + total_items: metadatum.total_items + }); + } + } + } + + this.searchResults = sResults; + + if (opts.length) { + this.options = opts; + } else if(!search) { + this.noMorePage = 1; + } + + if(this.options.length < this.maxNumOptionsCheckboxList && !search){ + this.noMorePage = 1; + } + + if (this.filter.max_options && this.options.length >= this.filter.max_options) { + let showViewAllButton = true; + + if(this.options.length === this.filter.max_options){ + this.options[this.filter.max_options-1].showViewAllButton = showViewAllButton; + } else { + this.options[this.options.length-1].showViewAllButton = showViewAllButton; + } + } + }, + prepareOptionsForRelationship(items, search, valuesToIgnore, isInCheckboxModal) { + + let sResults = []; + let opts = []; + + if (res.data.length > 0) { + for (let item of items) { + if (valuesToIgnore != undefined && valuesToIgnore.length > 0) { + let indexToIgnore = valuesToIgnore.findIndex(value => value == item.value); + + if (search && isInCheckboxModal) { + sResults.push({ + label: item.label, + value: item.value, + total_items: item.total_items + }); + } else if (indexToIgnore < 0) { + opts.push({ + label: item.label, + value: item.value, + img: (item.img ? item.img : this.thumbPlaceholderPath), + total_items: item.total_items + }); + } + } else { + if (search && isInCheckboxModal) { + sResults.push({ + label: item.label, + value: item.value, + img: (item.img ? item.img : this.thumbPlaceholderPath), + total_items: item.total_items + }); + } else { + opts.push({ + label: item.label, + value: item.value, + img: (item.img ? item.img : this.thumbPlaceholderPath), + total_items: item.total_items + }); + } + } + } + } + + this.searchResults = sResults; + + if (opts.length) { + this.options = opts; + } else { + this.noMorePage = 1; + } + + if(this.options.length < this.maxNumOptionsCheckboxList){ + this.noMorePage = 1; + } + + if (this.filter.max_options && this.options.length >= this.filter.max_options) { + let showViewAllButton = true; + + if(this.options.length === this.filter.max_options){ + this.options[this.filter.max_options-1].showViewAllButton = showViewAllButton; + } else { + this.options[this.options.length-1].showViewAllButton = showViewAllButton; + } + } } }, beforeDestroy() { diff --git a/src/js/store/modules/collection/actions.js b/src/js/store/modules/collection/actions.js index 03b7efe7d..e92a62617 100644 --- a/src/js/store/modules/collection/actions.js +++ b/src/js/store/modules/collection/actions.js @@ -73,7 +73,7 @@ export const fetchItems = ({ rootGetters, dispatch, commit }, { collectionId, is }) .then(res => { - let items = res.data; + let items = res.data.items; let viewModeObject = tainacan_plugin.registered_view_modes[postQueries.view_mode]; if (isOnTheme && viewModeObject != undefined && viewModeObject.type == 'template') { @@ -97,6 +97,10 @@ export const fetchItems = ({ rootGetters, dispatch, commit }, { collectionId, is dispatch('search/setTotalItems', res.headers['x-wp-total'], { root: true } ); dispatch('search/setTotalPages', res.headers['x-wp-totalpages'], { root: true } ); dispatch('search/setItemsPerPage', res.headers['x-wp-itemsperpage'], { root: true } ); + if (res.data.filters && Object.values(res.data.filters)) + dispatch('search/setFacets', res.data.filters, { root: true } ); + else + dispatch('search/setFacets', {}, { root: true } ); }) .catch((thrown) => { if (axios.isCancel(thrown)) { diff --git a/src/js/store/modules/search/actions.js b/src/js/store/modules/search/actions.js index c8a67996a..41fc4576c 100644 --- a/src/js/store/modules/search/actions.js +++ b/src/js/store/modules/search/actions.js @@ -67,6 +67,10 @@ export const setItemsPerPage = ({ commit }, page ) => { commit('setItemsPerPage', page ); }; +export const setFacets = ({ commit }, facets) => { + commit('setFacets', facets); +}; + export const setStatus= ({ commit }, status ) => { if (status == undefined || status == '') commit('removePostQueryAttribute', 'status'); diff --git a/src/js/store/modules/search/getters.js b/src/js/store/modules/search/getters.js index 8186f3a2a..1f9d4df0f 100644 --- a/src/js/store/modules/search/getters.js +++ b/src/js/store/modules/search/getters.js @@ -71,3 +71,7 @@ export const getFetchOnlyMeta = state => { export const getFilterTags = state => { return state.filter_tags; }; + +export const getFacets = state => { + return state.facets; +}; diff --git a/src/js/store/modules/search/index.js b/src/js/store/modules/search/index.js index 509dfb675..26f1b4d9a 100644 --- a/src/js/store/modules/search/index.js +++ b/src/js/store/modules/search/index.js @@ -21,7 +21,8 @@ const state = { filter_tags: [], totalItems: 0, totalPages: 0, - itemsPerPage: 12 // Not the same as postquery.perpage as API may have limited it's value + itemsPerPage: 12, // Not the same as postquery.perpage as API may have limited it's value + facets: {} }; export default { diff --git a/src/js/store/modules/search/mutations.js b/src/js/store/modules/search/mutations.js index 89883fe2b..8f521202a 100644 --- a/src/js/store/modules/search/mutations.js +++ b/src/js/store/modules/search/mutations.js @@ -179,4 +179,8 @@ export const cleanTaxQueries = (state) => { export const cleanFetchOnly = (state) => { delete state.postquery.fetch_only; -}; \ No newline at end of file +}; + +export const setFacets = (state, facets) => { + state.facets = facets; +} \ No newline at end of file