2019-03-28 06:09:44 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-01-27 18:40:02 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-04-16 13:29:54 +00:00
|
|
|
import { MenuGroup, MenuItem } from '@wordpress/components';
|
|
|
|
import { check } from '@wordpress/icons';
|
2021-04-27 15:23:34 +00:00
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2020-06-10 23:49:27 +00:00
|
|
|
import {
|
|
|
|
ONBOARDING_STORE_NAME,
|
2020-12-03 21:16:04 +00:00
|
|
|
OPTIONS_STORE_NAME,
|
|
|
|
PLUGINS_STORE_NAME,
|
2020-08-03 08:22:25 +00:00
|
|
|
SETTINGS_STORE_NAME,
|
2020-06-10 23:49:27 +00:00
|
|
|
} from '@woocommerce/data';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2019-03-28 06:09:44 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-14 14:35:55 +00:00
|
|
|
* Internal dependencies
|
2019-03-28 06:09:44 +00:00
|
|
|
*/
|
|
|
|
import './style.scss';
|
2020-08-13 02:05:22 +00:00
|
|
|
import CartModal from '../dashboard/components/cart-modal';
|
2020-12-03 21:16:04 +00:00
|
|
|
import { getAllTasks } from './tasks';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { getCountryCode } from '../dashboard/utils';
|
2020-12-03 21:16:04 +00:00
|
|
|
import TaskList from './list';
|
2021-04-16 13:29:54 +00:00
|
|
|
import { DisplayOption } from '../header/activity-panel/display-options';
|
2021-04-27 15:23:34 +00:00
|
|
|
import { TaskStep } from './task-step';
|
2019-09-23 21:47:08 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const taskDashboardSelect = ( select ) => {
|
|
|
|
const { getProfileItems, getTasksStatus } = select( ONBOARDING_STORE_NAME );
|
|
|
|
const { getSettings } = select( SETTINGS_STORE_NAME );
|
|
|
|
const { getOption } = select( OPTIONS_STORE_NAME );
|
|
|
|
const {
|
|
|
|
getActivePlugins,
|
|
|
|
getInstalledPlugins,
|
|
|
|
isJetpackConnected,
|
|
|
|
} = select( PLUGINS_STORE_NAME );
|
|
|
|
const profileItems = getProfileItems();
|
2021-04-16 13:29:54 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const trackedCompletedTasks =
|
|
|
|
getOption( 'woocommerce_task_list_tracked_completed_tasks' ) || [];
|
|
|
|
|
|
|
|
const { general: generalSettings = {} } = getSettings( 'general' );
|
|
|
|
const countryCode = getCountryCode(
|
|
|
|
generalSettings.woocommerce_default_country
|
|
|
|
);
|
|
|
|
|
|
|
|
const activePlugins = getActivePlugins();
|
|
|
|
const installedPlugins = getInstalledPlugins();
|
|
|
|
const onboardingStatus = getTasksStatus();
|
|
|
|
|
|
|
|
return {
|
|
|
|
activePlugins,
|
|
|
|
countryCode,
|
|
|
|
dismissedTasks: getOption( 'woocommerce_task_list_dismissed_tasks' ),
|
|
|
|
isExtendedTaskListComplete:
|
|
|
|
getOption( 'woocommerce_extended_task_list_complete' ) === 'yes',
|
|
|
|
isExtendedTaskListHidden:
|
|
|
|
getOption( 'woocommerce_extended_task_list_hidden' ) === 'yes',
|
|
|
|
isJetpackConnected: isJetpackConnected(),
|
|
|
|
isSetupTaskListHidden:
|
|
|
|
getOption( 'woocommerce_task_list_hidden' ) === 'yes',
|
|
|
|
isTaskListComplete:
|
|
|
|
getOption( 'woocommerce_task_list_complete' ) === 'yes',
|
|
|
|
installedPlugins,
|
|
|
|
onboardingStatus,
|
|
|
|
profileItems,
|
|
|
|
trackedCompletedTasks,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const TaskDashboard = ( { userPreferences, query } ) => {
|
|
|
|
const { createNotice } = useDispatch( 'core/notices' );
|
|
|
|
const { updateOptions } = useDispatch( OPTIONS_STORE_NAME );
|
|
|
|
const { installAndActivatePlugins } = useDispatch( PLUGINS_STORE_NAME );
|
|
|
|
const {
|
|
|
|
trackedCompletedTasks,
|
|
|
|
activePlugins,
|
|
|
|
countryCode,
|
|
|
|
installedPlugins,
|
|
|
|
isJetpackConnected,
|
|
|
|
onboardingStatus,
|
|
|
|
profileItems,
|
|
|
|
isSetupTaskListHidden,
|
|
|
|
dismissedTasks,
|
|
|
|
isTaskListComplete,
|
|
|
|
isExtendedTaskListHidden,
|
|
|
|
isExtendedTaskListComplete,
|
|
|
|
} = useSelect( taskDashboardSelect );
|
|
|
|
|
|
|
|
const [ isCartModalOpen, setIsCartModalOpen ] = useState( false );
|
|
|
|
|
|
|
|
useEffect( () => {
|
2019-08-21 05:58:47 +00:00
|
|
|
document.body.classList.add( 'woocommerce-onboarding' );
|
2019-07-18 10:11:21 +00:00
|
|
|
document.body.classList.add( 'woocommerce-task-dashboard__body' );
|
2021-04-27 15:23:34 +00:00
|
|
|
}, [] );
|
2019-07-18 10:11:21 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const getTaskStartedCount = ( taskName ) => {
|
2020-12-18 13:17:07 +00:00
|
|
|
const trackedStartedTasks =
|
|
|
|
userPreferences.task_list_tracked_started_tasks;
|
|
|
|
if ( ! trackedStartedTasks || ! trackedStartedTasks[ taskName ] ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return trackedStartedTasks[ taskName ];
|
|
|
|
};
|
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const updateTrackStartedCount = ( taskName, newCount ) => {
|
2020-12-18 13:17:07 +00:00
|
|
|
const trackedStartedTasks =
|
|
|
|
userPreferences.task_list_tracked_started_tasks || {};
|
|
|
|
userPreferences.updateUserPreferences( {
|
|
|
|
task_list_tracked_started_tasks: {
|
|
|
|
...( trackedStartedTasks || {} ),
|
|
|
|
[ taskName ]: newCount,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const isTaskCompleted = ( taskName ) => {
|
2021-01-07 13:27:12 +00:00
|
|
|
if ( ! trackedCompletedTasks ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return trackedCompletedTasks.includes( taskName );
|
|
|
|
};
|
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const onTaskSelect = ( taskName ) => {
|
|
|
|
const trackStartedCount = getTaskStartedCount( taskName );
|
2020-12-18 13:17:07 +00:00
|
|
|
recordEvent( 'tasklist_click', {
|
|
|
|
task_name: taskName,
|
|
|
|
} );
|
2021-04-27 15:23:34 +00:00
|
|
|
if ( ! isTaskCompleted( taskName ) ) {
|
|
|
|
updateTrackStartedCount( taskName, trackStartedCount + 1 );
|
2021-01-07 13:27:12 +00:00
|
|
|
}
|
2020-12-18 13:17:07 +00:00
|
|
|
};
|
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const toggleExtensionTaskList = () => {
|
2021-04-16 13:29:54 +00:00
|
|
|
const newValue = ! isExtendedTaskListHidden;
|
|
|
|
|
|
|
|
recordEvent(
|
|
|
|
newValue ? 'extended_tasklist_hide' : 'extended_tasklist_show'
|
|
|
|
);
|
|
|
|
updateOptions( {
|
|
|
|
woocommerce_extended_task_list_hidden: newValue ? 'yes' : 'no',
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const toggleCartModal = () => {
|
2020-01-02 02:36:25 +00:00
|
|
|
if ( ! isCartModalOpen ) {
|
|
|
|
recordEvent( 'tasklist_purchase_extensions' );
|
|
|
|
}
|
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
setIsCartModalOpen( ! isCartModalOpen );
|
|
|
|
};
|
2019-12-31 08:50:45 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const getCurrentTask = ( tasks ) => {
|
2021-01-14 16:59:31 +00:00
|
|
|
const { task } = query;
|
2021-04-27 15:23:34 +00:00
|
|
|
const currentTask = tasks.find( ( s ) => s.key === task );
|
2019-07-19 02:54:38 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
if ( ! currentTask ) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-04-16 13:29:54 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
return currentTask;
|
|
|
|
};
|
2021-04-16 13:29:54 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const allTasks = getAllTasks( {
|
|
|
|
activePlugins,
|
|
|
|
countryCode,
|
|
|
|
createNotice,
|
|
|
|
installAndActivatePlugins,
|
|
|
|
installedPlugins,
|
|
|
|
isJetpackConnected,
|
|
|
|
onboardingStatus,
|
|
|
|
profileItems,
|
|
|
|
query,
|
|
|
|
toggleCartModal,
|
|
|
|
onTaskSelect,
|
|
|
|
} );
|
2019-08-01 18:09:08 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const { extension, setup: setupTasks } = allTasks;
|
|
|
|
const { task } = query;
|
2020-12-18 13:17:07 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const extensionTasks =
|
|
|
|
Array.isArray( extension ) &&
|
|
|
|
extension.sort( ( a, b ) => {
|
|
|
|
if ( Boolean( a.completed ) === Boolean( b.completed ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-12-18 13:17:07 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
return a.completed ? 1 : -1;
|
|
|
|
} );
|
2020-08-03 08:22:25 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
const currentTask = getCurrentTask( [
|
|
|
|
...( extensionTasks || [] ),
|
|
|
|
...( setupTasks || [] ),
|
|
|
|
] );
|
|
|
|
|
|
|
|
if ( task && ! currentTask ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ( currentTask ) {
|
|
|
|
return (
|
|
|
|
<TaskStep taskContainer={ currentTask.container } query={ query } />
|
|
|
|
);
|
|
|
|
}
|
2019-10-11 12:55:35 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ setupTasks && ( ! isSetupTaskListHidden || task ) && (
|
|
|
|
<TaskList
|
|
|
|
dismissedTasks={ dismissedTasks || [] }
|
|
|
|
isComplete={ isTaskListComplete }
|
|
|
|
query={ query }
|
|
|
|
tasks={ setupTasks }
|
|
|
|
title={ __(
|
|
|
|
'Get ready to start selling',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
trackedCompletedTasks={ trackedCompletedTasks || [] }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
{ extensionTasks && (
|
|
|
|
<DisplayOption>
|
|
|
|
<MenuGroup
|
|
|
|
className="woocommerce-layout__homescreen-display-options"
|
|
|
|
label={ __( 'Display', 'woocommerce-admin' ) }
|
|
|
|
>
|
|
|
|
<MenuItem
|
|
|
|
className="woocommerce-layout__homescreen-extension-tasklist-toggle"
|
|
|
|
icon={ ! isExtendedTaskListHidden && check }
|
|
|
|
isSelected={ ! isExtendedTaskListHidden }
|
|
|
|
role="menuitemcheckbox"
|
|
|
|
onClick={ toggleExtensionTaskList }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Show things to do next',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</MenuItem>
|
|
|
|
</MenuGroup>
|
|
|
|
</DisplayOption>
|
|
|
|
) }
|
|
|
|
{ extensionTasks && ! isExtendedTaskListHidden && (
|
|
|
|
<TaskList
|
|
|
|
dismissedTasks={ dismissedTasks || [] }
|
|
|
|
isComplete={ isExtendedTaskListComplete }
|
|
|
|
name={ 'extended_task_list' }
|
|
|
|
query={ query }
|
|
|
|
tasks={ extensionTasks }
|
|
|
|
title={ __( 'Things to do next', 'woocommerce-admin' ) }
|
|
|
|
trackedCompletedTasks={ trackedCompletedTasks || [] }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
{ isCartModalOpen && (
|
|
|
|
<CartModal
|
|
|
|
onClose={ () => toggleCartModal() }
|
|
|
|
onClickPurchaseLater={ () => toggleCartModal() }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2020-07-14 01:40:56 +00:00
|
|
|
|
2021-04-27 15:23:34 +00:00
|
|
|
export default TaskDashboard;
|