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: Introduce useMenuEntityProp hook #31132

Merged
merged 3 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 2 additions & 3 deletions packages/edit-navigation/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { __, sprintf } from '@wordpress/i18n';
*/
import SaveButton from './save-button';
import MenuSwitcher from '../menu-switcher';
import { useMenuEntity } from '../../hooks';
import { useMenuEntityProp } from '../../hooks';

export default function Header( {
isMenuSelected,
Expand All @@ -20,8 +20,7 @@ export default function Header( {
isPending,
navigationPost,
} ) {
const { editedMenu: selectedMenu } = useMenuEntity( selectedMenuId );
const menuName = selectedMenu ? selectedMenu.name : undefined;
const [ menuName ] = useMenuEntityProp( 'name', selectedMenuId );
let actionHeaderText;

if ( menuName ) {
Expand Down
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 }
/>
);
}
6 changes: 3 additions & 3 deletions packages/edit-navigation/src/components/name-display/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import { BlockControls } from '@wordpress/block-editor';
*/
import {
untitledMenu,
useMenuEntity,
useSelectedMenuId,
useMenuEntityProp,
IsMenuNameControlFocusedContext,
} from '../../hooks';

import { sprintf, __ } from '@wordpress/i18n';
export default function NameDisplay() {
const [ menuId ] = useSelectedMenuId();
const { editedMenu } = useMenuEntity( menuId );
const [ name ] = useMenuEntityProp( 'name', menuId );
const [ , setIsMenuNameEditFocused ] = useContext(
IsMenuNameControlFocusedContext
);

const menuName = editedMenu?.name ?? untitledMenu;
const menuName = name ?? untitledMenu;

return (
<BlockControls>
Expand Down
19 changes: 7 additions & 12 deletions packages/edit-navigation/src/components/name-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { TextControl } from '@wordpress/components';
import { useEffect, useRef, useContext } from '@wordpress/element';

/**
* Internal dependencies
*/
import { TextControl } from '@wordpress/components';
import {
IsMenuNameControlFocusedContext,
untitledMenu,
useMenuEntity,
useMenuEntityProp,
useSelectedMenuId,
} from '../../hooks';

Expand All @@ -20,18 +20,13 @@ export function NameEditor() {
);

const [ menuId ] = useSelectedMenuId();
const { editedMenu, editMenuEntityRecord, menuEntityData } = useMenuEntity(
menuId
);
const editedMenuName = menuId && editedMenu.name;

const editMenuName = ( name = untitledMenu ) =>
editMenuEntityRecord( ...menuEntityData, { name } );
const [ name, setName ] = useMenuEntityProp( 'name', menuId );

const inputRef = useRef();
useEffect( () => {
if ( isMenuNameEditFocused ) inputRef.current.focus();
}, [ isMenuNameEditFocused ] );

return (
<>
<TextControl
Expand All @@ -42,8 +37,8 @@ export function NameEditor() {
label={ __( 'Name' ) }
onBlur={ () => setIsMenuNameEditFocused( false ) }
className="edit-navigation-name-editor__text-control"
value={ editedMenuName }
onChange={ editMenuName }
value={ name }
onChange={ setName }
/>
</>
);
Expand Down
1 change: 1 addition & 0 deletions packages/edit-navigation/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const untitledMenu = __( '(untitled menu)' );
export const IsMenuNameControlFocusedContext = createContext();

export { default as useMenuEntity } from './use-menu-entity';
export { default as useMenuEntityProp } from './use-menu-entity-prop';
export { default as useNavigationEditor } from './use-navigation-editor';
export { default as useNavigationBlockEditor } from './use-navigation-block-editor';
export { default as useMenuNotifications } from './use-menu-notifications';
Expand Down
24 changes: 24 additions & 0 deletions packages/edit-navigation/src/hooks/use-menu-entity-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import { MENU_KIND, MENU_POST_TYPE } from '../constants';

/**
* Returns the value and setter for the specified
* property of the menu.
*
* @param {string} prop A Property name.
* @param {string} menuId A menu ID.
*
* @return {[*, Function]} A tuple where the first item is the
* property value and the second is the
* setter.
*/
export default function useMenuEntityProp( prop, menuId ) {
return useEntityProp( MENU_KIND, MENU_POST_TYPE, prop, menuId );
}