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

Editor: refactor PostFormatPanel to use React hooks. #26273

Merged
merged 1 commit into from
Oct 19, 2020
Merged
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
Editor: refactor PostFormatPanel to use React hooks.
  • Loading branch information
ZebulanStanphill committed Oct 19, 2020
commit 3a85088fcdcfb2c20210821d660fd22701bc3c12
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ import { find, get, includes } from 'lodash';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { ifCondition, compose } from '@wordpress/compose';
import { withDispatch, withSelect } from '@wordpress/data';
import { Button, PanelBody } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { POST_FORMATS } from '../post-format';

const getSuggestion = ( supportedFormats, suggestedPostFormat ) => {
const formats = POST_FORMATS.filter( ( format ) =>
includes( supportedFormats, format.id )
);
return find( formats, ( format ) => format.id === suggestedPostFormat );
};

const PostFormatSuggestion = ( {
suggestedPostFormat,
suggestionText,
Expand All @@ -26,14 +32,40 @@ const PostFormatSuggestion = ( {
</Button>
);

const PostFormatPanel = ( { suggestion, onUpdatePostFormat } ) => {
export default function PostFormatPanel() {
const { currentPostFormat, suggestion } = useSelect( ( select ) => {
const { getEditedPostAttribute, getSuggestedPostFormat } = select(
'core/editor'
);
const supportedFormats = get(
select( 'core' ).getThemeSupports(),
[ 'formats' ],
[]
);
return {
currentPostFormat: getEditedPostAttribute( 'format' ),
suggestion: getSuggestion(
supportedFormats,
getSuggestedPostFormat()
),
};
}, [] );

const { editPost } = useDispatch( 'core/editor' );

const onUpdatePostFormat = ( format ) => editPost( { format } );

const panelBodyTitle = [
__( 'Suggestion:' ),
<span className="editor-post-publish-panel__link" key="label">
{ __( 'Use a post format' ) }
</span>,
];

if ( ! suggestion || suggestion.id === currentPostFormat ) {
return null;
}

return (
<PanelBody initialOpen={ false } title={ panelBodyTitle }>
<p>
Expand All @@ -54,40 +86,4 @@ const PostFormatPanel = ( { suggestion, onUpdatePostFormat } ) => {
</p>
</PanelBody>
);
};

const getSuggestion = ( supportedFormats, suggestedPostFormat ) => {
const formats = POST_FORMATS.filter( ( format ) =>
includes( supportedFormats, format.id )
);
return find( formats, ( format ) => format.id === suggestedPostFormat );
};

export default compose(
withSelect( ( select ) => {
const { getEditedPostAttribute, getSuggestedPostFormat } = select(
'core/editor'
);
const supportedFormats = get(
select( 'core' ).getThemeSupports(),
[ 'formats' ],
[]
);
return {
currentPostFormat: getEditedPostAttribute( 'format' ),
suggestion: getSuggestion(
supportedFormats,
getSuggestedPostFormat()
),
};
} ),
withDispatch( ( dispatch ) => ( {
onUpdatePostFormat( postFormat ) {
dispatch( 'core/editor' ).editPost( { format: postFormat } );
},
} ) ),
ifCondition(
( { suggestion, currentPostFormat } ) =>
suggestion && suggestion.id !== currentPostFormat
)
)( PostFormatPanel );
}