import { Button, CompactCard, ScreenReaderText, Gridicon, Spinner } from '@automattic/components'; import clsx from 'clsx'; import { localize } from 'i18n-calypso'; import PropTypes from 'prop-types'; import { Fragment, PureComponent } from 'react'; import Notice from 'calypso/components/notice'; class Task extends PureComponent { static propTypes = { action: PropTypes.string, buttonText: PropTypes.node, collapsed: PropTypes.bool, // derived from ui state completed: PropTypes.bool, completedButtonText: PropTypes.node, completedTitle: PropTypes.node, description: PropTypes.node, disableIcon: PropTypes.bool, duration: PropTypes.string, forceCollapsed: PropTypes.bool, // derived from API state href: PropTypes.string, inProgress: PropTypes.bool, isButtonDisabled: PropTypes.bool, isWarning: PropTypes.bool, noticeText: PropTypes.string, onClick: PropTypes.func, onTaskClick: PropTypes.func, onDismiss: PropTypes.func, target: PropTypes.string, title: PropTypes.node.isRequired, translate: PropTypes.func.isRequired, trackTaskDisplay: PropTypes.func, showSkip: PropTypes.bool, }; static defaultProps = { trackTaskDisplay: () => {}, }; componentDidMount() { this.props.trackTaskDisplay( this.props.id, this.props.completed, 'checklist' ); } renderCheckmarkIcon() { const { completed, disableIcon, inProgress, isWarning, translate } = this.props; const onDismiss = ! completed ? this.props.onDismiss : undefined; if ( inProgress ) { return ( { translate( 'In progress' ) } { this.renderGridicon() } ); } if ( disableIcon ) { return (
{ translate( 'Waiting to complete' ) }
); } if ( onDismiss ) { return (
{ completed ? translate( 'Mark as uncompleted' ) : translate( 'Mark as completed' ) } { this.renderGridicon() }
); } if ( completed ) { return (
{ translate( 'Complete' ) } { this.renderGridicon() }
); } if ( isWarning ) { return (
{ translate( 'Warning' ) } { this.renderGridicon() }
); } return null; } renderGridicon() { if ( this.props.inProgress ) { return ; } if ( this.props.isWarning ) { return (
); } return ; } render() { const { action, buttonText, collapsed, completed, completedDescription, completedButtonText, completedTitle, description, duration, forceCollapsed, href, isButtonDisabled, inProgress, isWarning, noticeText, onClick, target, title, translate, onDismiss, showSkip, } = this.props; const _collapsed = forceCollapsed || collapsed; // A task that's being automatically completed ("in progress") cannot be expanded. // An uncompleted task by definition has a call-to-action, which can only be accessed by // expanding it, so an uncompleted task is always expandable. // A completed task may or may not have a call-to-action, which can be best inferred from // the `completedButtonText` prop. const isExpandable = ! forceCollapsed || ( ! inProgress && ( ! completed || completedButtonText ) ); const taskActionButtonText = completed ? completedButtonText : buttonText || translate( 'Try it' ); return (

{ isExpandable ? ( ) : ( completedTitle ) }

{ ! _collapsed && (

{ completed && completedDescription ? completedDescription : description }

{ ! completed && duration && ( { translate( 'Estimated time:' ) } { duration } ) }
{ !! taskActionButtonText && ( ) } { ! completed && showSkip && ( ) } { !! noticeText && ( { noticeText } ) }
) }
{ this.renderCheckmarkIcon() }
); } } export default localize( Task );