From 2e8cd4fd86b70312bfb8739f8a6851771366f5dd Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Wed, 27 Feb 2019 14:34:03 -0300 Subject: [PATCH] Fixes queries for relationship value on frontend. --- .../filter-types/filter-types-mixin.js | 102 ++++++++++-------- .../filter-types/taginput/Taginput.vue | 6 +- .../relationship/Relationship.vue | 13 ++- .../collections-carousel/index.js | 2 +- .../tainacan-items/items-grid/index.js | 4 +- 5 files changed, 76 insertions(+), 51 deletions(-) diff --git a/src/classes/filter-types/filter-types-mixin.js b/src/classes/filter-types/filter-types-mixin.js index fb8afb770..d48cf6ff2 100644 --- a/src/classes/filter-types/filter-types-mixin.js +++ b/src/classes/filter-types/filter-types-mixin.js @@ -110,53 +110,71 @@ export const filter_type_mixin = { }, getValuesRelationship(collectionTarget, search, isRepositoryLevel, valuesToIgnore, offset, number, isInCheckboxModal, getSelected = '0') { - const source = axios.CancelToken.source(); + if (search || 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/' + this.filter.metadatum.metadatum_id + `?getSelected=${getSelected}&`; - else - url = '/collection/' + this.filter.collection_id + '/facets/' + this.filter.metadatum.metadatum_id + `?getSelected=${getSelected}&`; - - if(offset != undefined && number != undefined){ - url += `offset=${offset}&number=${number}`; - } else { - url += `nopaging=1` - } + 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){ - url += `&search=${search}`; - } + let url = ''; + if (isRepositoryLevel || this.filter.collection_id == 'filter_in_repository') + url = '/facets/' + this.filter.metadatum.metadatum_id + `?getSelected=${getSelected}&`; + else + url = '/collection/' + this.filter.collection_id + '/facets/' + this.filter.metadatum.metadatum_id + `?getSelected=${getSelected}&`; + + if(offset != undefined && number != undefined){ + url += `offset=${offset}&number=${number}`; + } else { + url += `nopaging=1` + } - this.isLoadingOptions = true; + if(search){ + url += `&search=${search}`; + } - return new Object ({ - request: - 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 = true; + + return new Object ({ + request: + axios.tainacan.get(url + '&fetch_only=thumbnail,title,id&' + qs.stringify(query_items)) + .then(res => { this.isLoadingOptions = false; - reject(thrown); - } - }), - source: source - }); + + if (res.data.values) + this.prepareOptionsForRelationship(res.data.values, search, valuesToIgnore, isInCheckboxModal); + else + 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 + }); + } else { + let callback = new Promise((resolve) => { + for (const facet in this.facetsFromItemSearch) { + if (facet == this.filter.id) { + this.prepareOptionsForRelationship(this.facetsFromItemSearch[facet], search, valuesToIgnore, isInCheckboxModal); + } + } + }); + return new Object ({ + request: callback + }); + } }, prepareOptionsForPlainText(metadata, search, valuesToIgnore, isInCheckboxModal) { @@ -224,7 +242,7 @@ export const filter_type_mixin = { let sResults = []; let opts = []; - if (res.data.length > 0) { + if (items.length > 0) { for (let item of items) { if (valuesToIgnore != undefined && valuesToIgnore.length > 0) { let indexToIgnore = valuesToIgnore.findIndex(value => value == item.value); diff --git a/src/classes/filter-types/taginput/Taginput.vue b/src/classes/filter-types/taginput/Taginput.vue index 7e890f6ff..65268309f 100644 --- a/src/classes/filter-types/taginput/Taginput.vue +++ b/src/classes/filter-types/taginput/Taginput.vue @@ -163,8 +163,10 @@ axios.get('/collection/' + collectionTarget + '/items?' + query) .then( res => { - for (let item of res.data) { - instance.selected.push({ label: item.title, value: item.id, img: item.thumbnail.thumbnail[0] }); + if (res.data.items) { + for (let item of res.data) { + instance.selected.push({ label: item.title, value: item.id, img: item.thumbnail.thumbnail[0] }); + } } }) .catch(error => { diff --git a/src/classes/metadata-types/relationship/Relationship.vue b/src/classes/metadata-types/relationship/Relationship.vue index ce2ab340f..cd1eb4e12 100644 --- a/src/classes/metadata-types/relationship/Relationship.vue +++ b/src/classes/metadata-types/relationship/Relationship.vue @@ -27,8 +27,10 @@ let query = qs.stringify({ postin: ( Array.isArray( this.metadatum.value ) ) ? this.metadatum.value : [ this.metadatum.value ] }); axios.get('/collection/'+collectionId+'/items?' + query + '&nopaging=1') .then( res => { - for (let item of res.data) { - this.selected.push({ label: item.title, value: item.id, img: '' }); + if (res.data.items) { + for (let item of res.data.items) { + this.selected.push({ label: item.title, value: item.id, img: '' }); + } } }) .catch(error => { @@ -115,8 +117,11 @@ this.loading = false; this.options = []; let result = res.data; - for (let item of result) { - this.options.push({ label: item.title, value: item.id }) + + if (result.items) { + for (let item of result.items) { + this.options.push({ label: item.title, value: item.id }) + } } }) .catch(error => { diff --git a/src/gutenberg-blocks/tainacan-collections/collections-carousel/index.js b/src/gutenberg-blocks/tainacan-collections/collections-carousel/index.js index d97f47873..349614214 100644 --- a/src/gutenberg-blocks/tainacan-collections/collections-carousel/index.js +++ b/src/gutenberg-blocks/tainacan-collections/collections-carousel/index.js @@ -123,7 +123,7 @@ registerBlockType('tainacan/collections-carousel', { return tainacan.get(`/collection/${collectionID}/items?perpage=3&paged=1&orderby=date`) .then(response => { - return response.data; + return response.data.items; }) .catch(error => { console.error(error); diff --git a/src/gutenberg-blocks/tainacan-items/items-grid/index.js b/src/gutenberg-blocks/tainacan-items/items-grid/index.js index fc99e2e00..9a00fe380 100644 --- a/src/gutenberg-blocks/tainacan-items/items-grid/index.js +++ b/src/gutenberg-blocks/tainacan-items/items-grid/index.js @@ -137,7 +137,7 @@ registerBlockType('tainacan/items-grid', { if(collectionID) { return tainacan.get(`/collection/${collectionID}/items?${query}`) .then(response => { - return response.data; + return response.data.items; }) .catch(error => { console.error(error); @@ -145,7 +145,7 @@ registerBlockType('tainacan/items-grid', { } else { return tainacan.get(`/items?${query}`) .then(response => { - return response.data; + return response.data.items; }) .catch(error => { console.error(error);