From 9fd630ca8d585883c4fcbaacc28f7c7c96fbb71f Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Fri, 25 Aug 2017 12:55:45 +0100 Subject: [PATCH 1/3] Framework: Refactor PostTaxonomies to use `withApiData` HoC --- editor/sidebar/post-taxonomies/index.js | 88 +++++++++++-------------- 1 file changed, 38 insertions(+), 50 deletions(-) diff --git a/editor/sidebar/post-taxonomies/index.js b/editor/sidebar/post-taxonomies/index.js index a2fb94def1b99..dbf5a72538958 100644 --- a/editor/sidebar/post-taxonomies/index.js +++ b/editor/sidebar/post-taxonomies/index.js @@ -2,13 +2,13 @@ * External Dependencies */ import { connect } from 'react-redux'; +import { flowRight } from 'lodash'; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { PanelBody } from '@wordpress/components'; -import { Component } from '@wordpress/element'; +import { PanelBody, withAPIData } from '@wordpress/components'; /** * Internal dependencies @@ -24,58 +24,37 @@ import { toggleSidebarPanel } from '../../actions'; */ const PANEL_NAME = 'post-taxonomies'; -class PostTaxonomies extends Component { - constructor() { - super( ...arguments ); +function PostTaxonomies( { postType, taxonomies, isOpened, onTogglePanel } ) { + const availableTaxonomies = !! taxonomies.data + ? Object.values( taxonomies.data ).filter( ( taxonomy ) => taxonomy.types.indexOf( postType ) !== -1 ) + : []; - this.state = { - taxonomies: [], - }; - } - - componentDidMount() { - this.fetchTaxonomies = new wp.api.collections.Taxonomies() - .fetch() - .done( ( taxonomies ) => { - this.setState( { taxonomies: Object.values( taxonomies ) } ); - } ); - } - - componentWillUnmout() { - this.fetchTaxonomies.abort(); + if ( ! availableTaxonomies.length ) { + return null; } - render() { - const availableTaxonomies = this.state.taxonomies - .filter( ( taxonomy ) => taxonomy.types.indexOf( this.props.postType ) !== -1 ); - - if ( ! availableTaxonomies.length ) { - return null; - } - - return ( - - { availableTaxonomies.map( ( taxonomy ) => { - const TaxonomyComponent = taxonomy.hierarchical ? HierarchicalTermSelector : FlatTermSelector; - return ( - - ); - } ) } - - ); - } + return ( + + { availableTaxonomies.map( ( taxonomy ) => { + const TaxonomyComponent = taxonomy.hierarchical ? HierarchicalTermSelector : FlatTermSelector; + return ( + + ); + } ) } + + ); } -export default connect( +const applyConnect = connect( ( state ) => { return { postType: getCurrentPostType( state ), @@ -87,5 +66,14 @@ export default connect( return toggleSidebarPanel( PANEL_NAME ); }, } -)( PostTaxonomies ); +); + +const applyWithAPIData = withAPIData( () => ( { + taxonomies: '/wp/v2/taxonomies?context=edit', +} ) ); + +export default flowRight( [ + applyConnect, + applyWithAPIData, +] )( PostTaxonomies ); From d5346d3063a8cb09501e5fbc6c01c2ad6a605c72 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 4 Sep 2017 08:45:04 +0100 Subject: [PATCH 2/3] Taxonomies: use lodash's filter and avoid preloading check --- editor/sidebar/post-taxonomies/index.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/editor/sidebar/post-taxonomies/index.js b/editor/sidebar/post-taxonomies/index.js index dbf5a72538958..3df9060e7a7fb 100644 --- a/editor/sidebar/post-taxonomies/index.js +++ b/editor/sidebar/post-taxonomies/index.js @@ -2,7 +2,7 @@ * External Dependencies */ import { connect } from 'react-redux'; -import { flowRight } from 'lodash'; +import { flowRight, filter } from 'lodash'; /** * WordPress dependencies @@ -25,13 +25,7 @@ import { toggleSidebarPanel } from '../../actions'; const PANEL_NAME = 'post-taxonomies'; function PostTaxonomies( { postType, taxonomies, isOpened, onTogglePanel } ) { - const availableTaxonomies = !! taxonomies.data - ? Object.values( taxonomies.data ).filter( ( taxonomy ) => taxonomy.types.indexOf( postType ) !== -1 ) - : []; - - if ( ! availableTaxonomies.length ) { - return null; - } + const availableTaxonomies = filter( taxonomies.data, ( taxonomy ) => taxonomy.types.indexOf( postType ) !== -1 ); return ( Date: Mon, 4 Sep 2017 08:47:25 +0100 Subject: [PATCH 3/3] Taxonmies: Preload Taxonomies API --- lib/client-assets.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/client-assets.php b/lib/client-assets.php index 77ce1758361ad..da3c792bb8cf2 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -731,6 +731,7 @@ function gutenberg_editor_scripts_and_styles( $hook ) { // Preload common data. $preload_paths = array( '/wp/v2/users/me?context=edit', + '/wp/v2/taxonomies?context=edit', gutenberg_get_rest_link( $post_to_edit, 'about', 'edit' ), ); if ( ! $is_new_post ) {