-
Notifications
You must be signed in to change notification settings - Fork 69
/
index.js
174 lines (160 loc) · 4.64 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/** @format **/
/**
* External dependencies
*/
import { useMemo } from '@wordpress/element';
import { dateI18n } from '@wordpress/date';
import { __ } from '@wordpress/i18n';
import moment from 'moment';
import { TableCard, Link } from '@woocommerce/components';
import { onQueryChange, getQuery } from '@woocommerce/navigation';
/**
* Internal dependencies.
*/
import { useDeposits, useDepositsSummary } from 'data';
import { displayType, displayStatus } from '../strings';
import { formatStringValue } from 'util';
import { formatCurrency } from 'utils/currency';
import DetailsLink, { getDetailsURL } from 'components/details-link';
import ClickableCell from 'components/clickable-cell';
import Page from '../../components/page';
import DepositsFilters from '../filters';
const getColumns = ( sortByDate ) => [
{
key: 'details',
label: '',
required: true,
cellClassName: 'info-button ' + ( sortByDate ? 'is-sorted' : '' ),
},
{
key: 'date',
label: __( 'Date', 'woocommerce-payments' ),
screenReaderLabel: __( 'Date', 'woocommerce-payments' ),
required: true,
isLeftAligned: true,
defaultOrder: 'desc',
cellClassName: 'date-time',
isSortable: true,
defaultSort: true,
},
{
key: 'type',
label: __( 'Type', 'woocommerce-payments' ),
screenReaderLabel: __( 'Type', 'woocommerce-payments' ),
required: true,
},
{
key: 'amount',
label: __( 'Amount', 'woocommerce-payments' ),
screenReaderLabel: __( 'Amount', 'woocommerce-payments' ),
isNumeric: true,
required: true,
isSortable: true,
},
{
key: 'status',
label: __( 'Status', 'woocommerce-payments' ),
screenReaderLabel: __( 'Status', 'woocommerce-payments' ),
required: true,
},
// TODO { key: 'transactions', label: __( 'Transactions', 'woocommerce-payments' ), isNumeric: true },
{
key: 'bankAccount',
label: __( 'Bank account', 'woocommerce-payments' ),
screenReaderLabel: __( 'Bank account', 'woocommerce-payments' ),
},
];
export const DepositsList = () => {
const { deposits, depositsCount, isLoading } = useDeposits( getQuery() );
const { depositsSummary, isLoading: isSummaryLoading } = useDepositsSummary(
getQuery()
);
const columnsArgs = [
! getQuery().orderby || 'date' === getQuery().orderby,
];
const columns = useMemo( () => getColumns( ...columnsArgs ), columnsArgs );
const rows = deposits.map( ( deposit ) => {
const clickable = ( children ) => (
<ClickableCell href={ getDetailsURL( deposit.id, 'deposits' ) }>
{ children }
</ClickableCell>
);
const detailsLink = (
<DetailsLink id={ deposit.id } parentSegment="deposits" />
);
const dateDisplay = (
<Link href={ getDetailsURL( deposit.id, 'deposits' ) }>
{ dateI18n(
'M j, Y',
moment.utc( deposit.date ).toISOString(),
true // TODO Change call to gmdateI18n and remove this deprecated param once WP 5.4 support ends.
) }
</Link>
);
// Map deposit to table row.
const data = {
details: { value: deposit.id, display: detailsLink },
date: { value: deposit.date, display: dateDisplay },
type: {
value: deposit.type,
display: clickable( displayType[ deposit.type ] ),
},
amount: {
value: deposit.amount / 100,
display: clickable(
formatCurrency( deposit.amount, deposit.currency )
),
},
status: {
value: deposit.status,
display: clickable(
displayStatus[ deposit.status ] ||
formatStringValue( deposit.status )
),
},
bankAccount: {
value: deposit.bankAccount,
display: clickable( deposit.bankAccount ),
},
};
return columns.map( ( { key } ) => data[ key ] || { display: null } );
} );
const summary = [
{ label: 'deposits', value: `${ depositsSummary.count }` },
];
const isCurrencyFiltered = 'string' === typeof getQuery().store_currency_is;
if ( ! isSummaryLoading ) {
const isSingleCurrency =
2 > ( depositsSummary.store_currencies || [] ).length;
if ( isSingleCurrency || isCurrencyFiltered ) {
summary.push( {
label: 'total',
value: `${ formatCurrency(
depositsSummary.total,
depositsSummary.currency
) }`,
} );
}
}
const storeCurrencies =
depositsSummary.store_currencies ||
( isCurrencyFiltered ? [ getQuery().store_currency_is ] : [] );
return (
<Page>
<DepositsFilters storeCurrencies={ storeCurrencies } />
<TableCard
className="deposits-list woocommerce-report-table"
title={ __( 'Deposit history', 'woocommerce-payments' ) }
isLoading={ isLoading }
rowsPerPage={ getQuery().per_page || 25 }
totalRows={ depositsCount }
headers={ columns }
rows={ rows }
summary={ summary }
query={ getQuery() }
onQueryChange={ onQueryChange }
/>
</Page>
);
};
export default DepositsList;