Skip to content

Commit

Permalink
Preferences: Use hooks instead of HoC in 'EnablePanelOption' (#66994)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: youknowriad <[email protected]>
  • Loading branch information
3 people authored Nov 14, 2024
1 parent a8a8747 commit 94b8ac4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
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 }
/>
);
}

1 comment on commit 94b8ac4

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 94b8ac4.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11834232032
📝 Reported issues:

Please sign in to comment.