-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
parent.js
77 lines (69 loc) · 2.05 KB
/
parent.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* External dependencies
*/
import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { TreeSelect } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { buildTermsTree } from '../../utils/terms';
export function PageAttributesParent( { parent, postType, items, onUpdateParent } ) {
const isHierarchical = get( postType, [ 'hierarchical' ], false );
const parentPageLabel = get( postType, [ 'labels', 'parent_item_colon' ] );
const pageItems = items || [];
if ( ! isHierarchical || ! parentPageLabel || ! pageItems.length ) {
return null;
}
const pagesTree = buildTermsTree( pageItems.map( ( item ) => ( {
id: item.id,
parent: item.parent,
name: item.title.raw ? item.title.raw : `#${ item.id } (${ __( 'no title' ) })`,
} ) ) );
return (
<TreeSelect
label={ parentPageLabel }
noOptionLabel={ `(${ __( 'no parent' ) })` }
tree={ pagesTree }
selectedId={ parent }
onChange={ onUpdateParent }
/>
);
}
const applyWithSelect = withSelect( ( select ) => {
const { getPostType, getEntityRecords } = select( 'core' );
const { getCurrentPostId, getEditedPostAttribute } = select( 'core/editor' );
const postTypeSlug = getEditedPostAttribute( 'type' );
const postType = getPostType( postTypeSlug );
const postId = getCurrentPostId();
const isHierarchical = get( postType, [ 'hierarchical' ], false );
const query = {
per_page: -1,
exclude: postId,
parent_exclude: postId,
orderby: 'menu_order',
order: 'asc',
};
return {
parent: getEditedPostAttribute( 'parent' ),
items: isHierarchical ? getEntityRecords( 'postType', postTypeSlug, query ) : [],
postType,
};
} );
const applyWithDispatch = withDispatch( ( dispatch ) => {
const { editPost } = dispatch( 'core/editor' );
return {
onUpdateParent( parent ) {
editPost( { parent: parent || 0 } );
},
};
} );
export default compose( [
applyWithSelect,
applyWithDispatch,
] )( PageAttributesParent );