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 screen: Fix failing request for menu items #28764

Merged
merged 4 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/edit-navigation/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function Header( {
showTooltip: false,
children: __( 'Select menu' ),
isTertiary: true,
disabled: ! menus,
disabled: ! menus?.length,
__experimentalIsFocusable: true,
} }
popoverProps={ {
Expand Down
7 changes: 4 additions & 3 deletions packages/edit-navigation/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default function Layout( { blockEditorSettings } ) {
navigationPost,
selectMenu,
deleteMenu,
hasLoadedMenus,
} = useNavigationEditor();

const [ blocks, onInput, onChange ] = useNavigationBlockEditor(
Expand All @@ -56,7 +57,7 @@ export default function Layout( { blockEditorSettings } ) {

<div className="edit-navigation-layout">
<Header
isPending={ ! navigationPost }
isPending={ ! hasLoadedMenus }
menus={ menus }
selectedMenuId={ selectedMenuId }
onSelectMenu={ selectMenu }
Expand All @@ -78,11 +79,11 @@ export default function Layout( { blockEditorSettings } ) {
saveBlocks={ savePost }
/>
<Toolbar
isPending={ ! navigationPost }
isPending={ ! hasLoadedMenus }
navigationPost={ navigationPost }
/>
<Editor
isPending={ ! navigationPost }
isPending={ ! hasLoadedMenus }
blocks={ blocks }
/>
<InspectorAdditions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ import { useState, useEffect } from '@wordpress/element';
import { store as editNavigationStore } from '../../store';

export default function useNavigationEditor() {
const menus = useSelect(
( select ) => select( 'core' ).getMenus( { per_page: -1 } ),
[]
);
const { menus, hasLoadedMenus } = useSelect( ( select ) => {
const selectors = select( 'core' );
const params = { per_page: -1 };
return {
menus: selectors.getMenus( params ),
hasLoadedMenus: selectors.hasFinishedResolution( 'getMenus', [
params,
] ),
};
}, [] );

const [ selectedMenuId, setSelectedMenuId ] = useState( null );

Expand Down Expand Up @@ -52,5 +58,6 @@ export default function useNavigationEditor() {
navigationPost,
selectMenu,
deleteMenu,
hasLoadedMenus,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function SaveButton( { navigationPost } ) {
onClick={ () => {
saveNavigationPost( navigationPost );
} }
disabled={ ! navigationPost }
>
{ __( 'Save' ) }
</Button>
Expand Down
4 changes: 4 additions & 0 deletions packages/edit-navigation/src/store/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import { KIND, POST_TYPE, buildNavigationPostId } from './utils';
* @return {void}
*/
export function* getNavigationPostForMenu( menuId ) {
if ( ! menuId ) {
return;
}

const stubPost = createStubPost( menuId );
// Persist an empty post to warm up the state
yield persistPost( stubPost );
Expand Down
6 changes: 6 additions & 0 deletions packages/edit-navigation/src/store/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ jest.mock( '@wordpress/blocks', () => {
} );

describe( 'getNavigationPostForMenu', () => {
it( 'returns early when a menuId is not provided', () => {
const generator = getNavigationPostForMenu( null );
expect( generator.next().value ).toBeUndefined();
expect( generator.next().done ).toBe( true );
} );

it( 'gets navigation post for menu id', () => {
const menuId = 123;

Expand Down