File size: 4,056 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 |
import { Card } from '@automattic/components';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import { times } from 'lodash';
import PropTypes from 'prop-types';
import { Children, PureComponent, cloneElement } from 'react';
import { useSelector } from 'react-redux';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import TaskPlaceholder from './task-placeholder';
class Checklist extends PureComponent {
static propTypes = {
className: PropTypes.string,
isPlaceholder: PropTypes.bool,
onExpandTask: PropTypes.func,
showChecklistHeader: PropTypes.bool,
checklistFooter: PropTypes.node,
updateCompletion: PropTypes.func,
siteId: PropTypes.number,
translate: PropTypes.func,
};
state = {
expandedTaskIndex: undefined,
};
componentDidMount() {
this.notifyCompletion();
}
componentDidUpdate() {
this.notifyCompletion();
}
notifyCompletion() {
if ( 'function' === typeof this.props.updateCompletion ) {
const [ complete, total ] = this.calculateCompletion();
this.props.updateCompletion( { complete: complete >= total } );
}
}
calculateCompletion() {
const { children } = this.props;
const childrenArray = Children.toArray( children ).filter( Boolean );
const completedCount = childrenArray.reduce(
( count, task ) => ( true === task.props.completed ? count + 1 : count ),
0
);
const total = childrenArray.length;
return [ completedCount, total ];
}
getExpandedTaskIndex() {
if ( this.state.expandedTaskIndex !== undefined ) {
return this.state.expandedTaskIndex;
}
// If the user hasn't expanded any task, return the
// first task that hasn't been completed yet.
return Children.toArray( this.props.children ).findIndex(
( task ) => task && ! task.props.completed && ! task.props.inProgress
);
}
setExpandedTask = ( newExpandedTaskIndex ) =>
void this.setState( ( { expandedTaskIndex } ) => {
if ( newExpandedTaskIndex === expandedTaskIndex ) {
return { expandedTaskIndex: null }; // Collapse
}
if ( typeof this.props.onExpandTask === 'function' ) {
this.props.onExpandTask(
Children.toArray( this.props.children )[ newExpandedTaskIndex ].props
);
}
return { expandedTaskIndex: newExpandedTaskIndex }; // Expand
} );
renderChecklistHeader = () => {
return (
<Card compact className="checklist__header">
<h2>{ this.props.translate( 'Your setup list' ) }</h2>
</Card>
);
};
render() {
const { showChecklistHeader, checklistFooter } = this.props;
const [ completed, total ] = this.calculateCompletion();
if ( this.props.isPlaceholder ) {
return (
<div className={ clsx( 'checklist', 'is-expanded', 'is-placeholder' ) }>
{ showChecklistHeader && completed !== total && this.renderChecklistHeader() }
<div className="checklist__tasks">
{ times( total, ( index ) => (
<TaskPlaceholder key={ index } />
) ) }
</div>
</div>
);
}
let skippedChildren = 0;
return (
<div className={ clsx( 'checklist', this.props.className ) }>
{ showChecklistHeader && completed !== total && this.renderChecklistHeader() }
<div className="checklist__tasks">
{ Children.map( this.props.children, ( child, index ) => {
if ( ! child ) {
skippedChildren += 1;
return child;
}
const realIndex = index - skippedChildren;
return cloneElement( child, {
collapsed: realIndex !== this.getExpandedTaskIndex(),
onTaskClick: () => this.setExpandedTask( realIndex ),
} );
} ) }
{ checklistFooter }
{ completed > 0 && completed < total && (
<div className="checklist__tasks-completed-title">
{ this.props.translate( 'Completed' ) }
</div>
) }
</div>
</div>
);
}
}
const ChecklistForSite = ( props ) => {
const siteId = useSelector( getSelectedSiteId );
const translate = useTranslate();
return <Checklist key={ siteId } siteId={ siteId } translate={ translate } { ...props } />;
};
export default ChecklistForSite;
|