Skip to content

Commit

Permalink
Navigation Editor: "Add new pages" use menu entity (#31643)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored May 10, 2021
1 parent 90e50dc commit e8081ad
Showing 1 changed file with 10 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useState, useEffect } from '@wordpress/element';
import { ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

export default function AutoAddPages( { menuId } ) {
const menu = useSelect( ( select ) => select( 'core' ).getMenu( menuId ), [
menuId,
] );

const [ autoAddPages, setAutoAddPages ] = useState( null );

useEffect( () => {
if ( autoAddPages === null && menu ) {
setAutoAddPages( menu.auto_add );
}
}, [ autoAddPages, menu ] );
/**
* Internal dependencies
*/
import { useMenuEntityProp } from '../../hooks';

const { saveMenu } = useDispatch( 'core' );
export default function AutoAddPages( { menuId } ) {
const [ autoAddPages, setAutoAddPages ] = useMenuEntityProp(
'auto_add',
menuId
);

return (
<ToggleControl
Expand All @@ -28,13 +22,7 @@ export default function AutoAddPages( { menuId } ) {
'Automatically add published top-level pages to this menu.'
) }
checked={ autoAddPages ?? false }
onChange={ ( newAutoAddPages ) => {
setAutoAddPages( newAutoAddPages );
saveMenu( {
...menu,
auto_add: newAutoAddPages,
} );
} }
onChange={ setAutoAddPages }
/>
);
}

0 comments on commit e8081ad

Please sign in to comment.