-
Notifications
You must be signed in to change notification settings - Fork 67
/
index.js
157 lines (146 loc) · 3.65 KB
/
index.js
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
/**
* External dependencies
*/
import * as React from 'react';
import { __ } from '@wordpress/i18n';
import { Card, CardBody } from '@wordpress/components';
import { getQuery } from '@woocommerce/navigation';
import {
downloadCSVFile,
generateCSVDataFromTable,
generateCSVFileName,
} from '@woocommerce/csv-export';
import { TableCard } from '@woocommerce/components';
/**
* Internal dependencies
*/
import { useCardReaderStats } from 'wcpay/data';
import { TestModeNotice } from 'components/test-mode-notice';
import Page from 'components/page';
import DownloadButton from 'components/download-button';
import { formatExplicitCurrency, formatExportAmount } from 'utils/currency';
const PaymentCardReaderChargeDetails = ( props ) => {
const { readers, chargeError, isLoading } = useCardReaderStats(
props.chargeId,
props.transactionId
);
// Check instance of chargeError because its default value is empty object
if ( ! isLoading && chargeError instanceof Error ) {
return (
<Page maxWidth={ 1032 } className="wcpay-payment-details">
<TestModeNotice currentPage="payments" isDetailsView={ true } />
<Card>
<CardBody>
{ __(
'Readers details not loaded',
'woocommerce-payments'
) }
</CardBody>
</Card>
</Page>
);
}
return (
<RenderPaymentCardReaderChargeDetails
readers={ readers }
isLoading={ isLoading }
/>
);
};
const RenderPaymentCardReaderChargeDetails = ( props ) => {
const readers = props.readers;
const isLoading = props.isLoading;
const headers = [
{
key: 'reader_id',
label: __( 'Reader id', 'woocommerce-payments' ),
visible: true,
isLeftAligned: true,
},
{
key: 'status',
label: __( 'Status', 'woocommerce-payments' ),
visible: true,
isLeftAligned: true,
},
{
key: 'count',
label: __( 'Transactions', 'woocommerce-payments' ),
visible: true,
isLeftAligned: true,
},
{
key: 'fee',
label: __( 'Fee', 'woocommerce-payments' ),
visible: true,
isLeftAligned: true,
},
];
const rows =
readers.length > 0
? readers.map( ( reader ) => {
return [
{
value: reader.reader_id,
display: reader.reader_id,
},
{
value: reader.status,
display: reader.status,
},
{
value: reader.count,
display: reader.count,
},
{
value: reader.fee
? formatExportAmount(
reader.fee.amount,
reader.fee.currency
)
: 0,
display: reader.fee
? formatExplicitCurrency(
reader.fee.amount,
reader.fee.currency
)
: 0,
},
];
} )
: [];
const onDownload = () => {
// We destructure page and path to get the right params.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { page, path, ...params } = getQuery();
downloadCSVFile(
generateCSVFileName( 'Card Readers', params ),
generateCSVDataFromTable( headers, rows )
);
};
const downloadable = !! rows.length;
return (
<Page maxWidth={ 1032 } className="wcpay-payment-details">
<TestModeNotice currentPage="payments" isDetailsView={ true } />
<TableCard
className="transactions-list woocommerce-report-table has-search"
title={ __( 'Card readers', 'woocommerce-payments' ) }
isLoading={ isLoading }
rowsPerPage={ parseInt( getQuery().per_page ?? '', 10 ) || 25 }
totalRows={ rows.length }
headers={ headers }
rows={ rows }
actions={ [
downloadable && (
<DownloadButton
key="download"
isDisabled={ isLoading }
onClick={ onDownload }
/>
),
] }
/>
</Page>
);
};
export default PaymentCardReaderChargeDetails;