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 ))
.then( res => {
this.loading = false;
this.options = [];
let result = res.data;
for (let item of result) {
this.options.push({ label: item.title, value: item.id })

View File

@ -32,6 +32,7 @@
collection: '',
field: '',
selected: [],
taxonomy: ''
}
},
props: {
@ -41,7 +42,10 @@
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
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: {
selected: function(val){
@ -53,6 +57,7 @@
getValuesCategory( taxonomy ){
return axios.get('/taxonomy/' + taxonomy + '/terms?hideempty=0' ).then( res => {
for (let item of res.data) {
this.taxonomy = item.taxonomy;
this.options.push(item);
}
})
@ -71,6 +76,7 @@
promise.then( () => {
this.isLoading = false;
this.selectedValues()
})
.catch( error => {
this.$console.log('error select', error );
@ -96,14 +102,26 @@
}
return result;
},
onSelect(){
this.$console.log(this.selected);
selectedValues(){
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', {
filter: 'term',
filter: 'selectbox',
taxonomy: this.taxonomy,
compare: 'IN',
field_id: this.field,
collection_id: this.collection,
value: this.selected
terms: this.selected
});
}
}

View File

@ -6,6 +6,7 @@
v-model = "selected"
@input = "onSelect()"
expanded>
<option value="">{{ $i18n.get('label_selectbox_init') }}...</option>
<option
v-for=" (option, index) in options"
:key="index"
@ -33,6 +34,7 @@
collection: '',
field: '',
selected: '',
taxonomy: ''
}
},
props: {
@ -42,7 +44,10 @@
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
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: {
selected: function(val){
@ -54,6 +59,7 @@
getValuesCategory( taxonomy ){
return axios.get('/taxonomy/' + taxonomy + '/terms?hideempty=0' ).then( res => {
for (let item of res.data) {
this.taxonomy = item.taxonomy;
this.options.push(item);
}
})
@ -72,6 +78,7 @@
promise.then( () => {
this.isLoading = false;
this.selectedValues();
})
.catch( error => {
this.$console.log('error select', error );
@ -97,14 +104,26 @@
}
return result;
},
onSelect(){
this.$console.log(this.selected);
selectedValues(){
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', {
filter: 'term',
filter: 'selectbox',
compare: 'IN',
taxonomy: this.taxonomy,
field_id: this.field,
collection_id: this.collection,
value: this.selected
terms: this.selected
});
}
}

View File

@ -20,6 +20,12 @@
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.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(){
return {
@ -29,7 +35,8 @@
isLoading: false,
type: '',
collection: '',
field: ''
field: '',
taxonomy: ''
}
},
props: {
@ -39,7 +46,10 @@
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
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: {
selected( value ){
@ -52,9 +62,11 @@
}
this.$emit('input', {
filter: 'taginput',
compare: 'IN',
taxonomy: this.taxonomy,
field_id: ( this.field_id ) ? this.field_id : this.filter.field,
collection_id: ( this.collection_id ) ? this.collection_id : this.filter.collection_id,
value: values
terms: values
});
}
},
@ -84,13 +96,37 @@
getValuesCategory( taxonomy, query ){
return axios.get('/taxonomy/' + taxonomy + '/terms?hideempty=0' ).then( res => {
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});
}
}
})
.catch(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);
});
}
}
}

View File

@ -13,7 +13,13 @@ export const eventBusSearch = new Vue({
created(){
this.$on('input', data => {
store.dispatch('search/setPage', 1);
this.add_metaquery(data)
if( data.taxonomy ){
this.add_taxquery(data);
} else {
this.add_metaquery(data);
}
this.updateURLQueries();
});
},
@ -41,6 +47,11 @@ export const eventBusSearch = new Vue({
store.dispatch('search/add_metaquery', data );
}
},
add_taxquery( data ){
if ( data && data.collection_id ){
store.dispatch('search/add_taxquery', data );
}
},
getErrors( filter_id ){
let error = this.errors.find( errorItem => errorItem.field_id === filter_id );
return ( error ) ? error.errors : false

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 ) => {
commit('removeMetaQuery', filter );
};
@ -57,5 +66,3 @@ export const setOrderBy = ({ commit }, orderBy ) => {
export const setOrder = ({ commit }, order ) => {
commit('setPostQueryAttribute', { attr: 'order', value: order } );
};

View File

@ -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 ) => {
let index = state.postquery.metaquery.findIndex( item => item.key === filter.field_id);
if (index >= 0) {
@ -35,6 +53,13 @@ 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 ) => {
state.totalItems = total;