/
index.tsx
109 lines (105 loc) · 2.86 KB
/
index.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
import { dispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { TableCard } from '@woocommerce/components';
import { Hooks } from 'wp-js-hooks';
import { OrderSection, storeName } from '../../../store';
import { transformOrdersToRows } from './rows';
import { calculateSummary } from './summary';
import { Upgrade } from './upgrade';
const headers = [
{
key: 'created_at',
isSortable: true,
label: __('Date', 'mailpoet'),
},
{
key: 'id',
label: __('Order #', 'mailpoet'),
},
{
key: 'last_name',
isSortable: true,
label: __('Customer', 'mailpoet'),
},
{
key: 'items',
label: __('Product(s)', 'mailpoet'),
},
{
key: 'subject',
isSortable: true,
label: __('Email clicked', 'mailpoet'),
},
{
key: 'status',
isSortable: true,
label: __('Status', 'mailpoet'),
},
{
key: 'revenue',
isSortable: true,
label: __('Revenue', 'mailpoet'),
},
];
export function Orders(): JSX.Element {
const { ordersSection } = useSelect((s) => ({
ordersSection: s(storeName).getSection('orders') as OrderSection,
}));
const orders = ordersSection?.data?.items;
const rows = transformOrdersToRows(ordersSection?.data);
const summary = calculateSummary(orders ?? []);
const beforeTable = Hooks.applyFilters(
'mailpoet_analytics_orders_before_table',
<Upgrade />,
) as null | JSX.Element;
return (
<div className="mailpoet-analytics-orders">
{beforeTable}
<TableCard
title=""
onQueryChange={(type: string) => (param: unknown) => {
let customQuery = {};
if (type === 'paged') {
customQuery = { page: param };
} else if (type === 'per_page') {
customQuery = {
page: 1,
limit: param,
};
} else if (type === 'sort') {
customQuery = {
page: 1,
order_by: param,
order:
ordersSection.customQuery.order_by === param &&
ordersSection.customQuery.order === 'asc'
? 'desc'
: 'asc',
};
}
void dispatch(storeName).updateSection({
...ordersSection,
customQuery: {
...ordersSection.customQuery,
...customQuery,
},
});
}}
query={{
paged: ordersSection.customQuery.page,
orderby: ordersSection.customQuery.order_by,
order: ordersSection.customQuery.order,
}}
rows={rows}
headers={headers}
showMenu={false}
rowsPerPage={ordersSection.customQuery.limit}
totalRows={
ordersSection.data !== undefined ? ordersSection.data.results : 0
}
summary={summary}
isLoading={orders === undefined}
/>
</div>
);
}