Begins frontend side integration with new items and facets return for elastic search.

This commit is contained in:
Mateus Machado Luna 2019-02-26 16:46:41 -03:00
parent cda873d9ae
commit 38d10da664
6 changed files with 217 additions and 172 deletions

View File

@ -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,9 +29,19 @@ 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') {
if (this.facetsFromItemSearch && this.facetsFromItemSearch.length > 0) {
const source = axios.CancelToken.source();
let currentQuery = JSON.parse(JSON.stringify(this.query));
@ -67,67 +79,7 @@ export const filter_type_mixin = {
axios.tainacan.get(url, { cancelToken: source.token })
.then(res => {
this.isLoadingOptions = false;
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;
}
}
this.prepareOptionsForPlainText(res.data, search, valuesToIgnore, isInCheckboxModal);
})
.catch((thrown) => {
if (axios.isCancel(thrown)) {
@ -139,6 +91,19 @@ export const filter_type_mixin = {
}),
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,12 +142,87 @@ export const filter_type_mixin = {
axios.tainacan.get(url + '&fetch_only=thumbnail,title,id&' + qs.stringify(query_items))
.then(res => {
this.isLoadingOptions = false;
this.prepareOptionsForRelationship(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
});
},
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 res.data) {
for (let item of items) {
if (valuesToIgnore != undefined && valuesToIgnore.length > 0) {
let indexToIgnore = valuesToIgnore.findIndex(value => value == item.value);
@ -241,18 +281,6 @@ export const filter_type_mixin = {
this.options[this.options.length-1].showViewAllButton = showViewAllButton;
}
}
})
.catch((thrown) => {
if (axios.isCancel(thrown)) {
console.log('Request canceled: ', thrown.message);
} else {
this.isLoadingOptions = false;
reject(thrown);
}
}),
source: source
});
}
},
beforeDestroy() {

View File

@ -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)) {

View File

@ -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');

View File

@ -71,3 +71,7 @@ export const getFetchOnlyMeta = state => {
export const getFilterTags = state => {
return state.filter_tags;
};
export const getFacets = state => {
return state.facets;
};

View File

@ -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 {

View File

@ -180,3 +180,7 @@ export const cleanTaxQueries = (state) => {
export const cleanFetchOnly = (state) => {
delete state.postquery.fetch_only;
};
export const setFacets = (state, facets) => {
state.facets = facets;
}