Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Make useIsSupportedMetaField hook context aware #263

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/src/blocks/post-meta/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
"default": ""
}
},
"usesContext": [
"postId",
"postType"
],
"editorScript": "file:./index.js"
}
9 changes: 6 additions & 3 deletions example/src/blocks/post-meta/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect, useDispatch } from '@wordpress/data';
import { PostMeta } from '@10up/block-components';
import { PostMeta, PostContext } from '@10up/block-components';

export const BlockEdit = (props) => {
const { attributes, setAttributes, name } = props;
const { attributes, setAttributes, name, context } = props;
const { postId, postType } = context;
const { metaKey } = attributes;
const blockProps = useBlockProps();

Expand Down Expand Up @@ -46,7 +47,9 @@ export const BlockEdit = (props) => {

return (
<div {...blockProps}>
<PostMeta metaKey={metaKey} placeholder="Meta Value" />
<PostContext postId={postId} postType={postType}>
<PostMeta metaKey={metaKey} placeholder="Meta Value" />
</PostContext>
</div>
);
};
19 changes: 8 additions & 11 deletions hooks/use-is-supported-meta-value/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { useEntityRecord } from '@wordpress/core-data';
import { usePost } from '../use-post';

export const useIsSupportedMetaField = (metaKey) => {
return useSelect(
(select) => {
const meta = select(editorStore).getCurrentPostAttribute('meta');
const supportedMetaKeys = Object.keys(meta || {});
const isSupportedMetaField = supportedMetaKeys?.some((name) => name === metaKey);
return [!!isSupportedMetaField];
},
[metaKey],
);
const { postId, postType } = usePost();
const { record } = useEntityRecord('postType', postType, postId);
const { meta } = record || {};
const supportedMetaKeys = Object.keys(meta || {});
const isSupportedMetaField = supportedMetaKeys?.some((name) => name === metaKey);
return [!!isSupportedMetaField];
};
Loading