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

Components: Extract Reusable PostTaxonomies components #3313

Merged
merged 1 commit into from
Nov 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { FormTokenField } from '@wordpress/components';
/**
* Internal dependencies
*/
import { getEditedPostAttribute } from '../../selectors';
import { editPost } from '../../actions';
import { getEditedPostAttribute } from '../selectors';
import { editPost } from '../actions';

const DEFAULT_QUERY = {
per_page: 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { withInstanceId } from '@wordpress/components';
/**
* Internal dependencies
*/
import { getEditedPostAttribute } from '../../selectors';
import { editPost } from '../../actions';
import { getEditedPostAttribute } from '../selectors';
import { editPost } from '../actions';

const DEFAULT_QUERY = {
per_page: 100,
Expand Down
56 changes: 56 additions & 0 deletions editor/post-taxonomies/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* External Dependencies
*/
import { connect } from 'react-redux';
import { flowRight, filter } from 'lodash';

/**
* WordPress dependencies
*/
import { withAPIData } from '@wordpress/components';

/**
* Internal dependencies
*/
import './style.scss';
import HierarchicalTermSelector from './hierarchical-term-selector';
import FlatTermSelector from './flat-term-selector';
import { getCurrentPostType } from '../selectors';

function PostTaxonomies( { postType, taxonomies } ) {
const availableTaxonomies = filter( taxonomies.data, ( taxonomy ) => taxonomy.types.indexOf( postType ) !== -1 );

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

const applyConnect = connect(
( state ) => {
return {
postType: getCurrentPostType( state ),
};
},
);

const applyWithAPIData = withAPIData( () => ( {
taxonomies: '/wp/v2/taxonomies?context=edit',
} ) );

export default flowRight( [
applyConnect,
applyWithAPIData,
] )( PostTaxonomies );

39 changes: 7 additions & 32 deletions editor/sidebar/post-taxonomies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,40 @@
* External Dependencies
*/
import { connect } from 'react-redux';
import { flowRight, filter } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { PanelBody, withAPIData } from '@wordpress/components';
import { PanelBody } from '@wordpress/components';

/**
* Internal dependencies
*/
import './style.scss';
import HierarchicalTermSelector from './hierarchical-term-selector';
import FlatTermSelector from './flat-term-selector';
import { getCurrentPostType, isEditorSidebarPanelOpened } from '../../selectors';
import PostTaxonomiesForm from '../../post-taxonomies';
import { isEditorSidebarPanelOpened } from '../../selectors';
import { toggleSidebarPanel } from '../../actions';

/**
* Module Constants
*/
const PANEL_NAME = 'post-taxonomies';

function PostTaxonomies( { postType, taxonomies, isOpened, onTogglePanel } ) {
const availableTaxonomies = filter( taxonomies.data, ( taxonomy ) => taxonomy.types.indexOf( postType ) !== -1 );

function PostTaxonomies( { isOpened, onTogglePanel } ) {
return (
<PanelBody
title={ __( 'Categories & Tags' ) }
opened={ isOpened }
onToggle={ 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 }
/>
);
} ) }
<PostTaxonomiesForm />
</PanelBody>
);
}

const applyConnect = connect(
export default connect(
( state ) => {
return {
postType: getCurrentPostType( state ),
isOpened: isEditorSidebarPanelOpened( state, PANEL_NAME ),
};
},
Expand All @@ -60,14 +44,5 @@ const applyConnect = connect(
return toggleSidebarPanel( PANEL_NAME );
},
}
);

const applyWithAPIData = withAPIData( () => ( {
taxonomies: '/wp/v2/taxonomies?context=edit',
} ) );

export default flowRight( [
applyConnect,
applyWithAPIData,
] )( PostTaxonomies );
)( PostTaxonomies );