adapting search store to accept tax query params and fix filter type categories to perform search

This commit is contained in:
eduardohumberto 2018-04-12 22:30:52 -03:00
parent 41d30209fb
commit e358462ab8
7 changed files with 145 additions and 28 deletions

View File

@ -104,6 +104,7 @@
axios.get('/collection/'+collectionId+'/items?' + qs.stringify( metaquery )) axios.get('/collection/'+collectionId+'/items?' + qs.stringify( metaquery ))
.then( res => { .then( res => {
this.loading = false; this.loading = false;
this.options = [];
let result = res.data; let result = res.data;
for (let item of result) { for (let item of result) {
this.options.push({ label: item.title, value: item.id }) this.options.push({ label: item.title, value: item.id })
@ -137,4 +138,4 @@
} }
} }
} }
</script> </script>

View File

@ -32,6 +32,7 @@
collection: '', collection: '',
field: '', field: '',
selected: [], selected: [],
taxonomy: ''
} }
}, },
props: { props: {
@ -41,7 +42,10 @@
field_id: [Number], // not required, but overrides the filter field id if is set field_id: [Number], // not required, but overrides the filter field id if is set
collection_id: [Number], // not required, but overrides the filter field id if is set collection_id: [Number], // not required, but overrides the filter field id if is set
filter_type: [String], // not required, but overrides the filter field type if is set filter_type: [String], // not required, but overrides the filter field type if is set
id: '' id: '',
query: {
type: Object // concentrate all attributes field id and type
}
}, },
watch: { watch: {
selected: function(val){ selected: function(val){
@ -53,6 +57,7 @@
getValuesCategory( taxonomy ){ getValuesCategory( taxonomy ){
return axios.get('/taxonomy/' + taxonomy + '/terms?hideempty=0' ).then( res => { return axios.get('/taxonomy/' + taxonomy + '/terms?hideempty=0' ).then( res => {
for (let item of res.data) { for (let item of res.data) {
this.taxonomy = item.taxonomy;
this.options.push(item); this.options.push(item);
} }
}) })
@ -71,6 +76,7 @@
promise.then( () => { promise.then( () => {
this.isLoading = false; this.isLoading = false;
this.selectedValues()
}) })
.catch( error => { .catch( error => {
this.$console.log('error select', error ); this.$console.log('error select', error );
@ -96,16 +102,28 @@
} }
return result; return result;
}, },
onSelect(){ selectedValues(){
this.$console.log(this.selected); if ( !this.query || !this.query.taxquery || !Array.isArray( this.query.taxquery ) )
return false;
let index = this.query.taxquery.findIndex(newField => newField.taxonomy === this.taxonomy );
if ( index >= 0){
let metadata = this.query.taxquery[ index ];
this.selected = metadata.terms;
} else {
return false;
}
},
onSelect(){
this.$emit('input', { this.$emit('input', {
filter: 'term', filter: 'selectbox',
taxonomy: this.taxonomy,
compare: 'IN',
field_id: this.field, field_id: this.field,
collection_id: this.collection, collection_id: this.collection,
value: this.selected terms: this.selected
}); });
} }
} }
} }
</script> </script>

View File

@ -6,6 +6,7 @@
v-model = "selected" v-model = "selected"
@input = "onSelect()" @input = "onSelect()"
expanded> expanded>
<option value="">{{ $i18n.get('label_selectbox_init') }}...</option>
<option <option
v-for=" (option, index) in options" v-for=" (option, index) in options"
:key="index" :key="index"
@ -33,6 +34,7 @@
collection: '', collection: '',
field: '', field: '',
selected: '', selected: '',
taxonomy: ''
} }
}, },
props: { props: {
@ -42,7 +44,10 @@
field_id: [Number], // not required, but overrides the filter field id if is set field_id: [Number], // not required, but overrides the filter field id if is set
collection_id: [Number], // not required, but overrides the filter field id if is set collection_id: [Number], // not required, but overrides the filter field id if is set
filter_type: [String], // not required, but overrides the filter field type if is set filter_type: [String], // not required, but overrides the filter field type if is set
id: '' id: '',
query: {
type: Object // concentrate all attributes field id and type
}
}, },
watch: { watch: {
selected: function(val){ selected: function(val){
@ -54,6 +59,7 @@
getValuesCategory( taxonomy ){ getValuesCategory( taxonomy ){
return axios.get('/taxonomy/' + taxonomy + '/terms?hideempty=0' ).then( res => { return axios.get('/taxonomy/' + taxonomy + '/terms?hideempty=0' ).then( res => {
for (let item of res.data) { for (let item of res.data) {
this.taxonomy = item.taxonomy;
this.options.push(item); this.options.push(item);
} }
}) })
@ -72,6 +78,7 @@
promise.then( () => { promise.then( () => {
this.isLoading = false; this.isLoading = false;
this.selectedValues();
}) })
.catch( error => { .catch( error => {
this.$console.log('error select', error ); this.$console.log('error select', error );
@ -97,16 +104,28 @@
} }
return result; return result;
}, },
onSelect(){ selectedValues(){
this.$console.log(this.selected); if ( !this.query || !this.query.taxquery || !Array.isArray( this.query.taxquery ) )
return false;
let index = this.query.taxquery.findIndex(newField => newField.taxonomy === this.taxonomy );
if ( index >= 0){
let metadata = this.query.taxquery[ index ];
this.selected = metadata.terms;
} else {
return false;
}
},
onSelect(){
this.$emit('input', { this.$emit('input', {
filter: 'term', filter: 'selectbox',
compare: 'IN',
taxonomy: this.taxonomy,
field_id: this.field, field_id: this.field,
collection_id: this.collection, collection_id: this.collection,
value: this.selected terms: this.selected
}); });
} }
} }
} }
</script> </script>

View File

@ -20,6 +20,12 @@
this.collection = ( this.collection_id ) ? this.collection_id : this.filter.collection_id; this.collection = ( this.collection_id ) ? this.collection_id : this.filter.collection_id;
this.field = ( this.field_id ) ? this.field_id : this.filter.field.field_id ; this.field = ( this.field_id ) ? this.field_id : this.filter.field.field_id ;
this.type = ( this.filter_type ) ? this.filter_type : this.filter.field.field_type; this.type = ( this.filter_type ) ? this.filter_type : this.filter.field.field_type;
axios.get('/collection/'+ this.collection +'/fields/' + this.field + '?context=edit')
.then( res => {
let field = res.data;
this.selectedValues( field.field_type_options.taxonomy_id );
});
}, },
data(){ data(){
return { return {
@ -29,7 +35,8 @@
isLoading: false, isLoading: false,
type: '', type: '',
collection: '', collection: '',
field: '' field: '',
taxonomy: ''
} }
}, },
props: { props: {
@ -39,7 +46,10 @@
field_id: [Number], // not required, but overrides the filter field id if is set field_id: [Number], // not required, but overrides the filter field id if is set
collection_id: [Number], // not required, but overrides the filter field id if is set collection_id: [Number], // not required, but overrides the filter field id if is set
filter_type: [String], // not required, but overrides the filter field type if is set filter_type: [String], // not required, but overrides the filter field type if is set
id: '' id: '',
query: {
type: Object // concentrate all attributes field id and type
}
}, },
watch: { watch: {
selected( value ){ selected( value ){
@ -52,9 +62,11 @@
} }
this.$emit('input', { this.$emit('input', {
filter: 'taginput', filter: 'taginput',
compare: 'IN',
taxonomy: this.taxonomy,
field_id: ( this.field_id ) ? this.field_id : this.filter.field, field_id: ( this.field_id ) ? this.field_id : this.filter.field,
collection_id: ( this.collection_id ) ? this.collection_id : this.filter.collection_id, collection_id: ( this.collection_id ) ? this.collection_id : this.filter.collection_id,
value: values terms: values
}); });
} }
}, },
@ -84,14 +96,38 @@
getValuesCategory( taxonomy, query ){ getValuesCategory( taxonomy, query ){
return axios.get('/taxonomy/' + taxonomy + '/terms?hideempty=0' ).then( res => { return axios.get('/taxonomy/' + taxonomy + '/terms?hideempty=0' ).then( res => {
for (let term of res.data) { for (let term of res.data) {
if( term.name.toLowerCase().indexOf( query.toLowerCase() ) >= 0 ) if( term.name.toLowerCase().indexOf( query.toLowerCase() ) >= 0 ){
this.taxonomy = term.taxonomy;
this.options.push({label: term.name, value: term.id}); this.options.push({label: term.name, value: term.id});
}
} }
}) })
.catch(error => { .catch(error => {
this.$console.log(error); this.$console.log(error);
}); });
},
selectedValues( taxonomy ){
if ( !this.query || !this.query.taxquery || !Array.isArray( this.query.taxquery ) )
return false;
let index = this.query.taxquery.findIndex(newField => newField.taxonomy === this.taxonomy );
if ( index >= 0){
let metadata = this.query.taxquery[ index ];
for ( let id of metadata.terms ){
this.getTerm( taxonomy, id );
}
} else {
return false;
}
},
getTerm( taxonomy, id ){
return axios.get('/taxonomy/' + taxonomy + '/terms/' + id ).then( res => {
this.$console.log(res);
})
.catch(error => {
this.$console.log(error);
});
} }
} }
} }
</script> </script>

