Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework: Refactor PostTaxonomies to use withApiData HoC #2537

Merged
merged 3 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 34 additions & 52 deletions editor/sidebar/post-taxonomies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
<PanelBody
title={ __( 'Categories & Tags' ) }
opened={ this.props.isOpened }
onToggle={ this.props.onTogglePanel }
>
{ availableTaxonomies.map( ( taxonomy ) => {
const TaxonomyComponent = taxonomy.hierarchical ? HierarchicalTermSelector : FlatTermSelector;
return (
<TaxonomyComponent
key={ taxonomy.slug }
label={ taxonomy.name }
restBase={ taxonomy.rest_base }
slug={ taxonomy.slug }
/>
);
} ) }
</PanelBody>
);
}
return (
<PanelBody
title={ __( 'Categories & Tags' ) }
opened={ isOpened }
onToggle={ onTogglePanel }
>
{ availableTaxonomies.map( ( taxonomy ) => {
Copy link
Member

@aduth aduth Aug 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To the above point, Lodash's reduce similarly handles undefined and object types, so this could even manage the filtering and mapping in one, ensuring the panel heading is always shown.

<PanelBody
	title={ __( 'Categories & Tags' ) }
	opened={ isOpened }
	onToggle={ onTogglePanel }
>
	{ reduce( taxonomies.data, ( result, taxonomy ) => {
		if ( includes( taxonomy.types, postType ) ) {
			const TaxonomyComponent = taxonomy.hierarchical 
				? HierarchicalTermSelector 
				: FlatTermSelector;

			result.push(
				<TaxonomyComponent
					key={ taxonomy.slug }
					label={ taxonomy.name }
					restBase={ taxonomy.rest_base }
					slug={ taxonomy.slug }
				/>
			);
		}

		return result;
	}, [] ) }
</PanelBody>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you know I prefer filter + map over reduce on small loops. I do understand the performance reasons for long loops.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant nothing of performance by my comment, filter / map or whatever is fine, just that we can skip the early return and account for undefined type with Lodash methods.

const TaxonomyComponent = taxonomy.hierarchical ? HierarchicalTermSelector : FlatTermSelector;
return (
<TaxonomyComponent
key={ taxonomy.slug }
label={ taxonomy.name }
restBase={ taxonomy.rest_base }
slug={ taxonomy.slug }
/>
);
} ) }
</PanelBody>
);
}

export default connect(
const applyConnect = connect(
( state ) => {
return {
postType: getCurrentPostType( state ),
Expand All @@ -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 );

1 change: 1 addition & 0 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down