-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Refactor Nav creation hooks and associated code #38824
Changes from 4 commits
62234f5
b8bc7da
a046fd5
a9e8980
fa71d7c
8fec011
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,8 @@ import UnsavedInnerBlocks from './unsaved-inner-blocks'; | |
import NavigationMenuDeleteControl from './navigation-menu-delete-control'; | ||
import useNavigationNotice from './use-navigation-notice'; | ||
import OverlayMenuIcon from './overlay-menu-icon'; | ||
import useConvertClassicMenu from '../use-convert-classic-menu'; | ||
import useCreateNavigationMenu from './use-create-navigation-menu'; | ||
|
||
const EMPTY_ARRAY = []; | ||
|
||
|
@@ -231,6 +233,16 @@ function Navigation( { | |
hasResolvedCanUserCreateNavigation, | ||
} = useNavigationMenu( ref ); | ||
|
||
const createNavigationMenu = useCreateNavigationMenu( clientId ); | ||
|
||
const { | ||
dispatch: convertClassicMenuToBlocks, | ||
blocks: classicMenuBlocks, | ||
name: classicMenuName, | ||
isResolving: isResolvingClassicMenuConversion, | ||
hasResolved: hasResolvedClassicMenuConversion, | ||
} = useConvertClassicMenu( () => {} ); | ||
|
||
const navRef = useRef(); | ||
const isDraftNavigationMenu = navigationMenu?.status === 'draft'; | ||
|
||
|
@@ -385,6 +397,27 @@ function Navigation( { | |
ref, | ||
] ); | ||
|
||
useEffect( () => { | ||
async function handleCreateNav() { | ||
const navigationMenuPost = await createNavigationMenu( | ||
classicMenuName, | ||
classicMenuBlocks | ||
); | ||
setRef( navigationMenuPost?.id ); | ||
} | ||
if ( | ||
hasResolvedClassicMenuConversion && | ||
classicMenuName && | ||
classicMenuBlocks?.length | ||
) { | ||
handleCreateNav(); | ||
} | ||
}, [ | ||
classicMenuBlocks, | ||
classicMenuName, | ||
hasResolvedClassicMenuConversion, | ||
] ); | ||
|
||
const startWithEmptyMenu = useCallback( () => { | ||
registry.batch( () => { | ||
if ( navigationArea ) { | ||
|
@@ -495,6 +528,13 @@ function Navigation( { | |
setRef( id ); | ||
onClose(); | ||
} } | ||
onSelectClassicMenu={ ( menu ) => { | ||
onClose(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to be a little careful about focus management here. |
||
convertClassicMenuToBlocks( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By passing the conversion code down into the dropdown we avoid errors thrown when the dropdown unmounts and an async request triggered by that component resolves. We do this by handling in the parent (which is not unmounted when the option is clicked). |
||
menu?.id, | ||
menu?.name | ||
); | ||
} } | ||
onCreateNew={ startWithEmptyMenu } | ||
/* translators: %s: The name of a menu. */ | ||
actionLabel={ __( "Switch to '%s'" ) } | ||
|
@@ -676,6 +716,7 @@ function Navigation( { | |
/> | ||
) } | ||
{ ! hasResolvedCanUserCreateNavigation || | ||
isResolvingClassicMenuConversion || | ||
( ! isEntityAvailable && ! isPlaceholderShown && ( | ||
<PlaceholderPreview isLoading /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to look again at loading states because I don't yet see this. Perhaps because of the call to |
||
) ) } | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ | |||||
import { Placeholder, Button, DropdownMenu } from '@wordpress/components'; | ||||||
import { __ } from '@wordpress/i18n'; | ||||||
import { navigation, Icon } from '@wordpress/icons'; | ||||||
import { useEffect } from '@wordpress/element'; | ||||||
|
||||||
/** | ||||||
* Internal dependencies | ||||||
|
@@ -14,6 +15,7 @@ import PlaceholderPreview from './placeholder-preview'; | |||||
import useNavigationMenu from '../../use-navigation-menu'; | ||||||
import useCreateNavigationMenu from '../use-create-navigation-menu'; | ||||||
import NavigationMenuSelector from '../navigation-menu-selector'; | ||||||
import useConvertClassicMenu from '../../use-convert-classic-menu'; | ||||||
|
||||||
export default function NavigationPlaceholder( { | ||||||
clientId, | ||||||
|
@@ -45,7 +47,39 @@ export default function NavigationPlaceholder( { | |||||
hasMenus, | ||||||
} = useNavigationEntities(); | ||||||
|
||||||
const isStillLoading = isResolvingPages || isResolvingMenus; | ||||||
const { | ||||||
dispatch: convertClassicMenuToBlocks, | ||||||
blocks: classicMenuBlocks, | ||||||
name: classicMenuName, | ||||||
isResolving: isResolvingClassicMenuConversion, | ||||||
hasResolved: hasResolvedClassicMenuConversion, | ||||||
} = useConvertClassicMenu( () => {} ); | ||||||
|
||||||
useEffect( () => { | ||||||
async function handleCreateNav() { | ||||||
const navigationMenuPost = await createNavigationMenu( | ||||||
classicMenuName, | ||||||
classicMenuBlocks | ||||||
); | ||||||
onFinish( navigationMenuPost, classicMenuBlocks ); | ||||||
} | ||||||
if ( | ||||||
hasResolvedClassicMenuConversion && | ||||||
classicMenuName && | ||||||
classicMenuBlocks?.length | ||||||
) { | ||||||
handleCreateNav(); | ||||||
} | ||||||
}, [ | ||||||
classicMenuBlocks, | ||||||
classicMenuName, | ||||||
hasResolvedClassicMenuConversion, | ||||||
] ); | ||||||
|
||||||
const isStillLoading = | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this was already in
Suggested change
|
||||||
isResolvingPages || | ||||||
isResolvingMenus || | ||||||
isResolvingClassicMenuConversion; | ||||||
|
||||||
const onCreateEmptyMenu = () => { | ||||||
onFinishMenuCreation( [] ); | ||||||
|
@@ -89,10 +123,19 @@ export default function NavigationPlaceholder( { | |||||
} } | ||||||
popoverProps={ { isAlternate: true } } | ||||||
> | ||||||
{ () => ( | ||||||
{ ( { onClose } ) => ( | ||||||
<NavigationMenuSelector | ||||||
clientId={ clientId } | ||||||
onSelect={ onFinish } | ||||||
onSelectClassicMenu={ ( | ||||||
menu | ||||||
) => { | ||||||
onClose(); | ||||||
convertClassicMenuToBlocks( | ||||||
menu?.id, | ||||||
menu?.name | ||||||
); | ||||||
} } | ||||||
/> | ||||||
) } | ||||||
</DropdownMenu> | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,15 +16,19 @@ export default function useConvertClassicMenu( onFinish ) { | |
setIsAwaitingMenuItemResolution, | ||
] = useState( false ); | ||
const [ menuName, setMenuName ] = useState( '' ); | ||
const [ blocks, setBlocks ] = useState( [] ); | ||
|
||
const { menuItems, hasResolvedMenuItems } = useNavigationEntities( | ||
selectedMenu | ||
); | ||
const { | ||
menuItems, | ||
hasResolvedMenuItems, | ||
isResolvingMenus, | ||
} = useNavigationEntities( selectedMenu ); | ||
|
||
const createFromMenu = useCallback( | ||
( name ) => { | ||
const { innerBlocks: blocks } = menuItemsToBlocks( menuItems ); | ||
onFinish( blocks, name ); | ||
const { innerBlocks: _blocks } = menuItemsToBlocks( menuItems ); | ||
setBlocks( _blocks ); | ||
onFinish( _blocks, name ); | ||
}, | ||
[ menuItems, menuItemsToBlocks, onFinish ] | ||
); | ||
|
@@ -38,7 +42,7 @@ export default function useConvertClassicMenu( onFinish ) { | |
} | ||
}, [ isAwaitingMenuItemResolution, hasResolvedMenuItems, menuName ] ); | ||
|
||
return useCallback( | ||
const dispatch = useCallback( | ||
( id, name ) => { | ||
setSelectedMenu( id ); | ||
|
||
|
@@ -55,4 +59,12 @@ export default function useConvertClassicMenu( onFinish ) { | |
}, | ||
[ hasResolvedMenuItems, createFromMenu ] | ||
); | ||
|
||
return { | ||
dispatch, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't love dispatch here. We could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
blocks, | ||
name: menuName, | ||
isResolving: isResolvingMenus, | ||
hasResolved: hasResolvedMenuItems, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I have an empty classic menu, nothing happens when clicking the menu item. (it's the first thing I noticed when testing as all my classic test menus are empty 😄 )