-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
Campaigns.tsx
195 lines (186 loc) · 4.76 KB
/
Campaigns.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* 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,
CreateNewCampaignModal,
} from '~/marketing/components';
import { useCampaigns } from '~/marketing/hooks';
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.
*/
export const Campaigns = () => {
const [ page, setPage ] = useState( 1 );
const [ isModalOpen, setModalOpen ] = useState( false );
const { loading, data, meta } = useCampaigns( page, perPage );
const total = meta?.total;
const getContent = () => {
if ( loading ) {
return (
<TablePlaceholder
caption={ tableCaption }
headers={ tableHeaders }
numberOfRows={ perPage }
/>
);
}
if ( ! data ) {
return (
<CardBody className="woocommerce-marketing-campaigns-card__content">
<Icon
className="woocommerce-marketing-campaigns-card__content-icon woocommerce-marketing-campaigns-card__content-icon--error"
icon={ cancelCircleFilled }
size={ 32 }
/>
<div className="woocommerce-marketing-campaigns-card__content-title">
{ __( 'An unexpected error occurred.', 'woocommerce' ) }
</div>
<div className="woocommerce-marketing-campaigns-card-body__content-description">
{ __(
'Please try again later. Check the logs if the problem persists. ',
'woocommerce'
) }
</div>
</CardBody>
);
}
if ( data.length === 0 ) {
return (
<CardBody className="woocommerce-marketing-campaigns-card__content">
<Icon
className="woocommerce-marketing-campaigns-card__content-icon woocommerce-marketing-campaigns-card__content-icon--empty"
icon={ megaphone }
size={ 32 }
/>
<div className="woocommerce-marketing-campaigns-card__content-title">
{ __(
'Advertise with marketing campaigns',
'woocommerce'
) }
</div>
<div className="woocommerce-marketing-campaigns-card__content-description">
{ __(
'Easily create and manage marketing campaigns without leaving WooCommerce.',
'woocommerce'
) }
</div>
</CardBody>
);
}
return (
<Table
caption={ tableCaption }
headers={ tableHeaders }
rows={ data.map( ( el ) => {
return [
{
display: (
<Flex gap={ 4 }>
<FlexItem className="woocommerce-marketing-campaigns-card__campaign-logo">
<img
src={ el.icon }
alt={ el.channelName }
width="16"
height="16"
/>
</FlexItem>
<FlexBlock>
<Flex direction="column" gap={ 1 }>
<FlexItem className="woocommerce-marketing-campaigns-card__campaign-title">
<Link href={ el.manageUrl }>
{ el.title }
</Link>
</FlexItem>
{ !! el.description && (
<FlexItem className="woocommerce-marketing-campaigns-card__campaign-description">
{ el.description }
</FlexItem>
) }
</Flex>
</FlexBlock>
</Flex>
),
},
{ display: el.cost },
];
} ) }
/>
);
};
return (
<Card className="woocommerce-marketing-campaigns-card">
<CardHeader>
<CardHeaderTitle>
{ __( 'Campaigns', 'woocommerce' ) }
</CardHeaderTitle>
<Button
variant="secondary"
onClick={ () => setModalOpen( true ) }
>
{ __( 'Create new campaign', 'woocommerce' ) }
</Button>
{ !! isModalOpen && (
<CreateNewCampaignModal
onRequestClose={ () => setModalOpen( false ) }
/>
) }
</CardHeader>
{ getContent() }
{ !! ( total && 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>
);
};