Skip to content

Commit

Permalink
Consolidate registerPostType{Actions|Fields} into registerPostTypeSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Nov 22, 2024
1 parent df078fd commit 3e6782e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 63 deletions.
6 changes: 3 additions & 3 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
[ postType ]
);

const { registerPostTypeActions } = unlock( useDispatch( editorStore ) );
const { registerPostTypeSchema } = unlock( useDispatch( editorStore ) );
useEffect( () => {
registerPostTypeActions( postType );
}, [ registerPostTypeActions, postType ] );
registerPostTypeSchema( postType );
}, [ registerPostTypeSchema, postType ] );

return useMemo( () => {
// Filter actions based on provided context. If not provided
Expand Down
6 changes: 3 additions & 3 deletions packages/editor/src/components/post-fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ interface Author {
function usePostFields(): UsePostFieldsReturn {
const postType = 'page'; // TODO: this could be page or post (experimental).

const { registerPostTypeFields } = unlock( useDispatch( editorStore ) );
const { registerPostTypeSchema } = unlock( useDispatch( editorStore ) );
useEffect( () => {
registerPostTypeFields( postType );
}, [ registerPostTypeFields, postType ] );
registerPostTypeSchema( postType );
}, [ registerPostTypeSchema, postType ] );

const { defaultFields } = useSelect(
( select ) => {
Expand Down
57 changes: 15 additions & 42 deletions packages/editor/src/dataviews/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,28 @@ export function unregisterEntityField(
};
}

export function setIsReady( kind: string, name: string, part: string ) {
export function setIsReady( kind: string, name: string ) {
return {
type: 'SET_IS_READY' as const,
kind,
name,
part,
};
}

export const registerPostTypeActions =
export const registerPostTypeSchema =
( postType: string ) =>
async ( { registry }: { registry: any } ) => {
const isReady = unlock( registry.select( editorStore ) ).isEntityReady(
'postType',
postType,
'actions'
postType
);
if ( isReady ) {
return;
}

unlock( registry.dispatch( editorStore ) ).setIsReady(
'postType',
postType,
'actions'
postType
);

const postTypeConfig = ( await registry
Expand Down Expand Up @@ -162,40 +159,6 @@ export const registerPostTypeActions =
permanentlyDeletePost,
];

registry.batch( () => {
actions.forEach( ( action ) => {
if ( ! action ) {
return;
}
unlock( registry.dispatch( editorStore ) ).registerEntityAction(
'postType',
postType,
action
);
} );
} );

doAction( 'core.registerPostTypeActions', postType );
};

export const registerPostTypeFields =
( postType: string ) =>
async ( { registry }: { registry: any } ) => {
const isReady = unlock( registry.select( editorStore ) ).isEntityReady(
'postType',
postType,
'fields'
);
if ( isReady ) {
return;
}

unlock( registry.dispatch( editorStore ) ).setIsReady(
'postType',
postType,
'fields'
);

const fields = [
featuredImageField,
titleField,
Expand All @@ -209,6 +172,16 @@ export const registerPostTypeFields =
];

registry.batch( () => {
actions.forEach( ( action ) => {
if ( ! action ) {
return;
}
unlock( registry.dispatch( editorStore ) ).registerEntityAction(
'postType',
postType,
action
);
} );
fields.forEach( ( field ) => {
unlock( registry.dispatch( editorStore ) ).registerEntityField(
'postType',
Expand All @@ -218,5 +191,5 @@ export const registerPostTypeFields =
} );
} );

doAction( 'core.registerPostTypeFields', postType );
doAction( 'core.registerPostTypeSchema', postType );
};
9 changes: 2 additions & 7 deletions packages/editor/src/dataviews/store/private-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ 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,
part: string
) {
return state.isReady[ kind ]?.[ name ]?.[ part ];
export function isEntityReady( state: State, kind: string, name: string ) {
return state.isReady[ kind ]?.[ name ];
}
10 changes: 2 additions & 8 deletions packages/editor/src/dataviews/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ 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, Record< string, boolean > >
>;
export type ReadyState = Record< string, Record< string, boolean > >;
export type State = {
actions: ActionState;
fields: FieldsState;
Expand All @@ -30,10 +27,7 @@ function isReady( state: ReadyState = {}, action: ReduxAction ) {
...state,
[ action.kind ]: {
...state[ action.kind ],
[ action.name ]: {
...( state[ action.kind ]?.[ action.name ] ?? {} ),
[ action.part ]: true,
},
[ action.name ]: true,
},
};
}
Expand Down

0 comments on commit 3e6782e

Please sign in to comment.