2022-12-21 14:56:22 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2023-03-09 14:23:27 +00:00
|
|
|
import { useState } from '@wordpress/element';
|
2022-12-21 14:56:22 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Modal,
|
|
|
|
Icon,
|
|
|
|
Flex,
|
|
|
|
FlexBlock,
|
|
|
|
FlexItem,
|
|
|
|
} from '@wordpress/components';
|
|
|
|
import { chevronUp, chevronDown, external } from '@wordpress/icons';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-03-02 18:34:16 +00:00
|
|
|
import {
|
|
|
|
useRecommendedChannels,
|
2023-03-09 13:52:11 +00:00
|
|
|
useCampaignTypes,
|
2023-03-02 18:34:16 +00:00
|
|
|
useRegisteredChannels,
|
2023-03-22 10:35:06 +00:00
|
|
|
useInstalledPluginsWithoutChannels,
|
2023-03-02 18:34:16 +00:00
|
|
|
} from '~/marketing/hooks';
|
2023-02-27 15:49:50 +00:00
|
|
|
import { SmartPluginCardBody } from '~/marketing/components';
|
2022-12-21 14:56:22 +00:00
|
|
|
import './CreateNewCampaignModal.scss';
|
|
|
|
|
|
|
|
const isExternalURL = ( url: string ) =>
|
|
|
|
new URL( url ).origin !== location.origin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Props for CreateNewCampaignModal, which is based on Modal.Props.
|
|
|
|
*
|
|
|
|
* Modal's title and children props are omitted because they are specified within the component
|
|
|
|
* and not needed to be specified by the consumer.
|
|
|
|
*/
|
|
|
|
type CreateCampaignModalProps = Omit< Modal.Props, 'title' | 'children' >;
|
|
|
|
|
|
|
|
export const CreateNewCampaignModal = ( props: CreateCampaignModalProps ) => {
|
|
|
|
const { className, ...restProps } = props;
|
|
|
|
const [ collapsed, setCollapsed ] = useState( true );
|
2023-03-09 14:51:12 +00:00
|
|
|
const { data: campaignTypes, refetch: refetchCampaignTypes } =
|
|
|
|
useCampaignTypes();
|
|
|
|
const { refetch: refetchRegisteredChannels } = useRegisteredChannels();
|
2022-12-21 14:56:22 +00:00
|
|
|
const { data: recommendedChannels } = useRecommendedChannels();
|
2023-03-22 10:35:06 +00:00
|
|
|
const { loadInstalledPluginsAfterActivation } =
|
|
|
|
useInstalledPluginsWithoutChannels();
|
2023-03-09 14:51:12 +00:00
|
|
|
|
2023-03-17 16:47:54 +00:00
|
|
|
const onInstalledAndActivated = ( pluginSlug: string ) => {
|
2023-03-09 14:51:12 +00:00
|
|
|
refetchCampaignTypes();
|
|
|
|
refetchRegisteredChannels();
|
2023-03-17 16:47:54 +00:00
|
|
|
loadInstalledPluginsAfterActivation( pluginSlug );
|
2023-03-09 14:51:12 +00:00
|
|
|
};
|
2022-12-21 14:56:22 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
2023-03-09 14:17:03 +00:00
|
|
|
{ ...restProps }
|
2022-12-21 14:56:22 +00:00
|
|
|
className={ classnames(
|
|
|
|
className,
|
|
|
|
'woocommerce-marketing-create-campaign-modal'
|
|
|
|
) }
|
|
|
|
title={ __( 'Create a new campaign', 'woocommerce' ) }
|
|
|
|
>
|
|
|
|
<div className="woocommerce-marketing-new-campaigns">
|
|
|
|
<div className="woocommerce-marketing-new-campaigns__question-label">
|
2023-03-09 18:13:05 +00:00
|
|
|
{ !! campaignTypes?.length
|
|
|
|
? __(
|
|
|
|
'Where would you like to promote your products?',
|
|
|
|
'woocommerce'
|
|
|
|
)
|
|
|
|
: __( 'No campaign types found.', 'woocommerce' ) }
|
2022-12-21 14:56:22 +00:00
|
|
|
</div>
|
2023-03-09 18:14:35 +00:00
|
|
|
{ campaignTypes?.map( ( el ) => (
|
|
|
|
<Flex
|
|
|
|
key={ el.id }
|
|
|
|
className="woocommerce-marketing-new-campaign-type"
|
|
|
|
gap={ 4 }
|
|
|
|
>
|
|
|
|
<FlexItem>
|
|
|
|
<img
|
|
|
|
src={ el.icon }
|
|
|
|
alt={ el.name }
|
|
|
|
width="32"
|
|
|
|
height="32"
|
|
|
|
/>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexBlock>
|
|
|
|
<Flex direction="column" gap={ 1 }>
|
|
|
|
<FlexItem className="woocommerce-marketing-new-campaign-type__name">
|
|
|
|
{ el.name }
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem className="woocommerce-marketing-new-campaign-type__description">
|
|
|
|
{ el.description }
|
|
|
|
</FlexItem>
|
|
|
|
</Flex>
|
|
|
|
</FlexBlock>
|
|
|
|
<FlexItem>
|
|
|
|
<Button
|
|
|
|
variant="secondary"
|
|
|
|
href={ el.createUrl }
|
|
|
|
target={
|
|
|
|
isExternalURL( el.createUrl )
|
|
|
|
? '_blank'
|
|
|
|
: '_self'
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Flex gap={ 1 }>
|
|
|
|
<FlexItem>
|
|
|
|
{ __( 'Create', 'woocommerce' ) }
|
2023-03-09 14:26:06 +00:00
|
|
|
</FlexItem>
|
2023-03-15 00:24:00 +00:00
|
|
|
{ !! isExternalURL( el.createUrl ) && (
|
2023-03-09 14:26:06 +00:00
|
|
|
<FlexItem>
|
2023-03-09 18:14:35 +00:00
|
|
|
<Icon
|
|
|
|
icon={ external }
|
|
|
|
size={ 16 }
|
|
|
|
/>
|
2022-12-21 14:56:22 +00:00
|
|
|
</FlexItem>
|
2023-03-09 18:14:35 +00:00
|
|
|
) }
|
|
|
|
</Flex>
|
|
|
|
</Button>
|
|
|
|
</FlexItem>
|
|
|
|
</Flex>
|
|
|
|
) ) }
|
2022-12-21 14:56:22 +00:00
|
|
|
</div>
|
2023-03-07 18:57:02 +00:00
|
|
|
{ !! recommendedChannels?.length && (
|
2022-12-21 14:56:22 +00:00
|
|
|
<div className="woocommerce-marketing-add-channels">
|
|
|
|
<Flex direction="column">
|
|
|
|
<FlexItem>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
onClick={ () => setCollapsed( ! collapsed ) }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Add channels for other campaign types',
|
|
|
|
'woocommerce'
|
|
|
|
) }
|
|
|
|
<Icon
|
|
|
|
icon={ collapsed ? chevronDown : chevronUp }
|
|
|
|
size={ 24 }
|
|
|
|
/>
|
|
|
|
</Button>
|
|
|
|
</FlexItem>
|
|
|
|
{ ! collapsed && (
|
|
|
|
<FlexItem>
|
2023-03-09 14:23:27 +00:00
|
|
|
{ recommendedChannels.map( ( el ) => (
|
|
|
|
<SmartPluginCardBody
|
|
|
|
key={ el.plugin }
|
|
|
|
plugin={ el }
|
2023-03-17 16:47:54 +00:00
|
|
|
onInstalledAndActivated={
|
|
|
|
onInstalledAndActivated
|
|
|
|
}
|
2023-03-09 14:23:27 +00:00
|
|
|
/>
|
|
|
|
) ) }
|
2022-12-21 14:56:22 +00:00
|
|
|
</FlexItem>
|
|
|
|
) }
|
|
|
|
</Flex>
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|