import { InspectorControls } from '@wordpress/block-editor'; import { CustomSelectControl, PanelBody, TextControl } from '@wordpress/components'; import { withSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; import { FunctionComponent } from 'react'; import type { BlockAttributes, SelectAttributes } from './types'; import type { BlockEditProps } from '@wordpress/blocks'; import './editor.scss'; type EditProps = BlockEditProps< BlockAttributes > & SelectAttributes; const edit: FunctionComponent< EditProps > = ( args ) => { const { allTasksLive, attributes, completedTasksLive, pendingTasksLive, setAttributes } = args; const { estimate, team, allTasks, pendingTasks, completedTasks } = attributes; const tasksToUpdate = Object.assign( {}, allTasks !== allTasksLive && { allTasks: allTasksLive }, pendingTasks !== pendingTasksLive && { pendingTasks: pendingTasksLive }, completedTasks !== completedTasksLive && { completedTasks: completedTasksLive } ); if ( Object.getOwnPropertyNames( tasksToUpdate ).length > 0 ) { setAttributes( tasksToUpdate ); } const estimates = [ { key: '', name: '---', }, { key: '1-week', name: '1 week', }, { key: '2-weeks', name: '2 weeks', }, { key: '3-weeks', name: '3 weeks', }, ]; const completedPercentage = Math.round( ( completedTasks * 100 ) / ( allTasks + Number.EPSILON ) ); return ( <> setAttributes( { team: value } ) } /> option.key === estimate ) || estimates[ 0 ] } onChange={ ( value ) => setAttributes( { estimate: value.selectedItem.key } ) } />

Project Overview

{ allTasks } Tasks { '\u2003' } { completedTasks } Completed ({ completedPercentage }%) { '\u2003' } { pendingTasks } In progress
Completed Tasks In-Progress Tasks
{ ( estimate || team ) && (
{ team && { 'Team ' + team } } { estimate && ( { estimate?.find( ( option ) => option.key === estimate ).name } ) }
) } ); }; export default withSelect( ( select ) => { const tasks = select( 'core/block-editor' ) .getBlocks() .filter( ( block ) => { return block.name === 'a8c/task'; } ); return { allTasksLive: tasks.length, completedTasksLive: tasks.filter( ( task ) => task.attributes.status === 'done' ).length, pendingTasksLive: tasks.filter( ( task ) => task.attributes.status === 'in-progress' ).length, }; } )( edit );