View File

@ -13,12 +13,18 @@ export const eventBusSearch = new Vue({
created(){ created(){
this.$on('input', data => { this.$on('input', data => {
store.dispatch('search/setPage', 1); store.dispatch('search/setPage', 1);
this.add_metaquery(data)
if( data.taxonomy ){
this.add_taxquery(data);
} else {
this.add_metaquery(data);
}
this.updateURLQueries(); this.updateURLQueries();
}); });
}, },
watch: { watch: {
'$route.query' () { '$route.query' () {
if (this.$route.name == 'CollectionItemsPage' || this.$route.name == 'ItemsPage') { if (this.$route.name == 'CollectionItemsPage' || this.$route.name == 'ItemsPage') {
if (this.$route.query.perpage == undefined) if (this.$route.query.perpage == undefined)
this.$route.query.perpage = 12; this.$route.query.perpage = 12;
@ -32,7 +38,7 @@ export const eventBusSearch = new Vue({
store.dispatch('search/set_postquery', this.$route.query); store.dispatch('search/set_postquery', this.$route.query);
//console.log(this.$route.query); //console.log(this.$route.query);
this.loadItems(); this.loadItems();
} }
} }
}, },
methods: { methods: {
@ -41,6 +47,11 @@ export const eventBusSearch = new Vue({
store.dispatch('search/add_metaquery', data ); store.dispatch('search/add_metaquery', data );
} }
}, },
add_taxquery( data ){
if ( data && data.collection_id ){
store.dispatch('search/add_taxquery', data );
}
},
getErrors( filter_id ){ getErrors( filter_id ){
let error = this.errors.find( errorItem => errorItem.field_id === filter_id ); let error = this.errors.find( errorItem => errorItem.field_id === filter_id );
return ( error ) ? error.errors : false return ( error ) ? error.errors : false
@ -79,7 +90,7 @@ export const eventBusSearch = new Vue({
store.dispatch('search/set_postquery', this.$route.query); store.dispatch('search/set_postquery', this.$route.query);
}, },
loadItems() { loadItems() {
this.$emit( 'isLoadingItems', true); this.$emit( 'isLoadingItems', true);
store.dispatch('collection/fetchItems', this.$route.params.collectionId).then((res) => { store.dispatch('collection/fetchItems', this.$route.params.collectionId).then((res) => {
this.$emit( 'isLoadingItems', false); this.$emit( 'isLoadingItems', false);
this.$emit( 'hasFiltered', res.hasFiltered); this.$emit( 'hasFiltered', res.hasFiltered);
@ -109,4 +120,4 @@ export const eventBusSearch = new Vue({
return components; return components;
}, },
} }
}); });

View File

@ -16,6 +16,15 @@ export const add_metaquery = ( { commit }, filter ) => {
} }
}; };
// Tax Queries from filters
export const add_taxquery = ( { commit }, filter ) => {
if( filter && filter.terms.length === 0 ){
commit('removeTaxQuery', filter );
} else {
commit('addTaxQuery', filter );
}
};
export const remove_metaquery = ( { commit }, filter ) => { export const remove_metaquery = ( { commit }, filter ) => {
commit('removeMetaQuery', filter ); commit('removeMetaQuery', filter );
}; };
@ -45,7 +54,7 @@ export const setOrderBy = ({ commit }, orderBy ) => {
} else if (orderBy.field_type_object.primitive_type == 'date') { } else if (orderBy.field_type_object.primitive_type == 'date') {
commit('setPostQueryAttribute', { attr: 'meta_key', value: orderBy.id } ); commit('setPostQueryAttribute', { attr: 'meta_key', value: orderBy.id } );
commit('setPostQueryAttribute', { attr: 'meta_type', value: 'DATETIME' } ); commit('setPostQueryAttribute', { attr: 'meta_type', value: 'DATETIME' } );
commit('setPostQueryAttribute', { attr: 'orderby', value: 'meta_value' } ); commit('setPostQueryAttribute', { attr: 'orderby', value: 'meta_value' } );
} else if (orderBy.field_type_object.core) { } else if (orderBy.field_type_object.core) {
commit('setPostQueryAttribute', { attr: 'orderby', value: orderBy.field_type_object.related_mapped_prop } ); commit('setPostQueryAttribute', { attr: 'orderby', value: orderBy.field_type_object.related_mapped_prop } );
} else { } else {
@ -57,5 +66,3 @@ export const setOrderBy = ({ commit }, orderBy ) => {
export const setOrder = ({ commit }, order ) => { export const setOrder = ({ commit }, order ) => {
commit('setPostQueryAttribute', { attr: 'order', value: order } ); commit('setPostQueryAttribute', { attr: 'order', value: order } );
}; };

View File

@ -7,7 +7,7 @@ export const setPostQueryAttribute = ( state, { attr, value }) => {
export const setPostQuery = ( state, postquery ) => { export const setPostQuery = ( state, postquery ) => {
state.postquery = postquery; state.postquery = postquery;
}; };
export const addMetaQuery = ( state, filter ) => { export const addMetaQuery = ( state, filter ) => {
state.postquery.metaquery = ( ! state.postquery.metaquery ) ? [] : state.postquery.metaquery; state.postquery.metaquery = ( ! state.postquery.metaquery ) ? [] : state.postquery.metaquery;
let index = state.postquery.metaquery.findIndex( item => item.key === filter.field_id); let index = state.postquery.metaquery.findIndex( item => item.key === filter.field_id);
@ -28,6 +28,24 @@ export const addMetaQuery = ( state, filter ) => {
} }
}; };
export const addTaxQuery = ( state, filter ) => {
state.postquery.taxquery = ( ! state.postquery.taxquery ) ? [] : state.postquery.taxquery;
let index = state.postquery.taxquery.findIndex( item => item.taxonomy === filter.taxonomy);
if ( index >= 0 ){
Vue.set( state.postquery.taxquery, index, {
taxonomy: filter.taxonomy,
terms: filter.terms,
compare: filter.compare
} );
}else{
state.postquery.taxquery.push({
taxonomy: filter.taxonomy,
terms: filter.terms,
compare: filter.compare
});
}
};
export const removeMetaQuery = ( state, filter ) => { export const removeMetaQuery = ( state, filter ) => {
let index = state.postquery.metaquery.findIndex( item => item.key === filter.field_id); let index = state.postquery.metaquery.findIndex( item => item.key === filter.field_id);
if (index >= 0) { if (index >= 0) {
@ -35,7 +53,14 @@ export const removeMetaQuery = ( state, filter ) => {
} }
}; };
export const removeTaxQuery = ( state, filter ) => {
let index = state.postquery.taxquery.findIndex( item => item.taxonomy === filter.taxonomy);
if (index >= 0) {
state.postquery.taxquery.splice(index, 1);
}
};
export const setTotalItems = ( state, total ) => { export const setTotalItems = ( state, total ) => {
state.totalItems = total; state.totalItems = total;
}; };