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

Navigation Editor: "Add new pages" use menu entity #31643

Merged
merged 1 commit into from
May 10, 2021
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
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 }
/>
);
}