Skip to content

Commit

Permalink
Apply numericID test instead of maintaining list of recordKey types
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Jul 7, 2023
1 parent 9d70104 commit 7fe8755
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import useInitEditedEntityFromURL from '../sync-state-with-url/use-init-edited-e
import usePatternDetails from './use-pattern-details';
import { store as editSiteStore } from '../../store';
import { unlock } from '../../lock-unlock';
import normalizePostIdForPostType from '../../utils/normalize-post-id-for-post-type';
import normalizeRecordKey from '../../utils/normalize-record-key';

export default function SidebarNavigationScreenPattern() {
const { categoryType } = getQueryArgs( window.location.href );
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );

const { params } = useNavigator();
const { postType } = params;
const postId = normalizePostIdForPostType( params?.postId, postType );
const postId = normalizeRecordKey( params?.postId );

useInitEditedEntityFromURL();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import {
SidebarNavigationScreenDetailsPanelLabel,
SidebarNavigationScreenDetailsPanelValue,
} from '../sidebar-navigation-screen-details-panel';
import normalizePostIdForPostType from '../../utils/normalize-post-id-for-post-type';
import normalizeRecordKey from '../../utils/normalize-record-key';

export default function usePatternDetails( postType, postId ) {
postId = normalizePostIdForPostType( postId, postType );
postId = normalizeRecordKey( postId );

const { getDescription, getTitle, record } = useEditedEntityRecord(
postType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
*/
import { store as editSiteStore } from '../../store';
import { unlock } from '../../lock-unlock';
import normalizePostIdForPostType from '../../utils/normalize-post-id-for-post-type';
import normalizeRecordKey from '../../utils/normalize-record-key';

const { useLocation } = unlock( routerPrivateApis );

Expand All @@ -20,7 +20,7 @@ export default function useInitEditedEntityFromURL() {

const { postType } = params;

const postId = normalizePostIdForPostType( params?.postId, postType );
const postId = normalizeRecordKey( params?.postId );

const { isRequestingSite, homepageId, url } = useSelect( ( select ) => {
const { getSite, getUnstableBase } = select( coreDataStore );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { decodeEntities } from '@wordpress/html-entities';
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import normalizePostIdForPostType from '../../utils/normalize-post-id-for-post-type';
import normalizeRecordKey from '../../utils/normalize-record-key';

export default function useEditedEntityRecord( postType, postId ) {
const { record, title, description, isLoaded, icon } = useSelect(
Expand All @@ -25,7 +25,7 @@ export default function useEditedEntityRecord( postType, postId ) {

let usedPostId = postId ?? getEditedPostId();

usedPostId = normalizePostIdForPostType( usedPostId, usedPostType );
usedPostId = normalizeRecordKey( usedPostId, usedPostType );

const _record = getEditedEntityRecord(
'postType',
Expand Down

This file was deleted.

11 changes: 11 additions & 0 deletions packages/edit-site/src/utils/normalize-record-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function isNumericID( str ) {
return /^\s*\d+\s*$/.test( str );
}

export default function normalizeRecordKey( postId ) {
if ( isNumericID( postId ) ) {
postId = Number( postId );
}

return postId;
}

0 comments on commit 7fe8755

Please sign in to comment.