/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { useState } from '@wordpress/element'; import { Button, Card, CardHeader, CardBody, CardFooter, Flex, FlexItem, FlexBlock, } from '@wordpress/components'; import { Icon, megaphone, cancelCircleFilled } from '@wordpress/icons'; import { Pagination, Table, TablePlaceholder, Link, } from '@woocommerce/components'; /** * Internal dependencies */ import { CardHeaderTitle } from '~/marketing/components'; import { useNewCampaignTypes } from '~/marketing/hooks'; import { useCampaigns } from './useCampaigns'; import { CreateNewCampaignModal } from './CreateNewCampaignModal'; import './Campaigns.scss'; const tableCaption = __( 'Campaigns', 'woocommerce' ); const tableHeaders = [ { key: 'campaign', label: __( 'Campaign', 'woocommerce' ), }, { key: 'cost', label: __( 'Cost', 'woocommerce' ), isNumeric: true, }, ]; const perPage = 5; /** * Card displaying campaigns in a table. * * Pagination will be rendered in the card footer if the total number of campaigns is more than one page. * * If there are no campaigns, there will be no table but an info message instead. * * If there is an error, there will be no table but an error message instead. * * The new campaign types data will also be loaded, * so that when users click on the "Create new campaign" button in the card header, * there will be no loading necessary in the modal. */ export const Campaigns = () => { const [ page, setPage ] = useState( 1 ); const [ open, setOpen ] = useState( false ); const { loading: loadingCampaigns, data, meta, } = useCampaigns( page, perPage ); const { loading: loadingNewCampaignTypes } = useNewCampaignTypes(); const total = meta?.total; const getContent = () => { if ( loadingNewCampaignTypes || loadingCampaigns ) { return ( ); } if ( ! data ) { return (
{ __( 'An unexpected error occurred.', 'woocommerce' ) }
{ __( 'Please try again later. Check the logs if the problem persists. ', 'woocommerce' ) }
); } if ( data.length === 0 ) { return (
{ __( 'Advertise with marketing campaigns', 'woocommerce' ) }
{ __( 'Easily create and manage marketing campaigns without leaving WooCommerce.', 'woocommerce' ) }
); } return ( { return [ { display: ( { { el.title } { el.description && ( { el.description } ) } ), }, { display: el.cost }, ]; } ) } /> ); }; return ( { __( 'Campaigns', 'woocommerce' ) } { open && ( setOpen( false ) } /> ) } { getContent() } { total && total > perPage && ( { setPage( newPage ); } } /> ) } ); };