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

Preferences: Use hooks instead of HoC in 'EnablePanelOption' #66994

Merged
merged 1 commit into from
Nov 14, 2024
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
44 changes: 27 additions & 17 deletions packages/edit-post/src/components/preferences-modal/enable-panel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* WordPress dependencies
*/
import { compose, ifCondition } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { privateApis as preferencesPrivateApis } from '@wordpress/preferences';

Expand All @@ -13,18 +12,29 @@ import { unlock } from '../../lock-unlock';

const { PreferenceBaseOption } = unlock( preferencesPrivateApis );

export default compose(
withSelect( ( select, { panelName } ) => {
const { isEditorPanelEnabled, isEditorPanelRemoved } =
select( editorStore );
return {
isRemoved: isEditorPanelRemoved( panelName ),
isChecked: isEditorPanelEnabled( panelName ),
};
} ),
ifCondition( ( { isRemoved } ) => ! isRemoved ),
withDispatch( ( dispatch, { panelName } ) => ( {
onChange: () =>
dispatch( editorStore ).toggleEditorPanelEnabled( panelName ),
} ) )
)( PreferenceBaseOption );
export default function EnablePanelOption( props ) {
const { toggleEditorPanelEnabled } = useDispatch( editorStore );
const { isChecked, isRemoved } = useSelect(
( select ) => {
const { isEditorPanelEnabled, isEditorPanelRemoved } =
select( editorStore );
return {
isChecked: isEditorPanelEnabled( props.panelName ),
isRemoved: isEditorPanelRemoved( props.panelName ),
};
},
[ props.panelName ]
);

if ( isRemoved ) {
return null;
}

return (
<PreferenceBaseOption
isChecked={ isChecked }
onChange={ () => toggleEditorPanelEnabled( props.panelName ) }
{ ...props }
/>
);
}
44 changes: 27 additions & 17 deletions packages/editor/src/components/preferences-modal/enable-panel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* WordPress dependencies
*/
import { compose, ifCondition } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { privateApis as preferencesPrivateApis } from '@wordpress/preferences';

/**
Expand All @@ -13,18 +12,29 @@ import { store as editorStore } from '../../store';

const { PreferenceBaseOption } = unlock( preferencesPrivateApis );

export default compose(
withSelect( ( select, { panelName } ) => {
const { isEditorPanelEnabled, isEditorPanelRemoved } =
select( editorStore );
return {
isRemoved: isEditorPanelRemoved( panelName ),
isChecked: isEditorPanelEnabled( panelName ),
};
} ),
ifCondition( ( { isRemoved } ) => ! isRemoved ),
withDispatch( ( dispatch, { panelName } ) => ( {
onChange: () =>
dispatch( editorStore ).toggleEditorPanelEnabled( panelName ),
} ) )
)( PreferenceBaseOption );
export default function EnablePanelOption( props ) {
const { toggleEditorPanelEnabled } = useDispatch( editorStore );
const { isChecked, isRemoved } = useSelect(
( select ) => {
const { isEditorPanelEnabled, isEditorPanelRemoved } =
select( editorStore );
return {
isChecked: isEditorPanelEnabled( props.panelName ),
isRemoved: isEditorPanelRemoved( props.panelName ),
};
},
[ props.panelName ]
);

if ( isRemoved ) {
return null;
}

return (
<PreferenceBaseOption
isChecked={ isChecked }
onChange={ () => toggleEditorPanelEnabled( props.panelName ) }
{ ...props }
/>
);
}
Loading