woocommerce/plugins/woocommerce-admin/client/analytics/report/customers/table.js

209 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-12-06 22:21:46 +00:00
/** @format */
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import { Tooltip } from '@wordpress/components';
2018-12-06 22:21:46 +00:00
/**
* WooCommerce dependencies
*/
import { defaultTableDateFormat } from '@woocommerce/date';
2018-12-06 22:21:46 +00:00
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
import { Date, Link } from '@woocommerce/components';
2018-12-06 22:21:46 +00:00
/**
* Internal dependencies
*/
import ReportTable from 'analytics/components/report-table';
import { numberFormat } from 'lib/number';
export default class CustomersReportTable extends Component {
constructor() {
super();
this.getHeadersContent = this.getHeadersContent.bind( this );
this.getRowsContent = this.getRowsContent.bind( this );
}
getHeadersContent() {
return [
{
label: __( 'Name', 'wc-admin' ),
key: 'name',
required: true,
isLeftAligned: true,
isSortable: true,
},
{
label: __( 'Username', 'wc-admin' ),
key: 'username',
hiddenByDefault: true,
},
{
label: __( 'Last Active', 'wc-admin' ),
key: 'date_last_active',
defaultSort: true,
isSortable: true,
},
2018-12-06 22:21:46 +00:00
{
label: __( 'Sign Up', 'wc-admin' ),
key: 'date_registered',
2018-12-06 22:21:46 +00:00
isSortable: true,
},
{
label: __( 'Email', 'wc-admin' ),
key: 'email',
},
{
label: __( 'Orders', 'wc-admin' ),
key: 'orders_count',
isSortable: true,
isNumeric: true,
},
{
label: __( 'Total Spend', 'wc-admin' ),
2018-12-06 22:21:46 +00:00
key: 'total_spend',
isSortable: true,
isNumeric: true,
},
{
2018-12-10 15:23:02 +00:00
label: __( 'AOV', 'wc-admin' ),
screenReaderLabel: __( 'Average Order Value', 'wc-admin' ),
2018-12-12 14:46:40 +00:00
key: 'avg_order_value',
2018-12-06 22:21:46 +00:00
isNumeric: true,
},
{
label: __( 'Country', 'wc-admin' ),
key: 'country',
},
{
label: __( 'City', 'wc-admin' ),
key: 'city',
hiddenByDefault: true,
},
{
label: __( 'Postal Code', 'wc-admin' ),
key: 'postal_code',
hiddenByDefault: true,
},
];
}
getCountryName( code ) {
const countries = ( wcSettings.dataEndpoints && wcSettings.dataEndpoints.countries ) || [];
const country = countries.find( c => c.code === code );
return country ? country.name : null;
}
2018-12-06 22:21:46 +00:00
getRowsContent( customers ) {
return customers.map( customer => {
const {
2018-12-12 14:46:40 +00:00
avg_order_value,
2018-12-06 22:21:46 +00:00
date_last_active,
date_registered,
2018-12-06 22:21:46 +00:00
email,
name,
user_id,
2018-12-06 22:21:46 +00:00
orders_count,
username,
total_spend,
postcode,
city,
country,
2018-12-06 22:21:46 +00:00
} = customer;
const countryName = this.getCountryName( country );
2018-12-06 22:21:46 +00:00
const customerNameLink = user_id ? (
<Link href={ 'user-edit.php?user_id=' + user_id } type="wp-admin">
2018-12-06 22:21:46 +00:00
{ name }
</Link>
) : (
name
2018-12-06 22:21:46 +00:00
);
const dateRegistered = date_registered ? (
<Date date={ date_registered } visibleFormat={ defaultTableDateFormat } />
) : (
'—'
);
const countryDisplay = (
<Fragment>
<Tooltip text={ countryName }>
<span aria-hidden="true">{ country }</span>
</Tooltip>
<span className="screen-reader-text">{ countryName }</span>
</Fragment>
);
2018-12-06 22:21:46 +00:00
return [
{
display: customerNameLink,
value: name,
},
{
display: username,
value: username,
},
{
display: <Date date={ date_last_active } visibleFormat={ defaultTableDateFormat } />,
value: date_last_active,
},
2018-12-06 22:21:46 +00:00
{
display: dateRegistered,
value: date_registered,
2018-12-06 22:21:46 +00:00
},
{
display: <a href={ 'mailto:' + email }>{ email }</a>,
value: email,
},
{
display: numberFormat( orders_count ),
value: orders_count,
},
{
display: formatCurrency( total_spend ),
value: getCurrencyFormatDecimal( total_spend ),
},
{
2018-12-12 14:46:40 +00:00
display: formatCurrency( avg_order_value ),
value: getCurrencyFormatDecimal( avg_order_value ),
2018-12-06 22:21:46 +00:00
},
{
display: countryDisplay,
2018-12-06 22:21:46 +00:00
value: country,
},
{
display: city,
value: city,
},
{
display: postcode,
value: postcode,
2018-12-06 22:21:46 +00:00
},
];
} );
}
render() {
const { query } = this.props;
return (
<ReportTable
endpoint="customers"
getHeadersContent={ this.getHeadersContent }
getRowsContent={ this.getRowsContent }
itemIdField="id"
query={ query }
labels={ { placeholder: __( 'Search by customer name', 'wc-admin' ) } }
searchBy="customers"
searchParam="name_includes"
title={ __( 'Customers', 'wc-admin' ) }
columnPrefsKey="customers_report_columns"
2018-12-06 22:21:46 +00:00
/>
);
}
}