Adds request cancelation to facet load options search

This commit is contained in:
Mateus Machado Luna 2018-10-16 17:11:01 -03:00
parent 44775512cd
commit c0526ca4a9
8 changed files with 222 additions and 155 deletions

View File

@ -168,6 +168,7 @@ export default {
this.arrayRealPath = this.arrayRealPath.filter((item) => item.length != 0); this.arrayRealPath = this.arrayRealPath.filter((item) => item.length != 0);
this.generateViewPath(); this.generateViewPath();
} }
}, },
methods: { methods: {

View File

@ -359,13 +359,17 @@
getOptions(offset){ getOptions(offset){
let promise = ''; let promise = '';
// Cancels previous Request
if (this.getOptionsValuesCancel != undefined)
this.getOptionsValuesCancel.cancel('Facet search Canceled.');
if ( this.metadatum_type === 'Tainacan\\Metadata_Types\\Relationship' ) { if ( this.metadatum_type === 'Tainacan\\Metadata_Types\\Relationship' ) {
let collectionTarget = ( this.metadatum_object && this.metadatum_object.metadata_type_options.collection_id ) ? let collectionTarget = ( this.metadatum_object && this.metadatum_object.metadata_type_options.collection_id ) ?
this.metadatum_object.metadata_type_options.collection_id : this.collection_id; this.metadatum_object.metadata_type_options.collection_id : this.collection_id;
promise = this.getValuesRelationship( collectionTarget, this.optionName, [], offset, this.maxNumOptionsCheckboxList, true); promise = this.getValuesRelationship( collectionTarget, this.optionName, [], offset, this.maxNumOptionsCheckboxList, true);
promise promise.request
.then(() => { .then(() => {
this.isCheckboxListLoading = false; this.isCheckboxListLoading = false;
this.isSearchingLoading = false; this.isSearchingLoading = false;
@ -376,7 +380,7 @@
} else { } else {
promise = this.getValuesPlainText( this.metadatum_id, this.optionName, this.isRepositoryLevel, [], offset, this.maxNumOptionsCheckboxList, true); promise = this.getValuesPlainText( this.metadatum_id, this.optionName, this.isRepositoryLevel, [], offset, this.maxNumOptionsCheckboxList, true);
promise promise.request
.then(() => { .then(() => {
this.isCheckboxListLoading = false; this.isCheckboxListLoading = false;
this.isSearchingLoading = false; this.isSearchingLoading = false;
@ -385,6 +389,9 @@
this.$console.log(error); this.$console.log(error);
}) })
} }
// Search Request Token for cancelling
this.getOptionsValuesCancel = promise.source;
}, },
autoComplete: _.debounce( function () { autoComplete: _.debounce( function () {
this.isSearching = !!this.optionName.length; this.isSearching = !!this.optionName.length;

View File

@ -98,6 +98,12 @@
if (query != '') { if (query != '') {
let promise = null; let promise = null;
this.options = []; this.options = [];
// Cancels previous Request
if (this.getOptionsValuesCancel != undefined)
this.getOptionsValuesCancel.cancel('Facet search Canceled.');
if ( this.type === 'Tainacan\\Metadata_Types\\Relationship' ) { if ( this.type === 'Tainacan\\Metadata_Types\\Relationship' ) {
let collectionTarget = ( this.metadatum_object && this.metadatum_object.metadata_type_options.collection_id ) ? let collectionTarget = ( this.metadatum_object && this.metadatum_object.metadata_type_options.collection_id ) ?
this.metadatum_object.metadata_type_options.collection_id : this.collection_id; this.metadatum_object.metadata_type_options.collection_id : this.collection_id;
@ -107,9 +113,13 @@
promise = this.getValuesPlainText( this.metadatum, query, this.isRepositoryLevel ); promise = this.getValuesPlainText( this.metadatum, query, this.isRepositoryLevel );
} }
promise.catch( error => { promise.request.catch( error => {
this.$console.log('error select', error ); this.$console.log('error select', error );
}); });
// Search Request Token for cancelling
this.getOptionsValuesCancel = promise.source;
} else { } else {
this.cleanSearch(); this.cleanSearch();
} }

View File

@ -32,6 +32,7 @@
export default { export default {
created(){ created(){
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.metadatum = ( this.metadatum_id ) ? this.metadatum_id : this.filter.metadatum.metadatum_id; this.metadatum = ( this.metadatum_id ) ? this.metadatum_id : this.filter.metadatum.metadatum_id;
const vm = this; const vm = this;
@ -81,46 +82,56 @@
methods: { methods: {
loadOptions(){ loadOptions(){
let promise = null; let promise = null;
this.isLoading = true;
// Cancels previous Request
if (this.getOptionsValuesCancel != undefined)
this.getOptionsValuesCancel.cancel('Facet search Canceled.');
if ( this.type === 'Tainacan\\Metadata_Types\\Relationship' ) { if ( this.type === 'Tainacan\\Metadata_Types\\Relationship' ) {
this.isLoading = true;
let collectionTarget = ( this.metadatum_object && this.metadatum_object.metadata_type_options.collection_id ) ? let collectionTarget = ( this.metadatum_object && this.metadatum_object.metadata_type_options.collection_id ) ?
this.metadatum_object.metadata_type_options.collection_id : this.collection_id; this.metadatum_object.metadata_type_options.collection_id : this.collection_id;
promise = this.getValuesRelationship( collectionTarget, null, [], 0, this.filter.max_options, false, '1'); promise = this.getValuesRelationship( collectionTarget, null, [], 0, this.filter.max_options, false, '1');
promise promise.request
.then(() => { .then(() => {
this.isLoading = false; this.isLoading = false;
if(this.options.length > this.filter.max_options){ if(this.options.length > this.filter.max_options){
this.options.splice(this.filter.max_options); this.options.splice(this.filter.max_options);
} }
this.selectedValues();
}).catch((error) => { }).catch((error) => {
this.$console.error(error); this.$console.error(error);
}) })
} else { } else {
this.isLoading = true;
promise = this.getValuesPlainText( this.metadatum, null, this.isRepositoryLevel, [], 0, this.filter.max_options, false, '1' ); promise = this.getValuesPlainText( this.metadatum, null, this.isRepositoryLevel, [], 0, this.filter.max_options, false, '1' );
promise promise.request
.then(() => { .then(() => {
this.isLoading = false; this.isLoading = false;
if(this.options.length > this.filter.max_options){ if(this.options.length > this.filter.max_options){
this.options.splice(this.filter.max_options); this.options.splice(this.filter.max_options);
} }
this.selectedValues();
}).catch((error) => { }).catch((error) => {
this.$console.error(error); this.$console.error(error);
}) });
} }
promise // promise.request
.then(() => { // .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 );
this.isLoading = false; // this.isLoading = false;
}); // });
// Search Request Token for cancelling
this.getOptionsValuesCancel = promise.source;
}, },
onSelect(){ onSelect(){
this.$emit('input', { this.$emit('input', {

View File

@ -1,10 +1,11 @@
import qs from 'qs'; import qs from 'qs';
import { tainacan as axios } from '../../js/axios/axios'; import axios from '../../js/axios/axios';
export const filter_type_mixin = { export const filter_type_mixin = {
data () { data () {
return { return {
thumbPlaceholderPath: tainacan_plugin.base_url + '/admin/images/placeholder_square.png' thumbPlaceholderPath: tainacan_plugin.base_url + '/admin/images/placeholder_square.png',
getOptionsValuesCancel: undefined
} }
}, },
props: { props: {
@ -19,11 +20,16 @@ export const filter_type_mixin = {
}, },
methods: { methods: {
getValuesPlainText(metadatumId, search, isRepositoryLevel, valuesToIgnore, offset, number, isInCheckboxModal, getSelected = '0') { getValuesPlainText(metadatumId, search, isRepositoryLevel, valuesToIgnore, offset, number, isInCheckboxModal, getSelected = '0') {
const source = axios.CancelToken.source();
let currentQuery = JSON.parse(JSON.stringify(this.query)); let currentQuery = JSON.parse(JSON.stringify(this.query));
if (currentQuery.fetch_only != undefined) {
for (let key of Object.keys(currentQuery.fetch_only)) { for (let key of Object.keys(currentQuery.fetch_only)) {
if (currentQuery.fetch_only[key] == null) if (currentQuery.fetch_only[key] == null)
delete currentQuery.fetch_only[key]; delete currentQuery.fetch_only[key];
} }
}
let query_items = { 'current_query': currentQuery }; let query_items = { 'current_query': currentQuery };
let url = `/collection/${this.collection}/facets/${metadatumId}?getSelected=${getSelected}&`; let url = `/collection/${this.collection}/facets/${metadatumId}?getSelected=${getSelected}&`;
@ -44,7 +50,9 @@ export const filter_type_mixin = {
url += qs.stringify(query_items); url += qs.stringify(query_items);
} }
return axios.get(url) return new Object ({
request:
axios.tainacan.get(url, { cancelToken: source.token })
.then(res => { .then(res => {
let sResults = []; let sResults = [];
let opts = []; let opts = [];
@ -103,16 +111,27 @@ export const filter_type_mixin = {
} }
}) })
.catch(error => { .catch((thrown) => {
this.$console.error(error); if (axios.isCancel(thrown)) {
console.log('Request canceled: ', thrown.message);
} else {
reject(thrown);
}
}),
source: source
}); });
}, },
getValuesRelationship(collectionTarget, search, valuesToIgnore, offset, number, isInCheckboxModal, getSelected = '0') { getValuesRelationship(collectionTarget, search, valuesToIgnore, offset, number, isInCheckboxModal, getSelected = '0') {
const source = axios.CancelToken.source();
let currentQuery = JSON.parse(JSON.stringify(this.query)); let currentQuery = JSON.parse(JSON.stringify(this.query));
if (currentQuery.fetch_only != undefined) {
for (let key of Object.keys(currentQuery.fetch_only)) { for (let key of Object.keys(currentQuery.fetch_only)) {
if (currentQuery.fetch_only[key] == null) if (currentQuery.fetch_only[key] == null)
delete currentQuery.fetch_only[key]; delete currentQuery.fetch_only[key];
} }
}
let query_items = { 'current_query': currentQuery }; let query_items = { 'current_query': currentQuery };
let url = '/collection/' + this.filter.collection_id + '/facets/' + this.filter.metadatum.metadatum_id + `?getSelected=${getSelected}&`; let url = '/collection/' + this.filter.collection_id + '/facets/' + this.filter.metadatum.metadatum_id + `?getSelected=${getSelected}&`;
@ -126,7 +145,9 @@ export const filter_type_mixin = {
url += `&search=${search}`; url += `&search=${search}`;
} }
return axios.get(url + '&fetch_only[0]=thumbnail&fetch_only[1]=title&fetch_only[2]=id&' + qs.stringify(query_items)) return new Object ({
request:
axios.tainacan.get(url + '&fetch_only[0]=thumbnail&fetch_only[1]=title&fetch_only[2]=id&' + qs.stringify(query_items))
.then(res => { .then(res => {
let sResults = []; let sResults = [];
let opts = []; let opts = [];
@ -191,6 +212,8 @@ export const filter_type_mixin = {
}) })
.catch(error => { .catch(error => {
this.$console.error(error); this.$console.error(error);
}),
source: source
}); });
} }
} }

View File

@ -78,16 +78,24 @@
loadOptions(){ loadOptions(){
this.isLoading = true; this.isLoading = true;
// Cancels previous Request
if (this.getOptionsValuesCancel != undefined)
this.getOptionsValuesCancel.cancel('Facet search Canceled.');
let promise = null; let promise = null;
promise = this.getValuesPlainText( this.metadatum, null, this.isRepositoryLevel ); promise = this.getValuesPlainText( this.metadatum, null, this.isRepositoryLevel );
promise.then(() => { promise.request
.then(() => {
this.isLoading = false; this.isLoading = false;
}) })
.catch( error => { .catch( error => {
this.$console.error('error select', error ); this.$console.error('error select', error );
this.isLoading = false; this.isLoading = false;
}); });
// Search Request Token for cancelling
this.getOptionsValuesCancel = promise.source;
}, },
onSelect(value){ onSelect(value){
this.selected = value; this.selected = value;

View File

@ -112,6 +112,10 @@
for(let val of this.selected) for(let val of this.selected)
valuesToIgnore.push( val.value ); valuesToIgnore.push( val.value );
// Cancels previous Request
if (this.getOptionsValuesCancel != undefined)
this.getOptionsValuesCancel.cancel('Facet search Canceled.');
if ( this.type === 'Tainacan\\Metadata_Types\\Relationship' ) { if ( this.type === 'Tainacan\\Metadata_Types\\Relationship' ) {
let collectionTarget = ( this.metadatum_object && this.metadatum_object.metadata_type_options.collection_id ) ? let collectionTarget = ( this.metadatum_object && this.metadatum_object.metadata_type_options.collection_id ) ?
this.metadatum_object.metadata_type_options.collection_id : this.collection_id; this.metadatum_object.metadata_type_options.collection_id : this.collection_id;
@ -121,10 +125,14 @@
promise = this.getValuesPlainText( this.metadatum, query, this.isRepositoryLevel, valuesToIgnore ); promise = this.getValuesPlainText( this.metadatum, query, this.isRepositoryLevel, valuesToIgnore );
} }
promise promise.request
.catch( error => { .catch( error => {
this.$console.log('error select', error ); this.$console.log('error select', error );
}); });
// Search Request Token for cancelling
this.getOptionsValuesCancel = promise.source;
}, 500), }, 500),
selectedValues(){ selectedValues(){
const instance = this; const instance = this;

View File

@ -104,7 +104,6 @@ export const fetchItems = ({ rootGetters, dispatch, commit }, { collectionId, is
}), }),
source: source source: source
}) })
}; };
export const deleteItem = ({ commit }, { itemId, isPermanently }) => { export const deleteItem = ({ commit }, { itemId, isPermanently }) => {