edit.js
146 lines
| 1 | import { useState } from 'react'; |
| 2 | import { Notice, Spinner, PanelBody, TextControl, CheckboxControl, BaseControl, RadioControl, SelectControl } from '@wordpress/components'; |
| 3 | import { __ } from '@wordpress/i18n'; |
| 4 | import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; |
| 5 | import ServerSideRender from '@wordpress/server-side-render'; |
| 6 | |
| 7 | export default function Edit( { attributes, setAttributes } ) { |
| 8 | // attributes |
| 9 | const { title, postTypes, period, numberOfPosts, noPostsMessage, order, listType, displayPostViews, displayPostExcerpt, displayPostAuthor, displayPostThumbnail, thumbnailSize } = attributes; |
| 10 | |
| 11 | // initialize post types state |
| 12 | const [checkedState, setCheckedState] = useState( ! postTypes ? pvcBlockEditorData.postTypesKeys : postTypes ); |
| 13 | |
| 14 | // spinner |
| 15 | const spinner = () => { |
| 16 | return <Spinner />; |
| 17 | } |
| 18 | |
| 19 | const error = ( value ) => { |
| 20 | return <Notice status="error">{ __( 'Something went wrong. Try again or refresh the page.', 'post-views-counter' ) }</Notice>; |
| 21 | } |
| 22 | |
| 23 | return ( |
| 24 | <> |
| 25 | <InspectorControls> |
| 26 | <PanelBody title={ __( 'Settings', 'post-views-counter' ) }> |
| 27 | <TextControl |
| 28 | __nextHasNoMarginBottom |
| 29 | label={ __( 'Title', 'post-views-counter' ) } |
| 30 | value={ title } |
| 31 | onChange={ ( value ) => setAttributes( { title: value } ) } |
| 32 | help={ __( 'Enter empty text to hide title.', 'post-views-counter' ) } |
| 33 | /> |
| 34 | <BaseControl |
| 35 | __nextHasNoMarginBottom |
| 36 | label={ __( 'Post types', 'post-views-counter' ) } |
| 37 | > |
| 38 | { ( |
| 39 | Object.keys( pvcBlockEditorData.postTypes ).map( ( postType ) => ( |
| 40 | <CheckboxControl |
| 41 | key={ postType } |
| 42 | label={ pvcBlockEditorData.postTypes[postType] } |
| 43 | checked={ checkedState[postType] } |
| 44 | onChange={ ( value ) => { |
| 45 | // clone postTypes, we cant change attribute value directly |
| 46 | let newValue = {...postTypes} |
| 47 | |
| 48 | // set new value |
| 49 | newValue[postType] = value |
| 50 | |
| 51 | // set state and attribute |
| 52 | setCheckedState( ( prevState ) => ( { ...prevState, [postType]: ! prevState[postType] } ) ) |
| 53 | setAttributes( { postTypes: newValue } ) |
| 54 | } } |
| 55 | /> |
| 56 | ) ) |
| 57 | ) } |
| 58 | </BaseControl> |
| 59 | <SelectControl |
| 60 | __nextHasNoMarginBottom |
| 61 | disabled={ pvcBlockEditorData.periods.length === 1 } |
| 62 | label={ __( 'Views period', 'post-views-counter' ) } |
| 63 | value={ period } |
| 64 | options={ pvcBlockEditorData.periods } |
| 65 | onChange={ ( value ) => setAttributes( { period: value } ) } |
| 66 | /> |
| 67 | <TextControl |
| 68 | __nextHasNoMarginBottom |
| 69 | label={ __( 'Number of posts to show', 'post-views-counter' ) } |
| 70 | value={ numberOfPosts } |
| 71 | onChange={ ( value ) => setAttributes( { numberOfPosts: Number( value ) } ) } |
| 72 | /> |
| 73 | <TextControl |
| 74 | __nextHasNoMarginBottom |
| 75 | label={ __( 'No posts message', 'post-views-counter' ) } |
| 76 | value={ noPostsMessage } |
| 77 | onChange={ ( value ) => setAttributes( { noPostsMessage: value } ) } |
| 78 | /> |
| 79 | <RadioControl |
| 80 | label={ __( 'Order', 'post-views-counter' ) } |
| 81 | selected={ order } |
| 82 | options={ [ |
| 83 | { label: __( 'Ascending', 'post-views-counter' ), value: 'asc' }, |
| 84 | { label: __( 'Descending', 'post-views-counter' ), value: 'desc' } |
| 85 | ] } |
| 86 | onChange={ ( value ) => setAttributes( { order: value } ) } |
| 87 | /> |
| 88 | <RadioControl |
| 89 | label={ __( 'Display style', 'post-views-counter' ) } |
| 90 | selected={ listType } |
| 91 | options={ [ |
| 92 | { label: __( 'Unordered list', 'post-views-counter' ), value: 'unordered' }, |
| 93 | { label: __( 'Ordered list', 'post-views-counter' ), value: 'ordered' } |
| 94 | ] } |
| 95 | onChange={ ( value ) => setAttributes( { listType: value } ) } |
| 96 | /> |
| 97 | <BaseControl |
| 98 | __nextHasNoMarginBottom |
| 99 | label={ __( 'Display Data', 'post-views-counter' ) } |
| 100 | > |
| 101 | <CheckboxControl |
| 102 | __nextHasNoMarginBottom |
| 103 | label={ __( 'Post views', 'post-views-counter' ) } |
| 104 | checked={ displayPostViews } |
| 105 | onChange={ ( value ) => setAttributes( { displayPostViews: value } ) } |
| 106 | /> |
| 107 | <CheckboxControl |
| 108 | __nextHasNoMarginBottom |
| 109 | label={ __( 'Post excerpt', 'post-views-counter' ) } |
| 110 | checked={ displayPostExcerpt } |
| 111 | onChange={ ( value ) => setAttributes( { displayPostExcerpt: value } ) } |
| 112 | /> |
| 113 | <CheckboxControl |
| 114 | __nextHasNoMarginBottom |
| 115 | label={ __( 'Post author', 'post-views-counter' ) } |
| 116 | checked={ displayPostAuthor } |
| 117 | onChange={ ( value ) => setAttributes( { displayPostAuthor: value } ) } |
| 118 | /> |
| 119 | <CheckboxControl |
| 120 | __nextHasNoMarginBottom |
| 121 | label={ __( 'Post thumbnail', 'post-views-counter' ) } |
| 122 | checked={ displayPostThumbnail } |
| 123 | onChange={ ( value ) => setAttributes( { displayPostThumbnail: value } ) } |
| 124 | /> |
| 125 | { displayPostThumbnail && <SelectControl |
| 126 | __nextHasNoMarginBottom |
| 127 | label={ __( 'Thumbnail size', 'post-views-counter' ) } |
| 128 | value={ thumbnailSize } |
| 129 | options={ pvcBlockEditorData.imageSizes } |
| 130 | onChange={ ( value ) => setAttributes( { thumbnailSize: value } ) } |
| 131 | /> } |
| 132 | </BaseControl> |
| 133 | </PanelBody> |
| 134 | </InspectorControls> |
| 135 | <div { ...useBlockProps() }> |
| 136 | <ServerSideRender |
| 137 | httpMethod="POST" |
| 138 | block="post-views-counter/most-viewed-posts" |
| 139 | attributes={ attributes } |
| 140 | LoadingResponsePlaceholder={ spinner } |
| 141 | ErrorResponsePlaceholder={ error } |
| 142 | /> |
| 143 | </div> |
| 144 | </> |
| 145 | ) |
| 146 | } |