woocommerce/plugins/woocommerce-admin/client/marketing/overview-multichannel/Campaigns/Campaigns.tsx

170 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-12-15 17:13:32 +00:00
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
2022-12-18 12:37:25 +00:00
import { useState } from '@wordpress/element';
import {
Card,
CardHeader,
CardBody,
2022-12-18 12:37:25 +00:00
CardFooter,
Flex,
FlexItem,
FlexBlock,
} from '@wordpress/components';
2023-02-02 09:17:29 +00:00
import { Icon, megaphone, cancelCircleFilled } from '@wordpress/icons';
import { Pagination, Table, Link } from '@woocommerce/components';
2022-12-15 17:13:32 +00:00
/**
* Internal dependencies
*/
2022-12-17 13:08:14 +00:00
import { CardHeaderTitle, CenteredSpinner } from '~/marketing/components';
import { useCampaigns } from './useCampaigns';
2022-12-15 17:13:32 +00:00
import './Campaigns.scss';
export const Campaigns = () => {
2022-12-18 12:37:25 +00:00
const [ page, setPage ] = useState( 1 );
2022-12-17 13:08:14 +00:00
const { loading, data } = useCampaigns();
if ( loading ) {
return (
<Card>
<CardHeader>
<CardHeaderTitle>
{ __( 'Campaigns', 'woocommerce' ) }
</CardHeaderTitle>
</CardHeader>
<CardBody>
<CenteredSpinner />
</CardBody>
</Card>
);
}
if ( ! data ) {
2023-02-02 09:17:29 +00:00
return (
<Card className="woocommerce-marketing-campaigns-card">
<CardHeader>
<CardHeaderTitle>
{ __( 'Campaigns', 'woocommerce' ) }
</CardHeaderTitle>
</CardHeader>
<CardBody className="woocommerce-marketing-campaigns-card-body woocommerce-marketing-campaigns-card-body--error">
<Icon icon={ cancelCircleFilled } size={ 32 } />
<div className="woocommerce-marketing-campaigns-card-body__title">
{ __( 'An unexpected error occurred.', 'woocommerce' ) }
</div>
<div className="woocommerce-marketing-campaigns-card-body__description">
{ __(
'Please try again later. Check the logs if the problem persists. ',
'woocommerce'
) }
</div>
</CardBody>
</Card>
);
}
2022-12-17 13:08:14 +00:00
if ( data.length === 0 ) {
return (
<Card className="woocommerce-marketing-campaigns-card">
<CardHeader>
<CardHeaderTitle>
{ __( 'Campaigns', 'woocommerce' ) }
</CardHeaderTitle>
</CardHeader>
2023-02-02 09:17:29 +00:00
<CardBody className="woocommerce-marketing-campaigns-card-body woocommerce-marketing-campaigns-card-body--empty">
2022-12-17 13:08:14 +00:00
<Icon icon={ megaphone } size={ 32 } />
2023-02-02 09:17:29 +00:00
<div className="woocommerce-marketing-campaigns-card-body__title">
2022-12-17 13:08:14 +00:00
{ __(
'Advertise with marketing campaigns',
'woocommerce'
) }
</div>
2023-02-02 09:17:29 +00:00
<div className="woocommerce-marketing-campaigns-card-body__description">
2022-12-17 13:08:14 +00:00
{ __(
'Easily create and manage marketing campaigns without leaving WooCommerce.',
'woocommerce'
) }
</div>
</CardBody>
</Card>
);
}
2022-12-18 12:37:25 +00:00
const perPage = 5;
const total = data.length;
const start = ( page - 1 ) * perPage;
const pagedData = data.slice( start, start + perPage );
return (
<Card className="woocommerce-marketing-campaigns-card">
<CardHeader>
<CardHeaderTitle>
{ __( 'Campaigns', 'woocommerce' ) }
</CardHeaderTitle>
</CardHeader>
<Table
2022-12-18 12:37:25 +00:00
caption={ __( 'Campaigns', 'woocommerce' ) }
headers={ [
{
key: 'campaign',
label: __( 'Campaign', 'woocommerce' ),
},
{
key: 'cost',
label: __( 'Cost', 'woocommerce' ),
isNumeric: true,
},
] }
2022-12-18 12:37:25 +00:00
rows={ pagedData.map( ( el ) => {
return [
{
display: (
<Flex gap={ 4 }>
<FlexItem className="woocommerce-marketing-campaign-logo">
<img
src={ el.icon }
alt={ el.channelName }
width="16"
height="16"
/>
</FlexItem>
<FlexBlock>
<Flex direction="column" gap={ 1 }>
<FlexItem className="woocommerce-marketing-campaign-title">
<Link href={ el.manageUrl }>
{ el.title }
</Link>
</FlexItem>
{ el.description && (
<FlexItem className="woocommerce-marketing-campaign-description">
{ el.description }
</FlexItem>
) }
</Flex>
</FlexBlock>
</Flex>
),
},
{ display: el.cost },
];
} ) }
/>
{ total > perPage && (
<CardFooter className="woocommerce-marketing-campaigns-card-footer">
<Pagination
showPerPagePicker={ false }
perPage={ perPage }
page={ page }
total={ total }
onPageChange={ ( newPage: number ) => {
setPage( newPage );
} }
/>
</CardFooter>
) }
</Card>
);
2022-12-15 17:13:32 +00:00
};