Skip to content

Commit

Permalink
[Navigation editor] Save menu items using the REST API (#34541)
Browse files Browse the repository at this point in the history
* Remove tests

* Validate if menu_order is set and >= 1

* First stab at saving menu items using the REST API

* Migrate batch save to async thunks using REST API calls

* simplify the logic

* Simplify computeDeletedMenuItemIds

* Use more generic variable names

* Use more consistent variable names

* Update the data flow to ensure API receives appropriate parameters names

* Alter the order of arguments in blockToMenuItem

* Move transformations to transform.js

* Remove dev comment

* Operate on lists and sets, not on trees

* Extract batchSaveMenuItems

* Inline blockIdToOldEntityRecord

* Ensure consistency between stored menu items and the ones recovered from the block attributes

* Add error checking

* Add generic batchSaveDiff function

* Preprocess menu data before sending it over to the API

* Adjust unit tests

* Remove dev artifacts

* Replace map/filter with a for loop for readability

* Rename "new" to Desired" to avoid abiguity

* Rename tasks and diffs to changesets

* Remove prePersistMenuItem

* Don't await releaseStoreLock

* Inline resolveSelectMenuItems

* Resolve a list of all menu items prior to creating placeholder menu items.

* Remove async code from the synchronous getDesiredMenuItems selector

* Don't await createErrorNotice and createSuccessNotice

* Rename computeChangeset to prepareChangeset and remove awaits from it and getFailedChanges

* Use store object to refer to core-data store, not a string identifier

* Set placeholder menu order to 1

* Don't create placeholder menu items for non-link blocks

* Remove menu order validation

* Remove menu id validation

* Restore rest_invalid_param validation error code

* Fix unit tests

* Add comments blocks

* Remove "@todo: extract locks to a separate package?"

* Remove unused block argument from createPlaceholderMenuItem

* Remove obsolete "block" argument

* Replace diff() with _.difference()

* Replace clientId mapping with internal attribute denoting entity record ID

* Radically simplify batch saving

* Fix unit tests

* Translate Menu item title

* Remove obsolete imports

* Switch to using immutable data structures

* Add doc blocks

* Add a test for default 'menu-item-type'

* Restore newline

* Lint

* Use isBlockSupportedInNav in blockToMenuItem

* Lint

* Code style: Collapse two filters into one

* Inline applyEdits

Co-authored-by: Jonny Harris <jon@spacedmonkey.co.uk>
  • Loading branch information
adamziel and spacedmonkey authored Sep 21, 2021
1 parent 49da23c commit a338b95
Show file tree
Hide file tree
Showing 24 changed files with 1,392 additions and 3,118 deletions.
1 change: 1 addition & 0 deletions lib/class-wp-rest-menu-items-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ protected function prepare_item_for_database( $request ) {
'menu-item-object' => $menu_item_obj->object,
'menu-item-parent-id' => $menu_item_obj->menu_item_parent,
'menu-item-position' => $position,
'menu-item-type' => $menu_item_obj->type,
'menu-item-title' => $menu_item_obj->title,
'menu-item-url' => $menu_item_obj->url,
'menu-item-description' => $menu_item_obj->description,
Expand Down
1 change: 1 addition & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const defaultEntities = [
baseURLParams: { context: 'edit' },
plural: 'menuItems',
label: __( 'Menu Item' ),
rawAttributes: [ 'title', 'content' ],
},
{
name: 'menuLocation',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { chevronDown } from '@wordpress/icons';
*/
import { useMenuEntityProp, useSelectedMenuId } from '../../hooks';
import useNavigationEntities from './use-navigation-entities';
import menuItemsToBlocks from './menu-items-to-blocks';
import { menuItemsToBlocks } from '../../store/transform';

/**
* Convert pages to blocks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createBlock } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { menuItemToBlockAttributes } from '../../store/utils';
import { menuItemToBlockAttributes } from '../../store/transform';

/**
* Convert a flat menu item structure to a nested blocks structure.
Expand Down
10 changes: 7 additions & 3 deletions packages/edit-navigation/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
BlockTools,
__unstableUseBlockSelectionClearer as useBlockSelectionClearer,
} from '@wordpress/block-editor';
import { useEntityBlockEditor } from '@wordpress/core-data';
import { Popover, SlotFillProvider, Spinner } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { useEffect, useMemo, useState } from '@wordpress/element';
Expand All @@ -25,7 +26,6 @@ import UnselectedMenuState from './unselected-menu-state';
import {
IsMenuNameControlFocusedContext,
useNavigationEditor,
useNavigationBlockEditor,
useMenuNotifications,
} from '../../hooks';
import ErrorBoundary from '../error-boundary';
Expand Down Expand Up @@ -68,8 +68,12 @@ export default function Layout( { blockEditorSettings } ) {
isMenuSelected,
} = useNavigationEditor();

const [ blocks, onInput, onChange ] = useNavigationBlockEditor(
navigationPost
const [ blocks, onInput, onChange ] = useEntityBlockEditor(
'root',
'postType',
{
id: navigationPost?.id,
}
);

const { hasSidebarEnabled, isInserterOpened } = useSelect(
Expand Down
1 change: 0 additions & 1 deletion packages/edit-navigation/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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';
export { default as useSelectedMenuId } from './use-selected-menu-id';
export { default as useMenuLocations } from './use-menu-locations';
Expand Down
34 changes: 0 additions & 34 deletions packages/edit-navigation/src/hooks/use-navigation-block-editor.js

This file was deleted.

Loading

0 comments on commit a338b95

Please sign in to comment.