Skip to content

Commit

Permalink
Refactor away the dual entity setup
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Nov 2, 2022
1 parent a6b22b2 commit 949beaa
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 120 deletions.
4 changes: 2 additions & 2 deletions packages/block-library/src/navigation/constants.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const DEFAULT_ENTITY_KIND = 'root';
export const DEFAULT_ENTITY_TYPE = 'navigation';
export const DEFAULT_ENTITY_KIND = 'postType';
export const DEFAULT_ENTITY_TYPE = 'wp_navigation';
18 changes: 4 additions & 14 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import { close, Icon } from '@wordpress/icons';
*/
import useNavigationMenu from '../use-navigation-menu';
import useNavigationEntities from '../use-navigation-entities';
import useNavigationEntityTypes from '../use-navigation-entity-types';
import Placeholder from './placeholder';
import ResponsiveWrapper from './responsive-wrapper';
import NavigationInnerBlocks from './inner-blocks';
Expand All @@ -62,6 +61,7 @@ import useCreateNavigationMenu from './use-create-navigation-menu';
import { useInnerBlocks } from './use-inner-blocks';
import useGeneratedSlug from './use-generated-slug';
import { detectColors } from './utils';
import { DEFAULT_ENTITY_KIND, DEFAULT_ENTITY_TYPE } from '../constants';

