Newer
Older
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
78
79
/* global externalPermalinksReduxConfig */
import { TextControl, SelectControl } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
const {
metaKeys: { target: metaKeyTarget, type: metaKeyType },
statusCodes,
} = externalPermalinksReduxConfig;
const View = ( { setTarget, setType, target, type } ) => {
return (
<>
<TextControl
label={ __(
'Destination Address:',
'external-permalinks-redux'
) }
help={ __(
'To restore the original permalink, remove the link entered above.',
'external-permalinks-redux'
) }
type="url"
value={ target }
onChange={ setTarget }
/>
<SelectControl
label={ __( 'Redirect Type:', 'external-permalinks-redux' ) }
options={ statusCodes }
selected={ type }
onChange={ setType }
/>
</>
);
};
/**
* HOC to provide meta values and methods for updating meta.
*/
const PanelBody = compose( [
withSelect( ( select ) => {
const { getEditedPostAttribute } = select( 'core/editor' );
const meta = getEditedPostAttribute( 'meta' );
return {
target: meta[ metaKeyTarget ],
type: meta[ metaKeyType ],
};
} ),
withDispatch( ( dispatch ) => {
const { editPost } = dispatch( 'core/editor' );
const setTarget = ( target ) => {
editPost( {
meta: {
[ metaKeyTarget ]: target.trim(),
},
} );
};
const setType = ( type ) => {
editPost( {
meta: {
[ metaKeyType ]: parseInt( type, 10 ),
},
} );
};
return {
setTarget,
setType,
};
} ),
] )( View );
export default PanelBody;