diff --git a/packages/editor/src/dataviews/store/private-actions.ts b/packages/editor/src/dataviews/store/private-actions.ts index 0db70c8a0e6e7a..682a0c29b28e07 100644 --- a/packages/editor/src/dataviews/store/private-actions.ts +++ b/packages/editor/src/dataviews/store/private-actions.ts @@ -88,11 +88,12 @@ export function unregisterEntityField( }; } -export function setIsReady( kind: string, name: string ) { +export function setIsReady( kind: string, name: string, part: string ) { return { type: 'SET_IS_READY' as const, kind, name, + part, }; } @@ -101,7 +102,8 @@ export const registerPostTypeActions = async ( { registry }: { registry: any } ) => { const isReady = unlock( registry.select( editorStore ) ).isEntityReady( 'postType', - postType + postType, + 'actions' ); if ( isReady ) { return; @@ -109,7 +111,8 @@ export const registerPostTypeActions = unlock( registry.dispatch( editorStore ) ).setIsReady( 'postType', - postType + postType, + 'actions' ); const postTypeConfig = ( await registry @@ -178,8 +181,20 @@ export const registerPostTypeActions = export const registerPostTypeFields = ( postType: string ) => async ( { registry }: { registry: any } ) => { - // TODO: do not register fields if there were already registered. - // Consider the existing isReady state. + const isReady = unlock( registry.select( editorStore ) ).isEntityReady( + 'postType', + postType, + 'fields' + ); + if ( isReady ) { + return; + } + + unlock( registry.dispatch( editorStore ) ).setIsReady( + 'postType', + postType, + 'fields' + ); const fields = [ featuredImageField, diff --git a/packages/editor/src/dataviews/store/private-selectors.ts b/packages/editor/src/dataviews/store/private-selectors.ts index e1daeb4032fc21..a683f1fbcc56ab 100644 --- a/packages/editor/src/dataviews/store/private-selectors.ts +++ b/packages/editor/src/dataviews/store/private-selectors.ts @@ -13,6 +13,11 @@ export function getEntityFields( state: State, kind: string, name: string ) { return state.fields[ kind ]?.[ name ] ?? EMPTY_ARRAY; } -export function isEntityReady( state: State, kind: string, name: string ) { - return state.isReady[ kind ]?.[ name ]; +export function isEntityReady( + state: State, + kind: string, + name: string, + part: string +) { + return state.isReady[ kind ]?.[ name ]?.[ part ]; } diff --git a/packages/editor/src/dataviews/store/reducer.ts b/packages/editor/src/dataviews/store/reducer.ts index 94d7f2e6c4f190..74a0bd91564390 100644 --- a/packages/editor/src/dataviews/store/reducer.ts +++ b/packages/editor/src/dataviews/store/reducer.ts @@ -13,7 +13,10 @@ type ReduxAction = export type ActionState = Record< string, Record< string, Action< any >[] > >; export type FieldsState = Record< string, Record< string, Field< any >[] > >; -export type ReadyState = Record< string, Record< string, boolean > >; +export type ReadyState = Record< + string, + Record< string, Record< string, boolean > > +>; export type State = { actions: ActionState; fields: FieldsState; @@ -27,7 +30,10 @@ function isReady( state: ReadyState = {}, action: ReduxAction ) { ...state, [ action.kind ]: { ...state[ action.kind ], - [ action.name ]: true, + [ action.name ]: { + ...( state[ action.kind ]?.[ action.name ] ?? {} ), + [ action.part ]: true, + }, }, }; }