function Navigation( {
attributes,
Expand Down Expand Up @@ -233,9 +233,6 @@ function Navigation( {
}
}, [ navigationMenu ] );

const [ navigationEntityKind, navigationEntityType ] =
useNavigationEntityTypes( ref );

const navMenuResolvedButMissing =
hasResolvedNavigationMenus && isNavigationMenuMissing;

Expand Down Expand Up @@ -870,8 +867,8 @@ function Navigation( {

return (
<EntityProvider
kind={ navigationEntityKind }
type={ navigationEntityType }
kind={ DEFAULT_ENTITY_KIND }
type={ DEFAULT_ENTITY_TYPE }
id={ idRef }
>
<RecursionProvider uniqueId={ recursionId }>
Expand Down Expand Up @@ -925,16 +922,11 @@ function Navigation( {
<InspectorControls __experimentalGroup="advanced">
{ hasResolvedCanUserUpdateNavigationMenu &&
canUserUpdateNavigationMenu && (
<NavigationMenuNameControl
kind={ navigationEntityKind }
type={ navigationEntityType }
/>
<NavigationMenuNameControl />
) }
{ hasResolvedCanUserDeleteNavigationMenu &&
canUserDeleteNavigationMenu && (
<NavigationMenuDeleteControl
kind={ navigationEntityKind }
type={ navigationEntityType }
onDelete={ ( deletedMenuTitle = '' ) => {
replaceInnerBlocks( clientId, [] );
showNavigationMenuStatusNotice(
Expand Down Expand Up @@ -975,8 +967,6 @@ function Navigation( {
{ isEntityAvailable && (
<NavigationInnerBlocks
clientId={ clientId }
kind={ navigationEntityKind }
type={ navigationEntityType }
hasCustomPlaceholder={
!! CustomPlaceholder
}
Expand Down
8 changes: 5 additions & 3 deletions packages/block-library/src/navigation/edit/inner-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useMemo } from '@wordpress/element';
* Internal dependencies
*/
import PlaceholderPreview from './placeholder/placeholder-preview';
import { DEFAULT_ENTITY_KIND, DEFAULT_ENTITY_TYPE } from '../constants';

const ALLOWED_BLOCKS = [
'core/navigation-link',
Expand All @@ -40,8 +41,6 @@ export default function NavigationInnerBlocks( {
clientId,
hasCustomPlaceholder,
orientation,
kind,
type,
templateLock,
} ) {
const {
Expand Down Expand Up @@ -72,7 +71,10 @@ export default function NavigationInnerBlocks( {
[ clientId ]
);

const [ blocks, onInput, onChange ] = useEntityBlockEditor( kind, type );
const [ blocks, onInput, onChange ] = useEntityBlockEditor(
DEFAULT_ENTITY_KIND,
DEFAULT_ENTITY_TYPE
);

const shouldDirectInsert = useMemo(
() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ import { useDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';

export default function NavigationMenuDeleteControl( {
onDelete,
kind,
type,
} ) {
/**
* Internal dependencies
*/
import { DEFAULT_ENTITY_KIND, DEFAULT_ENTITY_TYPE } from '../constants';

export default function NavigationMenuDeleteControl( { onDelete } ) {
const [ isConfirmModalVisible, setIsConfirmModalVisible ] =
useState( false );
const id = useEntityId( kind, type );
const [ title ] = useEntityProp( kind, type, 'title' );
const id = useEntityId( DEFAULT_ENTITY_KIND, DEFAULT_ENTITY_TYPE );
const [ title ] = useEntityProp(
DEFAULT_ENTITY_KIND,
DEFAULT_ENTITY_TYPE,
'title'
);
const { deleteEntityRecord } = useDispatch( coreStore );

return (
Expand Down Expand Up @@ -64,9 +69,14 @@ export default function NavigationMenuDeleteControl( {
<Button
variant="primary"
onClick={ () => {
deleteEntityRecord( kind, type, id, {
force: true,
} );
deleteEntityRecord(
DEFAULT_ENTITY_KIND,
DEFAULT_ENTITY_TYPE,
id,
{
force: true,
}
);
onDelete( title );
} }
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ import { TextControl } from '@wordpress/components';
import { useEntityProp } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';

export default function NavigationMenuNameControl( { kind, type } ) {
const [ title, updateTitle ] = useEntityProp( kind, type, 'title' );
/**
* Internal dependencies
*/
import { DEFAULT_ENTITY_KIND, DEFAULT_ENTITY_TYPE } from '../constants';

export default function NavigationMenuNameControl() {
const [ title, updateTitle ] = useEntityProp(
DEFAULT_ENTITY_KIND,
DEFAULT_ENTITY_TYPE,
'title'
);

return (
<TextControl
Expand Down

This file was deleted.

57 changes: 39 additions & 18 deletions packages/block-library/src/navigation/use-navigation-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import useNavigationEntityTypes from './use-navigation-entity-types';
import { isNumeric } from './edit/utils';
import { DEFAULT_ENTITY_KIND, DEFAULT_ENTITY_TYPE } from './constants';

export default function useNavigationMenu( recordKey, navPostId ) {
// Permissions can only be fetched by Post ID. As the Nav block
Expand All @@ -20,8 +20,6 @@ export default function useNavigationMenu( recordKey, navPostId ) {
// the Navigation Menu Post has been issued. This is unavoidable.
const permissions = useResourcePermissions( 'navigation', navPostId );

const entityConfig = useNavigationEntityTypes( recordKey );

return useSelect(
( select ) => {
const {
Expand All @@ -36,13 +34,13 @@ export default function useNavigationMenu( recordKey, navPostId ) {
navigationMenus,
isResolvingNavigationMenus,
hasResolvedNavigationMenus,
} = selectNavigationMenus( select, recordKey, entityConfig );
} = selectNavigationMenus( select, recordKey );

const {
navigationMenu,
isNavigationMenuResolved,
isNavigationMenuMissing,
} = selectExistingMenu( select, recordKey, entityConfig );
} = selectExistingMenu( select, recordKey );

return {
navigationMenus,
Expand Down Expand Up @@ -76,11 +74,15 @@ export default function useNavigationMenu( recordKey, navPostId ) {
);
}

function selectNavigationMenus( select, _recordKey, entityConfig ) {
function selectNavigationMenus( select ) {
const { getEntityRecords, hasFinishedResolution, isResolving } =
select( coreStore );

const args = [ ...entityConfig, { per_page: -1, status: 'publish' } ];
const args = [
DEFAULT_ENTITY_KIND,
DEFAULT_ENTITY_TYPE,
{ per_page: -1, status: 'publish' },
];

return {
navigationMenus: getEntityRecords( ...args ),
Expand All @@ -92,7 +94,7 @@ function selectNavigationMenus( select, _recordKey, entityConfig ) {
};
}

function selectExistingMenu( select, recordKey, entityConfig ) {
function selectExistingMenu( select, recordKey ) {
if ( ! recordKey ) {
return {
isNavigationMenuResolved: false,
Expand All @@ -118,28 +120,49 @@ function selectExistingMenu( select, recordKey, entityConfig ) {
// This call to `getEntityRecords` **must** be distinct from the
// call within the `selectNavigationMenus` (above) otherwise the
// query will return only `published` menus.
const navigationMenus = getEntityRecords( ...entityConfig, {
...recordKeyQuery,
per_page: 1, // only the 1 record is required.
status: [ 'publish', 'draft' ], // required to distinguish from primary `getEntityRecords` call.
} );
const navigationMenus = getEntityRecords(
DEFAULT_ENTITY_KIND,
DEFAULT_ENTITY_TYPE,
{
...recordKeyQuery,
per_page: 1, // only the 1 record is required.
status: [ 'publish', 'draft' ], // required to distinguish from primary `getEntityRecords` call.
}
);

const hasResolvedNavigationMenuSlugQuery = hasFinishedResolution(
'getEntityRecords',
[
DEFAULT_ENTITY_KIND,
DEFAULT_ENTITY_TYPE,
{
...recordKeyQuery,
per_page: 1, // only the 1 record is required.
status: [ 'publish', 'draft' ], // required to distinguish from primary `getEntityRecords` call.
},
]
);

const hasNavigationMenu = navigationMenus?.length;

// `wp_navigation` entities are keyed by Post ID in state.
// Perform subsequent lookups based on the ID of the record
// returned by the slug-based query (if available).
const idQueryArgs = hasNavigationMenu
? [ ...entityConfig, navigationMenus[ 0 ]?.id ]
? [ DEFAULT_ENTITY_KIND, DEFAULT_ENTITY_TYPE, navigationMenus[ 0 ]?.id ]
: [];

const editedNavigationMenu = hasNavigationMenu
? getEditedEntityRecord( ...idQueryArgs )
: null;

// "Resolved" in this case means either
// - the resolution state of request for the edited entity record
// (in the case there is an 0th Nav Post).
// - the resolution state of the query to look up with 0th post.
const hasResolvedNavigationMenu = hasNavigationMenu
? hasFinishedResolution( 'getEditedEntityRecord', idQueryArgs )
: null;
: hasResolvedNavigationMenuSlugQuery;

// Only published Navigation posts are considered valid.
// Draft Navigation posts are valid only on the editor,
Expand All @@ -149,7 +172,7 @@ function selectExistingMenu( select, recordKey, entityConfig ) {
editedNavigationMenu?.status === 'publish' ||
editedNavigationMenu?.status === 'draft';

const rtn = {
return {
isNavigationMenuResolved: hasResolvedNavigationMenu,
isNavigationMenuMissing:
hasResolvedNavigationMenu &&
Expand All @@ -161,6 +184,4 @@ function selectExistingMenu( select, recordKey, entityConfig ) {
? editedNavigationMenu
: null,
};

return rtn;
}
26 changes: 2 additions & 24 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,28 +167,6 @@ export const rootEntitiesConfig = [
baseURLParams: { context: 'edit' },
key: 'plugin',
},
{
// Navigation entities keyed by `slug`.
// Note that there is also a separate Navigation `postType` entitiy which
// stores `wp_navigation` posts keyed by postId for reasons relating
// to backwards compatibility.
label: __( 'Navigation Menus' ),
name: 'navigation',
kind: 'root',
baseURL: '/wp/v2/navigation',
baseURLParams: { context: 'edit' },
key: 'slug',
transientEdits: {
blocks: true,
selection: true,
},
mergedEdits: { meta: true },
rawAttributes: POST_RAW_ATTRIBUTES,
getTitle: ( record ) =>
record?.title?.rendered || record?.title || String( record.id ),
__unstablePrePersist: prePersistPostType,
__unstable_rest_base: 'navigation',
},
];

export const additionalEntityConfigLoaders = [
Expand All @@ -203,7 +181,7 @@ export const additionalEntityConfigLoaders = [
* @param {Object} edits Edits.
* @return {Object} Updated edits.
*/
export function prePersistPostType( persistedRecord, edits ) {
export const prePersistPostType = ( persistedRecord, edits ) => {
const newEdits = {};

if ( persistedRecord?.status === 'auto-draft' ) {
Expand All @@ -224,7 +202,7 @@ export function prePersistPostType( persistedRecord, edits ) {
}

return newEdits;
}
};

/**
* Returns the list of post type entities.
Expand Down

0 comments on commit 949beaa

Please sign in to comment.