File size: 4,356 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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 (
		<>
			<InspectorControls>
				<PanelBody title={ __( 'Project Settings' ) }>
					<TextControl
						label={ __( 'Team Assignment' ) }
						value={ team }
						onChange={ ( value ) => setAttributes( { team: value } ) }
					/>
					<CustomSelectControl
						label={ __( 'Time Estimate' ) }
						options={ estimates }
						value={ estimates.find( ( option ) => option.key === estimate ) || estimates[ 0 ] }
						onChange={ ( value ) => setAttributes( { estimate: value.selectedItem.key } ) }
					/>
				</PanelBody>
			</InspectorControls>
			<section className="wp-block-project-status__header">
				<h1 className="wp-block-project-status__title">Project Overview</h1>
				<div className="wp-block-project-status__counts">
					<strong className="wp-block-project-status__total">{ allTasks } Tasks</strong>
					{ '\u2003' }
					<span>
						{ completedTasks } Completed ({ completedPercentage }%)
					</span>
					{ '\u2003' }
					<span>{ pendingTasks } In progress</span>
				</div>
			</section>
			<div className="wp-block-project-status__bar">
				<span className="if-missing-style">Completed Tasks</span>
				<span
					style={ {
						display: 'block',
						width: `${ Math.round( ( completedTasks * 730 ) / ( allTasks + Number.EPSILON ) ) }px`,
						background: '#22DE84',
						height: '18px',
					} }
				/>
				<span
					className="if-missing-style"
					style={ {
						display: 'block',
						width: `${ Math.round(
							730 * ( 1 - pendingTasks / ( allTasks + Number.EPSILON ) )
						) }px`,
						background: '#207c3e',
						height: '3px',
					} }
				/>

				<span className="if-missing-style">In-Progress Tasks</span>
				<span
					style={ {
						display: 'block',
						width: `${ Math.round( ( pendingTasks * 730 ) / ( allTasks + Number.EPSILON ) ) }px`,
						background: '#D6F3E3',
						height: '18px',
					} }
				/>
				<span
					className="if-missing-style"
					style={ {
						display: 'block',
						width: `${ Math.round(
							730 * ( 1 - completedTasks / ( allTasks + Number.EPSILON ) )
						) }px`,
						background: '#8aa192',
						height: '3px',
					} }
				/>
			</div>
			{ ( estimate || team ) && (
				<div className="wp-block-project-status__footer">
					{ team && <span className="wp-block-project-status__team">{ 'Team ' + team }</span> }
					{ estimate && (
						<span className="wp-block-project-status__estimate">
							{ estimate?.find( ( option ) => option.key === estimate ).name }
						</span>
					) }
				</div>
			) }
		</>
	);
};

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 );