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

Conversation

youknowriad
Copy link
Contributor

Just some more consistency in fetching API DATA

@youknowriad youknowriad added the Framework Issues related to broader framework topics, especially as it relates to javascript label Aug 25, 2017
@youknowriad youknowriad self-assigned this Aug 25, 2017
@youknowriad youknowriad requested a review from aduth August 25, 2017 11:58
@youknowriad
Copy link
Contributor Author

@aduth Do you have any idea how to use withAPIData when they depend on a local state value? (Thinking about the TermSelectors)? I guess one option would be to store this state value in Redux, but seems weird to do so just for this?

@aduth
Copy link
Member

aduth commented Aug 25, 2017

Do you have any idea how to use withAPIData when they depend on a local state value?

You could have a parent component which manages the state, then passes the state as a prop to a child component, then available to withAPIData.

This is precisely how search is implemented in Calypso's TermTreeSelector component.

@youknowriad youknowriad force-pushed the update/post-taxonomies-with-api-data branch from 358550e to 9fd630c Compare August 31, 2017 10:04
@youknowriad
Copy link
Contributor Author

Can I get a 👍 or 👎?

@codecov
Copy link

codecov bot commented Aug 31, 2017

Codecov Report

Merging #2537 into master will increase coverage by 0.53%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2537      +/-   ##
==========================================
+ Coverage   31.42%   31.95%   +0.53%     
==========================================
  Files         177      177              
  Lines        5407     5576     +169     
  Branches      946      981      +35     
==========================================
+ Hits         1699     1782      +83     
- Misses       3135     3197      +62     
- Partials      573      597      +24
Impacted Files Coverage Δ
editor/sidebar/post-taxonomies/index.js 0% <0%> (ø) ⬆️
blocks/library/latest-posts/index.js 7.4% <0%> (-2.6%) ⬇️
...ebar/post-taxonomies/hierarchical-term-selector.js 0% <0%> (ø) ⬆️
blocks/api/registration.js 100% <0%> (ø) ⬆️
editor/inserter/menu.js 48.46% <0%> (+0.05%) ⬆️
blocks/library/embed/index.js 60.31% <0%> (+14.86%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5885233...f129648. Read the comment docs.

super( ...arguments );
function PostTaxonomies( { postType, taxonomies, isOpened, onTogglePanel } ) {
const availableTaxonomies = !! taxonomies.data
? Object.values( taxonomies.data ).filter( ( taxonomy ) => taxonomy.types.indexOf( postType ) !== -1 )
Copy link
Member

Choose a reason for hiding this comment

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

Minor: Lodash's filter handles both undefined and Object types, so this could be simplified:

const availableTaxonomies = filter( taxonomies.data, ( { types } ) => includes( types, postType ) );


componentWillUnmout() {
this.fetchTaxonomies.abort();
if ( ! availableTaxonomies.length ) {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should seek to avoid content flickering in and out of the sidebar as the page loads. Especially if we're more likely to have taxonomies than not, we could at least show the panel heading, even if the body can't be known yet. Alternatively, #2501 was designed for this purpose, and we could consider preloading taxonomies.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Framework Issues related to broader framework topics, especially as it relates to javascript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants