diff --git a/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/carousel-terms-modal.js b/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/carousel-terms-modal.js deleted file mode 100644 index 51ee66038..000000000 --- a/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/carousel-terms-modal.js +++ /dev/null @@ -1,309 +0,0 @@ -import tainacan from '../../api-client/axios.js'; -import axios from 'axios'; - -const { __ } = wp.i18n; - -const { TextControl, Button, Modal, CheckboxControl, Spinner } = wp.components; - -export default class TermsModal extends React.Component { - constructor(props) { - super(props); - - // Initialize state - this.state = { - searchTermName: '', - termsRequestSource: undefined, - terms: [], - temporarySelectedTerms: [], - isLoadingTerms: false, - modalTerms: [], - totalModalTerms: 0, - termsPerPage: 24, - termsPage: 1, - }; - - // Bind events - this.selectTemporaryTerm = this.selectTemporaryTerm.bind(this); - this.removeTemporaryTermOfId = this.removeTemporaryTermOfId.bind(this); - this.applySelectedTerms = this.applySelectedTerms.bind(this); - this.isTemporaryTermSelected = this.isTemporaryTermSelected.bind(this); - this.toggleSelectTemporaryTerm = this.toggleSelectTemporaryTerm.bind(this); - this.cancelSelection = this.cancelSelection.bind(this); - this.selectTerm = this.selectTerm.bind(this); - this.fetchModalTerms = this.fetchModalTerms.bind(this); - this.fetchTerms = this.fetchTerms.bind(this); - } - - componentWillMount() { - - this.fetchModalTerms(); - - this.setState( { - terms: [], - termsPage: 1, - temporarySelectedTerms: JSON.parse(JSON.stringify(this.props.selectedTermsObject)) - } ); - } - - selectTemporaryTerm(term) { - let existingTermIndex = this.state.temporarySelectedTerms.findIndex((existingTerm) => existingTerm.id == term.id); - - if (existingTermIndex < 0) { - let aTemporarySelectedTerms = this.state.temporarySelectedTerms; - aTemporarySelectedTerms.push({ - id: term.id, - name: term.name, - url: term.url, - thumbnail: term.thumbnail - }); - this.setState({ temporarySelectedTerms: aTemporarySelectedTerms }); - } - } - - removeTemporaryTermOfId(termId) { - - let existingTermIndex = this.state.temporarySelectedTerms.findIndex((existingTerm) => existingTerm.id == termId); - - if (existingTermIndex >= 0) { - let aTemporarySelectedTerms = this.state.temporarySelectedTerms; - aTemporarySelectedTerms.splice(existingTermIndex, 1); - this.setState({ temporarySelectedTerms: aTemporarySelectedTerms }); - } - } - - applySelectedTerms() { - let aSelectedTermsObject = JSON.parse(JSON.stringify(this.state.temporarySelectedTerms)); - this.props.onApplySelection(aSelectedTermsObject); - } - - isTemporaryTermSelected(termId) { - return this.state.temporarySelectedTerms.findIndex(term => term.id == termId) >= 0; - } - - toggleSelectTemporaryTerm(term, isChecked) { - if (isChecked) - this.selectTemporaryTerm(term); - else - this.removeTemporaryTermOfId(term.id); - } - - cancelSelection() { - - this.setState({ - termsPage: 1, - modalTerms: [] - }); - - this.props.onCancelSelection(); - } - - selectTerm(selectedTermId) { - - this.setState({ - termId: selectedTermId - }); - this.fetchTerm(); - this.fetchModalTerms(); - } - - fetchModalTerms() { - - let currentModalTerms = this.state.modalTerms; - if (this.state.termsPage <= 1) - currentModalTerms = []; - - let endpoint = '/terms/?orderby=title&order=asc&perpage=' + this.state.termsPerPage + '&paged=' + this.state.termsPage; - - this.setState({ - isLoadingTerms: true, - modalTerms: currentModalTerms, - }); - - tainacan.get(endpoint) - .then(response => { - - for (let term of response.data) { - currentModalTerms.push({ - name: term.name, - id: term.id, - url: term.url, - thumbnail: [{ - src: term.thumbnail['tainacan-medium'] != undefined ? term.thumbnail['tainacan-medium'][0] : term.thumbnail['medium'][0], - alt: term.name - }] - }); - } - - this.setState({ - termsPage: this.state.termsPage + 1, - isLoadingTerms: false, - modalTerms: currentModalTerms, - totalModalTerms: response.headers['x-wp-total'] - }); - - return currentModalTerms; - }) - .catch(error => { - console.log('Error trying to fetch terms: ' + error); - }); - } - - fetchTerms(name) { - if (this.state.termsRequestSource != undefined) - this.state.termsRequestSource.cancel('Previous terms search canceled.'); - - let aTermRequestSource = axios.CancelToken.source(); - this.setState({ - termsRequestSource: aTermRequestSource, - isLoadingTerms: true - }); - - let endpoint = '/terms/?orderby=title&order=asc&perpage=' + this.state.termsPerPage; - - if (name != undefined && name != '') - endpoint += '&search=' + name; - - tainacan.get(endpoint, { cancelToken: aTermRequestSource.token }) - .then(response => { - - let someTerms = this.state.terms; - someTerms = response.data.map((term) => ({ - name: term.name, - id: term.id, - url: term.url, - thumbnail: [{ - src: term.thumbnail['tainacan-medium'] != undefined ? term.thumbnail['tainacan-medium'][0] : term.thumbnail['medium'][0], - alt: term.name - }] - })); - - this.setState({ - isLoadingTerms: false, - terms: someTerms - }); - - return someTerms; - }) - .catch(error => { - console.log('Error trying to fetch terms: ' + error); - }); - } - - render() { - return ( - this.cancelSelection() } - contentLabel={__('Select terms', 'tainacan')}> - -
-
- { - this.setState({ - searchTermName: value.target.value - }); - }} - onChange={(value) => this.fetchTerms(value)}/> -
- {( - this.state.searchTermName != '' ? ( - - this.state.terms.length > 0 ? - ( -
-
    - { - this.state.terms.map((term) => -
  • - { term.thumbnail ? - { - : null - } - { this.toggleSelectTemporaryTerm(term, isChecked) } } - /> -
  • - ) - } -
- { this.state.isLoadingTerms ?
: null } -
- ) - : this.state.isLoadingTerms ?
: -
-

{ __('Sorry, no terms found.', 'tainacan') }

-
- ) : - this.state.modalTerms.length > 0 ? - ( -
-
    - { - this.state.modalTerms.map((term) => -
  • - { term.thumbnail ? - { - : null - } - { this.toggleSelectTemporaryTerm(term, isChecked) } } /> -
  • - ) - } - { this.state.isLoadingTerms ?
    : null } -
-
-

{ __('Showing', 'tainacan') + " " + this.state.modalTerms.length + " " + __('of', 'tainacan') + " " + this.state.totalModalTerms + " " + __('terms', 'tainacan') + "."}

- { - this.state.modalTerms.length < this.state.totalModalTerms ? ( - - ) : null - } -
-
- ) : this.state.isLoadingTerms ? : -
-

{ __('Sorry, no terms found.', 'tainacan') }

-
- )} -
- - -
-
-
- ); - } -} \ No newline at end of file diff --git a/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/index.js b/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/index.js index a6ea32bec..120470b37 100644 --- a/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/index.js +++ b/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/index.js @@ -6,7 +6,7 @@ const { RangeControl, Spinner, Button, BaseControl, ToggleControl, SelectControl const { InspectorControls } = wp.editor; -import CarouselTermsModal from './carousel-terms-modal.js'; +import TermsModal from '../terms-list/terms-modal.js'; import tainacan from '../../api-client/axios.js'; import axios from 'axios'; import qs from 'qs'; @@ -98,6 +98,10 @@ registerBlockType('tainacan/carousel-terms-list', { termTextColor: { type: String, default: "#ffffff" + }, + taxonomyId: { + type: String, + default: undefined } }, supports: { @@ -118,7 +122,8 @@ registerBlockType('tainacan/carousel-terms-list', { autoPlaySpeed, loopSlides, hideName, - showTermThumbnail + showTermThumbnail, + taxonomyId } = attributes; // Obtains block's client id to render it on save function @@ -214,7 +219,7 @@ registerBlockType('tainacan/carousel-terms-list', { terms = []; - let endpoint = '/terms?'+ qs.stringify({ postin: selectedTerms }) + '&fetch_only=name,url,thumbnail'; + let endpoint = '/taxonomy/' + taxonomyId + '/terms/?'+ qs.stringify({ postin: selectedTerms }) + '&fetch_only=name,url,header_image'; tainacan.get(endpoint, { cancelToken: itemsRequestSource.token }) .then(response => { @@ -401,18 +406,23 @@ registerBlockType('tainacan/carousel-terms-list', { (
{ isModalOpen ? - { - selectedTerms = selectedTerms.concat(aSelectionOfTerms.map((term) => { return term.id; })); - setAttributes({ - selectedTerms: selectedTerms, - isModalOpen: false - }); - setContent(); - }} - onCancelSelection={ () => setAttributes({ isModalOpen: false }) }/> - : null + { + taxonomyId = selectedTaxonomyId; + setAttributes({ taxonomyId: taxonomyId }); + }} + onApplySelection={ (aSelectionOfTerms) =>{ + selectedTerms = selectedTerms.concat(aSelectionOfTerms.map((term) => { return term.id; })); + setAttributes({ + selectedTerms: selectedTerms, + isModalOpen: false + }); + setContent(); + }} + onCancelSelection={ () => setAttributes({ isModalOpen: false }) }/> + : null } { terms.length ? ( @@ -530,7 +540,8 @@ registerBlockType('tainacan/carousel-terms-list', { autoPlaySpeed, loopSlides, hideName, - showTermThumbnail + showTermThumbnail, + taxonomyId } = attributes; return