-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate concerns in useNavigationBlocks (#22825)
* First stab at encapsulation of useNavigationBlocks * Flicker issue solved again * Encapsulated parts of the logic from useNavigationBlocks * Formatting * Decouple fetching the data, transforming, saving, and creating draft posts * Move blocks argument to useSaveNavigationBlocks hook * Separate fetching menu items from creating navigation blocks * Move PromiseQueue to use-create-missing-menu-items as it is only used there * construct query inline instead of having useMenuItemsQuery * Simplify the refactor (thank you Andrei!) * Extract PromiseQueue into a separate file * Move eventuallySaveItems to useMenuItems * Update packages/edit-navigation/src/components/menu-editor/use-create-missing-menu-items.js Co-authored-by: andrei draganescu <[email protected]> * Update packages/edit-navigation/src/components/menu-editor/use-create-missing-menu-items.js Co-authored-by: andrei draganescu <[email protected]> * Show the notice after receiveEntityRecords * Fix spacing issue * Remove rogue async * rename linkBlocks to blocks Co-authored-by: andrei draganescu <[email protected]>
- Loading branch information
1 parent
77fb18e
commit ab878d5
Showing
6 changed files
with
174 additions
and
140 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
packages/edit-navigation/src/components/menu-editor/helpers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const flattenBlocks = ( blocks ) => | ||
blocks.flatMap( ( item ) => | ||
[ item ].concat( flattenBlocks( item.innerBlocks || [] ) ) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/edit-navigation/src/components/menu-editor/use-create-missing-menu-items.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { useRef, useCallback } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { flattenBlocks } from './helpers'; | ||
import PromiseQueue from './promise-queue'; | ||
|
||
/** | ||
* When a new Navigation child block is added, we create a draft menuItem for it because | ||
* the batch save endpoint expects all the menu items to have a valid id already. | ||
* PromiseQueue is used in order to | ||
* 1) limit the amount of requests processed at the same time | ||
* 2) save the menu only after all requests are finalized | ||
* | ||
* @return {function(*=): void} Function registering it's argument to be called once all menuItems are created. | ||
*/ | ||
export default function useCreateMissingMenuItems() { | ||
const promiseQueueRef = useRef( new PromiseQueue() ); | ||
const enqueuedBlocksIds = useRef( [] ); | ||
const createMissingMenuItems = ( blocks, menuItemsRef ) => { | ||
for ( const { clientId, name } of flattenBlocks( blocks ) ) { | ||
// No need to create menuItems for the wrapping navigation block | ||
if ( name === 'core/navigation' ) { | ||
continue; | ||
} | ||
// Menu item was already created | ||
if ( clientId in menuItemsRef.current ) { | ||
continue; | ||
} | ||
// Menu item already in the queue | ||
if ( enqueuedBlocksIds.current.includes( clientId ) ) { | ||
continue; | ||
} | ||
enqueuedBlocksIds.current.push( clientId ); | ||
promiseQueueRef.current.enqueue( () => | ||
createDraftMenuItem( clientId ).then( ( menuItem ) => { | ||
menuItemsRef.current[ clientId ] = menuItem; | ||
enqueuedBlocksIds.current.splice( | ||
enqueuedBlocksIds.current.indexOf( clientId ) | ||
); | ||
} ) | ||
); | ||
} | ||
}; | ||
const onCreated = useCallback( | ||
( callback ) => promiseQueueRef.current.then( callback ), | ||
[ promiseQueueRef.current ] | ||
); | ||
return { createMissingMenuItems, onCreated }; | ||
} | ||
|
||
function createDraftMenuItem() { | ||
return apiFetch( { | ||
path: `/__experimental/menu-items`, | ||
method: 'POST', | ||
data: { | ||
title: 'Placeholder', | ||
url: 'Placeholder', | ||
menu_order: 0, | ||
}, | ||
} ); | ||
} |
18 changes: 0 additions & 18 deletions
18
packages/edit-navigation/src/components/menu-editor/use-debounced-value.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.