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

261 lines
5.8 KiB
JavaScript
Raw Normal View History

2018-12-06 22:21:46 +00:00
/** @format */
/**
* External dependencies
*/
import { __, _n } 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 'lib/date';
import { formatCurrency, getCurrencyFormatDecimal } from 'lib/currency-format';
import { Date, Link } from '@woocommerce/components';
import { formatValue } from 'lib/number-format';
import { getAdminLink, getSetting } from '@woocommerce/wc-admin-settings';
const { countries } = getSetting( 'dataEndpoints', { countries: {} } );
2018-12-06 22:21:46 +00:00
/**
* Internal dependencies
*/
import ReportTable from 'analytics/components/report-table';
export default class CustomersReportTable extends Component {
constructor() {
super();
this.getHeadersContent = this.getHeadersContent.bind( this );
this.getRowsContent = this.getRowsContent.bind( this );
this.getSummary = this.getSummary.bind( this );
2018-12-06 22:21:46 +00:00
}
getHeadersContent() {
return [
{
label: __( 'Name', 'woocommerce-admin' ),
2018-12-06 22:21:46 +00:00
key: 'name',
required: true,
isLeftAligned: true,
isSortable: true,
},
{
label: __( 'Username', 'woocommerce-admin' ),
2018-12-06 22:21:46 +00:00
key: 'username',
hiddenByDefault: true,
},
{
label: __( 'Last Active', 'woocommerce-admin' ),
key: 'date_last_active',
defaultSort: true,
isSortable: true,
},
2018-12-06 22:21:46 +00:00
{
label: __( 'Sign Up', 'woocommerce-admin' ),
key: 'date_registered',
2018-12-06 22:21:46 +00:00
isSortable: true,
},
{
label: __( 'Email', 'woocommerce-admin' ),
2018-12-06 22:21:46 +00:00
key: 'email',
},
{
label: __( 'Orders', 'woocommerce-admin' ),
2018-12-06 22:21:46 +00:00
key: 'orders_count',
isSortable: true,
isNumeric: true,
},
{
label: __( 'Total Spend', 'woocommerce-admin' ),
2018-12-06 22:21:46 +00:00
key: 'total_spend',
isSortable: true,
isNumeric: true,
},
{
label: __( 'AOV', 'woocommerce-admin' ),
screenReaderLabel: __( 'Average Order Value', 'woocommerce-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', 'woocommerce-admin' ),
2018-12-06 22:21:46 +00:00
key: 'country',
isSortable: true,
2018-12-06 22:21:46 +00:00
},
{
label: __( 'City', 'woocommerce-admin' ),
2018-12-06 22:21:46 +00:00
key: 'city',
hiddenByDefault: true,
isSortable: true,
},
{
2019-06-28 16:41:22 +00:00
label: __( 'Region', 'woocommerce-admin' ),
key: 'state',
hiddenByDefault: true,
isSortable: true,
2018-12-06 22:21:46 +00:00
},
{
label: __( 'Postal Code', 'woocommerce-admin' ),
key: 'postcode',
2018-12-06 22:21:46 +00:00
hiddenByDefault: true,
isSortable: true,
2018-12-06 22:21:46 +00:00
},
];
}
getCountryName( code ) {
return typeof countries[ code ] !== 'undefined' ? countries[ code ] : 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,
state,
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={ getAdminLink( '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 dateLastActive = date_last_active ? (
<Date date={ date_last_active } visibleFormat={ defaultTableDateFormat } />
) : (
'—'
);
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: dateLastActive,
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: formatValue( 'number', orders_count ),
2018-12-06 22:21:46 +00:00
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: state,
value: state,
},
2018-12-06 22:21:46 +00:00
{
display: postcode,
value: postcode,
2018-12-06 22:21:46 +00:00
},
];
} );
}
getSummary( totals ) {
const {
customers_count = 0,
avg_orders_count = 0,
avg_total_spend = 0,
avg_avg_order_value = 0,
} = totals;
return [
{
label: _n( 'customer', 'customers', customers_count, 'woocommerce-admin' ),
value: formatValue( 'number', customers_count ),
},
{
label: _n( 'average order', 'average orders', avg_orders_count, 'woocommerce-admin' ),
value: formatValue( 'number', avg_orders_count ),
},
{
label: __( 'average lifetime spend', 'woocommerce-admin' ),
value: formatCurrency( avg_total_spend ),
},
{
label: __( 'average order value', 'woocommerce-admin' ),
value: formatCurrency( avg_avg_order_value ),
},
];
}
2018-12-06 22:21:46 +00:00
render() {
2019-03-21 03:25:05 +00:00
const { isRequesting, query, filters, advancedFilters } = this.props;
2018-12-06 22:21:46 +00:00
return (
<ReportTable
endpoint="customers"
getHeadersContent={ this.getHeadersContent }
getRowsContent={ this.getRowsContent }
getSummary={ this.getSummary }
isRequesting={ isRequesting }
2018-12-06 22:21:46 +00:00
itemIdField="id"
query={ query }
labels={ { placeholder: __( 'Search by customer name', 'woocommerce-admin' ) } }
searchBy="customers"
title={ __( 'Customers', 'woocommerce-admin' ) }
columnPrefsKey="customers_report_columns"
2019-03-21 03:25:05 +00:00
filters={ filters }
advancedFilters={ advancedFilters }
2018-12-06 22:21:46 +00:00
/>
);
}
}