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,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() {

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

@ -179,4 +179,8 @@ export const cleanTaxQueries = (state) => {
export const cleanFetchOnly = (state) => {
delete state.postquery.fetch_only;
};
};
export const setFacets = (state, facets) => {
state.facets = facets;
}