/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { Component, cloneElement } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import classNames from 'classnames'; import { Button, Card, CardBody, CardHeader, __experimentalText as Text, } from '@wordpress/components'; import { withDispatch, withSelect } from '@wordpress/data'; import { Icon, check } from '@wordpress/icons'; import { List, EllipsisMenu, Badge } from '@woocommerce/components'; import { updateQueryString } from '@woocommerce/navigation'; import { PLUGINS_STORE_NAME, OPTIONS_STORE_NAME, ONBOARDING_STORE_NAME, SETTINGS_STORE_NAME, } from '@woocommerce/data'; import { recordEvent } from '@woocommerce/tracks'; /** * Internal dependencies */ import { recordTaskViewEvent } from './tasks'; import { getCountryCode } from '../dashboard/utils'; import sanitizeHTML from '../lib/sanitize-html'; export class TaskList extends Component { componentDidMount() { this.recordTaskView(); this.recordTaskListView(); this.possiblyCompleteTaskList(); this.possiblyTrackCompletedTasks(); } componentDidUpdate( prevProps ) { const { query } = this.props; const { query: prevQuery } = prevProps; const { task: prevTask } = prevQuery; const { task } = query; if ( prevTask !== task ) { window.document.documentElement.scrollTop = 0; this.recordTaskView(); } this.possiblyCompleteTaskList(); this.possiblyTrackCompletedTasks(); } possiblyCompleteTaskList() { const { isExtended, isTaskListComplete, isExtendedTaskListComplete, updateOptions, } = this.props; const isSetupTaskListInComplete = ! isExtended && ! isTaskListComplete; const isExtendedTaskListInComplete = isExtended && ! isExtendedTaskListComplete; const taskListToComplete = isExtended ? { woocommerce_extended_task_list_complete: 'yes' } : { woocommerce_task_list_complete: 'yes', woocommerce_default_homepage_layout: 'two_columns', }; if ( ! this.getIncompleteTasks().length && ( isSetupTaskListInComplete || isExtendedTaskListInComplete ) ) { updateOptions( { ...taskListToComplete, } ); } } getCompletedTaskKeys() { return this.getVisibleTasks( 'all' ) .filter( ( task ) => task.completed ) .map( ( task ) => task.key ); } getIncompleteTasks() { const { dismissedTasks, specificTasks } = this.props; return specificTasks.filter( ( task ) => task.visible && ! task.completed && ! dismissedTasks.includes( task.key ) ); } isTrackingListUpdated( list, subList ) { if ( subList.length === 0 ) { return false; } return subList.every( ( taskName ) => list.indexOf( taskName ) >= 0 ); } getIncludedTasks( list, subList ) { if ( ! subList ) { return []; } return list.filter( ( taskName ) => subList.includes( taskName ) ); } possiblyTrackCompletedTasks() { const { trackedCompletedTasks: totalTrackedCompletedTasks, updateOptions, } = this.props; const completedTaskKeys = this.getCompletedTaskKeys(); const trackedCompletedTasks = this.getIncludedTasks( completedTaskKeys, totalTrackedCompletedTasks ); if ( ! this.isTrackingListUpdated( trackedCompletedTasks, completedTaskKeys ) ) { updateOptions( { woocommerce_task_list_tracked_completed_tasks: completedTaskKeys, } ); } } dismissTask( { key, onDismiss } ) { const { createNotice, dismissedTasks, updateOptions } = this.props; createNotice( 'success', __( 'Task dismissed' ), { actions: [ { label: __( 'Undo', 'woocommerce-admin' ), onClick: () => this.undoDismissTask( key ), }, ], } ); recordEvent( 'tasklist_dismiss_task', { task_name: key } ); updateOptions( { woocommerce_task_list_dismissed_tasks: [ ...dismissedTasks, key ], } ); if ( onDismiss ) { onDismiss(); } } undoDismissTask( key ) { const { dismissedTasks, updateOptions } = this.props; const updatedDismissedTasks = dismissedTasks.filter( ( task ) => task !== key ); updateOptions( { woocommerce_task_list_dismissed_tasks: updatedDismissedTasks, } ); } getVisibleTasks( type ) { const { allTasks, specificTasks, dismissedTasks } = this.props; const tasks = type === 'all' ? allTasks : specificTasks; return tasks.filter( ( task ) => task.visible && ! dismissedTasks.includes( task.key ) ); } recordTaskView() { const { isJetpackConnected, activePlugins, installedPlugins, query, } = this.props; const { task: taskName } = query; if ( ! taskName ) { return; } recordTaskViewEvent( taskName, isJetpackConnected, activePlugins, installedPlugins ); } recordTaskListView() { if ( this.getCurrentTask() ) { return; } const { profileItems } = this.props; const tasks = this.getVisibleTasks(); recordEvent( 'tasklist_view', { number_tasks: tasks.length, store_connected: profileItems.wccom_connected, } ); } hideTaskCard( action, isExtended ) { const eventTaskListName = isExtended ? 'extended_tasklist_completed' : 'tasklist_completed'; const updateOptions = isExtended ? { woocommerce_extended_task_list_hidden: 'yes' } : { woocommerce_task_list_hidden: 'yes', woocommerce_task_list_prompt_shown: true, woocommerce_default_homepage_layout: 'two_columns', }; recordEvent( eventTaskListName, { action, completed_task_count: this.getCompletedTaskKeys().length, incomplete_task_count: this.getIncompleteTasks().length, } ); this.props.updateOptions( { ...updateOptions, } ); } getCurrentTask() { const { specificTasks, query } = this.props; const { task } = query; const currentTask = specificTasks.find( ( s ) => s.key === task ); if ( ! currentTask ) { return null; } return currentTask; } renderMenu( isExtended ) { return (