Skip to content

Commit

Permalink
Preferences: Use hooks instead of HoC in 'EnablePanelOption'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Nov 14, 2024
1 parent a8a8747 commit 8f4c4cf
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 }
/>
);
}

0 comments on commit 8f4c4cf

Please sign in to comment.