1 |
|
2 |
|
3 |
|
4 | import { InspectorControls } from '@wordpress/block-editor';
|
5 | import {
|
6 | Disabled,
|
7 | PanelBody,
|
8 | RangeControl,
|
9 | ToggleControl,
|
10 | } from '@wordpress/components';
|
11 | import { ServerSideRender } from '@wordpress/editor';
|
12 | import { Component, Fragment } from '@wordpress/element';
|
13 | import { __ } from '@wordpress/i18n';
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const MIN_COMMENTS = 1;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const MAX_COMMENTS = 100;
|
27 |
|
28 | class LatestComments extends Component {
|
29 | constructor() {
|
30 | super( ...arguments );
|
31 |
|
32 | this.setCommentsToShow = this.setCommentsToShow.bind( this );
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | this.toggleDisplayAvatar = this.createToggleAttribute( 'displayAvatar' );
|
38 | this.toggleDisplayDate = this.createToggleAttribute( 'displayDate' );
|
39 | this.toggleDisplayExcerpt = this.createToggleAttribute( 'displayExcerpt' );
|
40 | }
|
41 |
|
42 | createToggleAttribute( propName ) {
|
43 | return () => {
|
44 | const value = this.props.attributes[ propName ];
|
45 | const { setAttributes } = this.props;
|
46 |
|
47 | setAttributes( { [ propName ]: ! value } );
|
48 | };
|
49 | }
|
50 |
|
51 | setCommentsToShow( commentsToShow ) {
|
52 | this.props.setAttributes( { commentsToShow } );
|
53 | }
|
54 |
|
55 | render() {
|
56 | const {
|
57 | commentsToShow,
|
58 | displayAvatar,
|
59 | displayDate,
|
60 | displayExcerpt,
|
61 | } = this.props.attributes;
|
62 |
|
63 | return (
|
64 | <Fragment>
|
65 | <InspectorControls>
|
66 | <PanelBody title={ __( 'Latest Comments Settings' ) }>
|
67 | <ToggleControl
|
68 | label={ __( 'Display Avatar' ) }
|
69 | checked={ displayAvatar }
|
70 | onChange={ this.toggleDisplayAvatar }
|
71 | />
|
72 | <ToggleControl
|
73 | label={ __( 'Display Date' ) }
|
74 | checked={ displayDate }
|
75 | onChange={ this.toggleDisplayDate }
|
76 | />
|
77 | <ToggleControl
|
78 | label={ __( 'Display Excerpt' ) }
|
79 | checked={ displayExcerpt }
|
80 | onChange={ this.toggleDisplayExcerpt }
|
81 | />
|
82 | <RangeControl
|
83 | label={ __( 'Number of Comments' ) }
|
84 | value={ commentsToShow }
|
85 | onChange={ this.setCommentsToShow }
|
86 | min={ MIN_COMMENTS }
|
87 | max={ MAX_COMMENTS }
|
88 | required
|
89 | />
|
90 | </PanelBody>
|
91 | </InspectorControls>
|
92 | <Disabled>
|
93 | <ServerSideRender
|
94 | block="core/latest-comments"
|
95 | attributes={ this.props.attributes }
|
96 | />
|
97 | </Disabled>
|
98 | </Fragment>
|
99 | );
|
100 | }
|
101 | }
|
102 |
|
103 | export default LatestComments;
|