diff --git a/editor/sidebar/post-taxonomies/index.js b/editor/sidebar/post-taxonomies/index.js
index a2fb94def1b99..3df9060e7a7fb 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, filter } 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,31 @@ import { toggleSidebarPanel } from '../../actions';
*/
const PANEL_NAME = 'post-taxonomies';
-class PostTaxonomies extends Component {
- constructor() {
- super( ...arguments );
+function PostTaxonomies( { postType, taxonomies, isOpened, onTogglePanel } ) {
+ const availableTaxonomies = filter( taxonomies.data, ( 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();
- }
-
- 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 +60,14 @@ export default connect(
return toggleSidebarPanel( PANEL_NAME );
},
}
-)( PostTaxonomies );
+);
+
+const applyWithAPIData = withAPIData( () => ( {
+ taxonomies: '/wp/v2/taxonomies?context=edit',
+} ) );
+
+export default flowRight( [
+ applyConnect,
+ applyWithAPIData,
+] )( PostTaxonomies );
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 ) {