-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.ts
30 lines (26 loc) · 859 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { WP_REST_API_Term } from 'wp-types';
export const useSelectedTermsOfSavedPost = (taxonomyName: string, postId: number | string) => {
return useSelect(
(select) => {
// @ts-ignore-next-line - The type definitions for the core store are incomplete.
const { getEntityRecords, hasFinishedResolution } = select(coreStore);
const selectedTermsQuery = [
'taxonomy',
taxonomyName,
{
per_page: -1,
post: postId as number,
},
] as const;
const terms = getEntityRecords<WP_REST_API_Term>(...selectedTermsQuery);
const hasResolvedTerms: boolean = hasFinishedResolution(
'getEntityRecords',
selectedTermsQuery,
);
return [terms, hasResolvedTerms] as const;
},
[taxonomyName, postId],
);
};