| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { useBlockProps, InspectorControls } from '@wordpress/block-editor'; |
| 3 |
import { Disabled, PanelBody } from '@wordpress/components'; |
| 4 |
import ServerSideRender from '@wordpress/server-side-render'; |
| 5 |
|
| 6 |
import { ToggleControlGroup } from './components/toggle-control-group'; |
| 7 |
import { RangeControls } from './components/range-controls'; |
| 8 |
import { PostDisplayControls } from './components/post-display-controls'; |
| 9 |
import { StyleControls } from './components/style-controls'; |
| 10 |
import { OtherAttributesControl } from './components/other-attributes-control'; |
| 11 |
|
| 12 |
export default function Edit({ attributes, setAttributes }) { |
| 13 |
const { |
| 14 |
heading, |
| 15 |
daily, |
| 16 |
show_excerpt, |
| 17 |
show_author, |
| 18 |
show_date, |
| 19 |
disp_list_count, |
| 20 |
other_attributes, |
| 21 |
} = attributes; |
| 22 |
|
| 23 |
const blockProps = useBlockProps(); |
| 24 |
|
| 25 |
const handleToggle = (attributeName) => () => { |
| 26 |
setAttributes({ [attributeName]: !attributes[attributeName] }); |
| 27 |
}; |
| 28 |
|
| 29 |
const handleChange = (attributeName) => (newValue) => { |
| 30 |
setAttributes({ [attributeName]: newValue }); |
| 31 |
}; |
| 32 |
|
| 33 |
return ( |
| 34 |
<> |
| 35 |
<InspectorControls> |
| 36 |
<PanelBody |
| 37 |
title={__('Popular Posts Settings', 'top-10')} |
| 38 |
initialOpen={true} |
| 39 |
> |
| 40 |
<ToggleControlGroup |
| 41 |
controls={[ |
| 42 |
{ |
| 43 |
label: __('Custom period?', 'top-10'), |
| 44 |
attributeName: 'daily', |
| 45 |
checked: daily, |
| 46 |
onChange: handleToggle('daily'), |
| 47 |
}, |
| 48 |
]} |
| 49 |
/> |
| 50 |
|
| 51 |
{daily && ( |
| 52 |
<RangeControls |
| 53 |
attributes={attributes} |
| 54 |
onChange={handleChange} |
| 55 |
/> |
| 56 |
)} |
| 57 |
|
| 58 |
<PostDisplayControls |
| 59 |
attributes={attributes} |
| 60 |
onChange={handleChange} |
| 61 |
/> |
| 62 |
|
| 63 |
<ToggleControlGroup |
| 64 |
controls={[ |
| 65 |
{ |
| 66 |
label: __('Show heading', 'top-10'), |
| 67 |
attributeName: 'heading', |
| 68 |
checked: heading, |
| 69 |
onChange: handleToggle('heading'), |
| 70 |
}, |
| 71 |
{ |
| 72 |
label: __('Show excerpt', 'top-10'), |
| 73 |
attributeName: 'show_excerpt', |
| 74 |
checked: show_excerpt, |
| 75 |
onChange: handleToggle('show_excerpt'), |
| 76 |
}, |
| 77 |
{ |
| 78 |
label: __('Show author', 'top-10'), |
| 79 |
attributeName: 'show_author', |
| 80 |
checked: show_author, |
| 81 |
onChange: handleToggle('show_author'), |
| 82 |
}, |
| 83 |
{ |
| 84 |
label: __('Show date', 'top-10'), |
| 85 |
attributeName: 'show_date', |
| 86 |
checked: show_date, |
| 87 |
onChange: handleToggle('show_date'), |
| 88 |
}, |
| 89 |
{ |
| 90 |
label: __('Show count', 'top-10'), |
| 91 |
attributeName: 'disp_list_count', |
| 92 |
checked: disp_list_count, |
| 93 |
onChange: handleToggle('disp_list_count'), |
| 94 |
}, |
| 95 |
]} |
| 96 |
/> |
| 97 |
|
| 98 |
<StyleControls |
| 99 |
attributes={attributes} |
| 100 |
onChange={handleChange} |
| 101 |
/> |
| 102 |
|
| 103 |
<OtherAttributesControl |
| 104 |
value={other_attributes} |
| 105 |
onChange={handleChange('other_attributes')} |
| 106 |
/> |
| 107 |
</PanelBody> |
| 108 |
</InspectorControls> |
| 109 |
|
| 110 |
<div {...blockProps}> |
| 111 |
<Disabled> |
| 112 |
<ServerSideRender |
| 113 |
block="top-10/popular-posts" |
| 114 |
attributes={attributes} |
| 115 |
urlQueryArgs={{ _locale: 'site' }} |
| 116 |
/> |
| 117 |
</Disabled> |
| 118 |
</div> |
| 119 |
</> |
| 120 |
); |
| 121 |
} |
| 122 |
|