diff --git a/packages/edit-navigation/src/components/header/index.js b/packages/edit-navigation/src/components/header/index.js index ae94955166ff1a..41177d5bded10e 100644 --- a/packages/edit-navigation/src/components/header/index.js +++ b/packages/edit-navigation/src/components/header/index.js @@ -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, @@ -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 ) { diff --git a/packages/edit-navigation/src/components/name-display/index.js b/packages/edit-navigation/src/components/name-display/index.js index d6973bdad761c6..18e6d41eb37fea 100644 --- a/packages/edit-navigation/src/components/name-display/index.js +++ b/packages/edit-navigation/src/components/name-display/index.js @@ -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 ( diff --git a/packages/edit-navigation/src/components/name-editor/index.js b/packages/edit-navigation/src/components/name-editor/index.js index e5c0c4ec3084bf..0bf2856fe0dcb5 100644 --- a/packages/edit-navigation/src/components/name-editor/index.js +++ b/packages/edit-navigation/src/components/name-editor/index.js @@ -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'; @@ -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 ( <> setIsMenuNameEditFocused( false ) } className="edit-navigation-name-editor__text-control" - value={ editedMenuName } - onChange={ editMenuName } + value={ name } + onChange={ setName } /> ); diff --git a/packages/edit-navigation/src/hooks/index.js b/packages/edit-navigation/src/hooks/index.js index b0ad3b18b1f5fc..36ac7098254c52 100644 --- a/packages/edit-navigation/src/hooks/index.js +++ b/packages/edit-navigation/src/hooks/index.js @@ -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'; diff --git a/packages/edit-navigation/src/hooks/use-menu-entity-prop.js b/packages/edit-navigation/src/hooks/use-menu-entity-prop.js new file mode 100644 index 00000000000000..50bb2c13ae730a --- /dev/null +++ b/packages/edit-navigation/src/hooks/use-menu-entity-prop.js @@ -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 ); +}