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

307 lines
6.6 KiB
JavaScript
Raw Normal View History

2018-12-06 22:21:46 +00:00
/**
* External dependencies
*/
import { __, _n } from '@wordpress/i18n';
import { Fragment, useContext } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { Tooltip } from '@wordpress/components';
import { Date, Link, Pill } from '@woocommerce/components';
import { formatValue } from '@woocommerce/number';
import { getAdminLink } from '@woocommerce/settings';
import { defaultTableDateFormat } from '@woocommerce/date';
import { COUNTRIES_STORE_NAME } from '@woocommerce/data';
import { CurrencyContext } from '@woocommerce/currency';
2018-12-06 22:21:46 +00:00
/**
* Internal dependencies
*/
import ReportTable from '../../components/report-table';
import { getAdminSetting } from '~/utils/admin-settings';
2018-12-06 22:21:46 +00:00
function CustomersReportTable( {
isRequesting,
query,
filters,
advancedFilters,
} ) {
const context = useContext( CurrencyContext );
const { countries, loadingCountries } = useSelect( ( select ) => {
const { getCountries, hasFinishedResolution } =
select( COUNTRIES_STORE_NAME );
return {
countries: getCountries(),
loadingCountries: ! hasFinishedResolution( 'getCountries' ),
};
} );
2018-12-06 22:21:46 +00:00
const getHeadersContent = () => {
2018-12-06 22:21:46 +00:00
return [
{
label: __( 'Name', 'woocommerce' ),
2018-12-06 22:21:46 +00:00
key: 'name',
required: true,
isLeftAligned: true,
isSortable: true,
},
{
label: __( 'Username', 'woocommerce' ),
2018-12-06 22:21:46 +00:00
key: 'username',
hiddenByDefault: true,
},
{
label: __( 'Last active', 'woocommerce' ),
key: 'date_last_active',
defaultSort: true,
isSortable: true,
},
2018-12-06 22:21:46 +00:00
{
label: __( 'Date registered', 'woocommerce' ),
key: 'date_registered',
2018-12-06 22:21:46 +00:00
isSortable: true,
},
{
label: __( 'Email', 'woocommerce' ),
2018-12-06 22:21:46 +00:00
key: 'email',
},
{
label: __( 'Orders', 'woocommerce' ),
2018-12-06 22:21:46 +00:00
key: 'orders_count',
isSortable: true,
isNumeric: true,
},
{
label: __( 'Total spend', 'woocommerce' ),
2018-12-06 22:21:46 +00:00
key: 'total_spend',
isSortable: true,
isNumeric: true,
},
{
label: __( 'AOV', 'woocommerce' ),
screenReaderLabel: __( 'Average order value', 'woocommerce' ),
2018-12-12 14:46:40 +00:00
key: 'avg_order_value',
2018-12-06 22:21:46 +00:00
isNumeric: true,
},
{
label: __( 'Country / Region', 'woocommerce' ),
2018-12-06 22:21:46 +00:00
key: 'country',
isSortable: true,
2018-12-06 22:21:46 +00:00
},
{
label: __( 'City', 'woocommerce' ),
2018-12-06 22:21:46 +00:00
key: 'city',
hiddenByDefault: true,
isSortable: true,
},
{
label: __( 'Region', 'woocommerce' ),
key: 'state',
hiddenByDefault: true,
isSortable: true,
2018-12-06 22:21:46 +00:00
},
{
label: __( 'Postal code', 'woocommerce' ),
key: 'postcode',
2018-12-06 22:21:46 +00:00
hiddenByDefault: true,
isSortable: true,
2018-12-06 22:21:46 +00:00
},
];
};
2018-12-06 22:21:46 +00:00
const getCountryName = ( code ) => {
return typeof countries[ code ] !== 'undefined'
? countries[ code ]
: null;
};
const getRowsContent = ( customers ) => {
const dateFormat = getAdminSetting(
'dateFormat',
defaultTableDateFormat
);
const {
formatAmount,
formatDecimal: getCurrencyFormatDecimal,
getCurrencyConfig,
} = context;
return customers?.map( ( customer ) => {
2018-12-06 22:21:46 +00:00
const {
avg_order_value: avgOrderValue,
date_last_active: dateLastActive,
date_registered: dateRegistered,
2018-12-06 22:21:46 +00:00
email,
name,
user_id: userId,
orders_count: ordersCount,
2018-12-06 22:21:46 +00:00
username,
total_spend: totalSpend,
postcode,
city,
state,
country,
2018-12-06 22:21:46 +00:00
} = customer;
const countryName = getCountryName( country );
const customerName =
name?.trim() !== '' ? (
name
) : (
<Pill>{ __( 'Guest', 'woocommerce' ) }</Pill>
);
2018-12-06 22:21:46 +00:00
const customerNameLink = userId ? (
<Link
href={ getAdminLink( 'user-edit.php?user_id=' + userId ) }
type="wp-admin"
>
2018-12-06 22:21:46 +00:00
{ name }
</Link>
) : (
customerName
2018-12-06 22:21:46 +00:00
);
const dateLastActiveDisplay = dateLastActive ? (
<Date date={ dateLastActive } visibleFormat={ dateFormat } />
) : (
'—'
);
const dateRegisteredDisplay = dateRegistered ? (
<Date date={ dateRegistered } visibleFormat={ dateFormat } />
) : (
'—'
);
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: dateLastActiveDisplay,
value: dateLastActive,
},
2018-12-06 22:21:46 +00:00
{
display: dateRegisteredDisplay,
value: dateRegistered,
2018-12-06 22:21:46 +00:00
},
{
display: <a href={ 'mailto:' + email }>{ email }</a>,
value: email,
},
{
display: formatValue(
getCurrencyConfig(),
'number',
ordersCount
),
value: ordersCount,
2018-12-06 22:21:46 +00:00
},
{
display: formatAmount( totalSpend ),
value: getCurrencyFormatDecimal( totalSpend ),
2018-12-06 22:21:46 +00:00
},
{
display: formatAmount( avgOrderValue ),
value: getCurrencyFormatDecimal( avgOrderValue ),
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
},
];
} );
};
2018-12-06 22:21:46 +00:00
const getSummary = ( totals ) => {
const {
customers_count: customersCount = 0,
avg_orders_count: avgOrdersCount = 0,
avg_total_spend: avgTotalSpend = 0,
avg_avg_order_value: avgAvgOrderValue = 0,
} = totals;
const { formatAmount, getCurrencyConfig } = context;
const currency = getCurrencyConfig();
return [
{
label: _n(
'customer',
'customers',
customersCount,
'woocommerce'
),
value: formatValue( currency, 'number', customersCount ),
},
{
label: _n(
'Average order',
'Average orders',
avgOrdersCount,
'woocommerce'
),
value: formatValue( currency, 'number', avgOrdersCount ),
},
{
label: __( 'Average lifetime spend', 'woocommerce' ),
value: formatAmount( avgTotalSpend ),
},
{
label: __( 'Average order value', 'woocommerce' ),
value: formatAmount( avgAvgOrderValue ),
},
];
};
return (
<ReportTable
endpoint="customers"
getHeadersContent={ getHeadersContent }
getRowsContent={ getRowsContent }
getSummary={ getSummary }
summaryFields={ [
'customers_count',
'avg_orders_count',
'avg_total_spend',
'avg_avg_order_value',
] }
isRequesting={ isRequesting || loadingCountries }
itemIdField="id"
query={ query }
labels={ {
placeholder: __( 'Search by customer name', 'woocommerce' ),
} }
searchBy="customers"
title={ __( 'Customers', 'woocommerce' ) }
columnPrefsKey="customers_report_columns"
filters={ filters }
advancedFilters={ advancedFilters }
/>
);
}
export default CustomersReportTable;