2021-09-21 19:33:44 +00:00
/ * *
* External dependencies
* /
import { __ } from '@wordpress/i18n' ;
import { Card , CardHeader , Spinner } from '@wordpress/components' ;
2022-04-25 06:16:39 +00:00
import {
ONBOARDING_STORE_NAME ,
PLUGINS_STORE_NAME ,
Extension ,
ExtensionList ,
} from '@woocommerce/data' ;
2021-09-21 19:33:44 +00:00
import { recordEvent } from '@woocommerce/tracks' ;
import { Text } from '@woocommerce/experimental' ;
import { useMemo , useState } from '@wordpress/element' ;
import { useSelect , useDispatch } from '@wordpress/data' ;
import { registerPlugin } from '@wordpress/plugins' ;
import { WooOnboardingTask } from '@woocommerce/onboarding' ;
/ * *
* Internal dependencies
* /
import './Marketing.scss' ;
import { createNoticesFromResponse } from '~/lib/notices' ;
import { PluginList , PluginListProps } from './PluginList' ;
import { PluginProps } from './Plugin' ;
2022-01-24 16:23:12 +00:00
import { getPluginSlug } from '../../../utils' ;
2021-09-21 19:33:44 +00:00
2021-10-29 12:47:05 +00:00
const ALLOWED_PLUGIN_LISTS = [ 'task-list/reach' , 'task-list/grow' ] ;
2021-09-21 19:33:44 +00:00
export const transformExtensionToPlugin = (
extension : Extension ,
activePlugins : string [ ] ,
installedPlugins : string [ ]
) : PluginProps = > {
2022-06-21 08:37:34 +00:00
const { description , image_url , is_built_by_wc , key , manage_url , name } =
extension ;
2022-01-24 16:23:12 +00:00
const slug = getPluginSlug ( key ) ;
2021-09-21 19:33:44 +00:00
return {
description ,
slug ,
imageUrl : image_url ,
isActive : activePlugins.includes ( slug ) ,
isInstalled : installedPlugins.includes ( slug ) ,
2021-12-10 13:01:13 +00:00
isBuiltByWC : is_built_by_wc ,
2021-09-21 19:33:44 +00:00
manageUrl : manage_url ,
name ,
} ;
} ;
export const getMarketingExtensionLists = (
freeExtensions : ExtensionList [ ] ,
activePlugins : string [ ] ,
installedPlugins : string [ ]
) : [ PluginProps [ ] , PluginListProps [ ] ] = > {
const installed : PluginProps [ ] = [ ] ;
const lists : PluginListProps [ ] = [ ] ;
freeExtensions . forEach ( ( list ) = > {
if ( ! ALLOWED_PLUGIN_LISTS . includes ( list . key ) ) {
return ;
}
const listPlugins : PluginProps [ ] = [ ] ;
list . plugins . forEach ( ( extension ) = > {
const plugin = transformExtensionToPlugin (
extension ,
activePlugins ,
installedPlugins
) ;
if ( plugin . isInstalled ) {
installed . push ( plugin ) ;
return ;
}
listPlugins . push ( plugin ) ;
} ) ;
if ( ! listPlugins . length ) {
return ;
}
const transformedList : PluginListProps = {
. . . list ,
plugins : listPlugins ,
} ;
lists . push ( transformedList ) ;
} ) ;
return [ installed , lists ] ;
} ;
export type MarketingProps = {
2022-04-21 04:44:19 +00:00
onComplete : ( bool ? : boolean ) = > void ;
2021-09-21 19:33:44 +00:00
} ;
2021-10-01 19:53:22 +00:00
const Marketing : React.FC < MarketingProps > = ( { onComplete } ) = > {
2021-09-21 19:33:44 +00:00
const [ currentPlugin , setCurrentPlugin ] = useState < string | null > (
null
) ;
2021-10-05 17:07:50 +00:00
const { actionTask } = useDispatch ( ONBOARDING_STORE_NAME ) ;
2021-09-21 19:33:44 +00:00
const { installAndActivatePlugins } = useDispatch ( PLUGINS_STORE_NAME ) ;
2022-06-21 08:37:34 +00:00
const { activePlugins , freeExtensions , installedPlugins , isResolving } =
useSelect ( ( select ) = > {
const { getActivePlugins , getInstalledPlugins } =
select ( PLUGINS_STORE_NAME ) ;
const { getFreeExtensions , hasFinishedResolution } = select (
ONBOARDING_STORE_NAME
) ;
2021-09-21 19:33:44 +00:00
2022-06-21 08:37:34 +00:00
return {
activePlugins : getActivePlugins ( ) ,
freeExtensions : getFreeExtensions ( ) ,
installedPlugins : getInstalledPlugins ( ) ,
isResolving : ! hasFinishedResolution ( 'getFreeExtensions' ) ,
} ;
} ) ;
2021-09-21 19:33:44 +00:00
const [ installedExtensions , pluginLists ] = useMemo (
( ) = >
getMarketingExtensionLists (
freeExtensions ,
activePlugins ,
installedPlugins
) ,
[ installedPlugins , activePlugins , freeExtensions ]
) ;
const installAndActivate = ( slug : string ) = > {
setCurrentPlugin ( slug ) ;
2021-10-05 17:07:50 +00:00
actionTask ( 'marketing' ) ;
2021-09-21 19:33:44 +00:00
installAndActivatePlugins ( [ slug ] )
2022-04-25 06:16:39 +00:00
. then ( ( response ) = > {
2021-09-21 19:33:44 +00:00
recordEvent ( 'tasklist_marketing_install' , {
selected_extension : slug ,
installed_extensions : installedExtensions.map (
( extension ) = > extension . slug
) ,
} ) ;
createNoticesFromResponse ( response ) ;
setCurrentPlugin ( null ) ;
2021-10-05 17:07:50 +00:00
onComplete ( ) ;
2021-09-21 19:33:44 +00:00
} )
. catch ( ( response : { errors : Record < string , string > } ) = > {
createNoticesFromResponse ( response ) ;
setCurrentPlugin ( null ) ;
} ) ;
} ;
2022-07-01 21:28:45 +00:00
const onManage = ( ) = > {
actionTask ( 'marketing' ) ;
} ;
2021-09-21 19:33:44 +00:00
if ( isResolving ) {
return < Spinner / > ;
}
return (
< div className = "woocommerce-task-marketing" >
{ ! ! installedExtensions . length && (
< Card className = "woocommerce-task-card" >
< CardHeader >
< Text
variant = "title.small"
as = "h2"
className = "woocommerce-task-card__title"
>
{ __ (
'Installed marketing extensions' ,
2022-03-30 09:00:04 +00:00
'woocommerce'
2021-09-21 19:33:44 +00:00
) }
< / Text >
< / CardHeader >
< PluginList
currentPlugin = { currentPlugin }
2021-10-04 17:11:00 +00:00
installAndActivate = { installAndActivate }
2022-07-01 21:28:45 +00:00
onManage = { onManage }
2021-09-21 19:33:44 +00:00
plugins = { installedExtensions }
/ >
< / Card >
) }
{ ! ! pluginLists . length && (
< Card className = "woocommerce-task-card" >
< CardHeader >
< Text
variant = "title.small"
as = "h2"
className = "woocommerce-task-card__title"
>
{ __ (
'Recommended marketing extensions' ,
2022-03-30 09:00:04 +00:00
'woocommerce'
2021-09-21 19:33:44 +00:00
) }
< / Text >
< Text as = "span" >
{ __ (
'We recommend adding one of the following marketing tools for your store. The extension will be installed and activated for you when you click "Get started".' ,
2022-03-30 09:00:04 +00:00
'woocommerce'
2021-09-21 19:33:44 +00:00
) }
< / Text >
< / CardHeader >
{ pluginLists . map ( ( list ) = > {
const { key , title , plugins } = list ;
return (
< PluginList
currentPlugin = { currentPlugin }
installAndActivate = { installAndActivate }
2022-07-01 21:28:45 +00:00
onManage = { onManage }
2021-09-21 19:33:44 +00:00
key = { key }
plugins = { plugins }
title = { title }
/ >
) ;
} ) }
< / Card >
) }
< / div >
) ;
} ;
registerPlugin ( 'wc-admin-onboarding-task-marketing' , {
2022-04-21 04:44:19 +00:00
// @ts-expect-error @types/wordpress__plugins need to be updated
2021-09-21 19:33:44 +00:00
scope : 'woocommerce-tasks' ,
render : ( ) = > (
< WooOnboardingTask id = "marketing" >
2022-04-21 04:44:19 +00:00
{ ( { onComplete } : MarketingProps ) = > {
2021-10-01 19:53:22 +00:00
return < Marketing onComplete = { onComplete } / > ;
} }
2021-09-21 19:33:44 +00:00
< / WooOnboardingTask >
) ,
} ) ;