2018-12-19 11:18:43 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, _n } from '@wordpress/i18n';
|
|
|
|
import { Component } from '@wordpress/element';
|
2020-03-25 03:20:17 +00:00
|
|
|
import { withSelect } from '@wordpress/data';
|
2018-12-19 11:18:43 +00:00
|
|
|
import { map } from 'lodash';
|
2019-01-07 11:54:42 +00:00
|
|
|
import moment from 'moment';
|
2019-01-14 09:54:44 +00:00
|
|
|
import { Date, Link } from '@woocommerce/components';
|
2018-12-19 11:18:43 +00:00
|
|
|
import { getNewPath, getPersistedQuery } from '@woocommerce/navigation';
|
2020-04-02 21:54:38 +00:00
|
|
|
import { formatValue } from '@woocommerce/number';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
2020-03-25 03:20:17 +00:00
|
|
|
import { SETTINGS_STORE_NAME } from '@woocommerce/data';
|
2020-08-17 21:53:13 +00:00
|
|
|
import { getCurrentDates, defaultTableDateFormat } from '@woocommerce/date';
|
2018-12-19 11:18:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import ReportTable from '../../components/report-table';
|
|
|
|
import { CurrencyContext } from '../../../lib/currency-context';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2018-12-19 11:18:43 +00:00
|
|
|
|
2020-12-14 05:08:19 +00:00
|
|
|
class DownloadsReportTable extends Component {
|
2018-12-19 11:18:43 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.getHeadersContent = this.getHeadersContent.bind( this );
|
|
|
|
this.getRowsContent = this.getRowsContent.bind( this );
|
|
|
|
this.getSummary = this.getSummary.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
getHeadersContent() {
|
|
|
|
return [
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Date', 'woocommerce-admin' ),
|
2018-12-19 11:18:43 +00:00
|
|
|
key: 'date',
|
|
|
|
defaultSort: true,
|
|
|
|
required: true,
|
|
|
|
isLeftAligned: true,
|
|
|
|
isSortable: true,
|
|
|
|
},
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'Product title', 'woocommerce-admin' ),
|
2019-01-10 17:10:31 +00:00
|
|
|
key: 'product',
|
|
|
|
isSortable: true,
|
2018-12-19 11:18:43 +00:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
2021-08-13 18:54:24 +00:00
|
|
|
label: __( 'File name', 'woocommerce-admin' ),
|
2018-12-19 11:18:43 +00:00
|
|
|
key: 'file_name',
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Order #', 'woocommerce-admin' ),
|
|
|
|
screenReaderLabel: __( 'Order Number', 'woocommerce-admin' ),
|
2019-02-28 14:14:21 +00:00
|
|
|
key: 'order_number',
|
2018-12-19 11:18:43 +00:00
|
|
|
},
|
|
|
|
{
|
2020-03-10 08:53:01 +00:00
|
|
|
label: __( 'Username', 'woocommerce-admin' ),
|
2018-12-19 11:18:43 +00:00
|
|
|
key: 'user_id',
|
|
|
|
},
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'IP', 'woocommerce-admin' ),
|
2018-12-19 11:18:43 +00:00
|
|
|
key: 'ip_address',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowsContent( downloads ) {
|
|
|
|
const { query } = this.props;
|
|
|
|
const persistedQuery = getPersistedQuery( query );
|
2022-01-06 12:53:30 +00:00
|
|
|
const dateFormat = getAdminSetting(
|
|
|
|
'dateFormat',
|
|
|
|
defaultTableDateFormat
|
|
|
|
);
|
2018-12-19 11:18:43 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return map( downloads, ( download ) => {
|
2019-02-19 19:35:29 +00:00
|
|
|
const {
|
|
|
|
_embedded,
|
|
|
|
date,
|
2020-02-14 02:23:21 +00:00
|
|
|
file_name: fileName,
|
|
|
|
file_path: filePath,
|
|
|
|
ip_address: ipAddress,
|
|
|
|
order_id: orderId,
|
|
|
|
order_number: orderNumber,
|
|
|
|
product_id: productId,
|
2019-02-19 19:35:29 +00:00
|
|
|
username,
|
|
|
|
} = download;
|
2018-12-19 11:18:43 +00:00
|
|
|
|
2020-07-28 02:32:58 +00:00
|
|
|
const {
|
|
|
|
code: errorCode,
|
|
|
|
name: productName,
|
|
|
|
} = _embedded.product[ 0 ];
|
2020-06-16 13:47:27 +00:00
|
|
|
let productDisplay, productValue;
|
|
|
|
|
|
|
|
// Handle deleted products.
|
|
|
|
if ( errorCode === 'woocommerce_rest_product_invalid_id' ) {
|
|
|
|
productDisplay = __( '(Deleted)', 'woocommerce-admin' );
|
|
|
|
productValue = __( '(Deleted)', 'woocommerce-admin' );
|
|
|
|
} else {
|
|
|
|
const productURL = getNewPath(
|
|
|
|
persistedQuery,
|
|
|
|
'/analytics/products',
|
|
|
|
{
|
|
|
|
filter: 'single_product',
|
|
|
|
products: productId,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
productDisplay = (
|
|
|
|
<Link href={ productURL } type="wc-admin">
|
|
|
|
{ productName }
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
productValue = productName;
|
|
|
|
}
|
2018-12-19 11:18:43 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
display: (
|
2020-03-10 08:53:01 +00:00
|
|
|
<Date date={ date } visibleFormat={ dateFormat } />
|
2020-02-14 02:23:21 +00:00
|
|
|
),
|
2018-12-19 11:18:43 +00:00
|
|
|
value: date,
|
|
|
|
},
|
|
|
|
{
|
2020-06-16 13:47:27 +00:00
|
|
|
display: productDisplay,
|
|
|
|
value: productValue,
|
2018-12-19 11:18:43 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-08 16:51:19 +00:00
|
|
|
display: (
|
2020-02-14 02:23:21 +00:00
|
|
|
<Link href={ filePath } type="external">
|
|
|
|
{ fileName }
|
2019-01-08 16:51:19 +00:00
|
|
|
</Link>
|
|
|
|
),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: fileName,
|
2018-12-19 11:18:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
display: (
|
2019-11-22 17:07:26 +00:00
|
|
|
<Link
|
2020-02-14 02:23:21 +00:00
|
|
|
href={ getAdminLink(
|
|
|
|
`post.php?post=${ orderId }&action=edit`
|
|
|
|
) }
|
2019-11-22 17:07:26 +00:00
|
|
|
type="wp-admin"
|
|
|
|
>
|
2020-02-14 02:23:21 +00:00
|
|
|
{ orderNumber }
|
2018-12-19 11:18:43 +00:00
|
|
|
</Link>
|
|
|
|
),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: orderNumber,
|
2018-12-19 11:18:43 +00:00
|
|
|
},
|
|
|
|
{
|
2019-02-19 19:35:29 +00:00
|
|
|
display: username,
|
|
|
|
value: username,
|
2018-12-19 11:18:43 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
display: ipAddress,
|
|
|
|
value: ipAddress,
|
2018-12-19 11:18:43 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
getSummary( totals ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const { download_count: downloadCount = 0 } = totals;
|
2020-03-25 03:20:17 +00:00
|
|
|
const { query, defaultDateRange } = this.props;
|
|
|
|
const dates = getCurrentDates( query, defaultDateRange );
|
2019-01-07 11:54:42 +00:00
|
|
|
const after = moment( dates.primary.after );
|
|
|
|
const before = moment( dates.primary.before );
|
|
|
|
const days = before.diff( after, 'days' ) + 1;
|
2020-06-17 23:33:40 +00:00
|
|
|
const currency = this.context.getCurrencyConfig();
|
2019-01-07 11:54:42 +00:00
|
|
|
|
2018-12-19 11:18:43 +00:00
|
|
|
return [
|
|
|
|
{
|
2019-03-13 17:14:02 +00:00
|
|
|
label: _n( 'day', 'days', days, 'woocommerce-admin' ),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', days ),
|
2018-12-19 11:18:43 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
label: _n(
|
2021-08-13 18:54:24 +00:00
|
|
|
'Download',
|
|
|
|
'Downloads',
|
2020-02-14 02:23:21 +00:00
|
|
|
downloadCount,
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatValue( currency, 'number', downloadCount ),
|
2018-12-19 11:18:43 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-03-21 03:25:05 +00:00
|
|
|
const { query, filters, advancedFilters } = this.props;
|
2018-12-19 11:18:43 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<ReportTable
|
|
|
|
endpoint="downloads"
|
|
|
|
getHeadersContent={ this.getHeadersContent }
|
|
|
|
getRowsContent={ this.getRowsContent }
|
|
|
|
getSummary={ this.getSummary }
|
2020-03-31 15:08:40 +00:00
|
|
|
summaryFields={ [ 'download_count' ] }
|
2018-12-19 11:18:43 +00:00
|
|
|
query={ query }
|
2019-01-07 11:54:42 +00:00
|
|
|
tableQuery={ {
|
|
|
|
_embed: true,
|
|
|
|
} }
|
2019-03-13 17:14:02 +00:00
|
|
|
title={ __( 'Downloads', 'woocommerce-admin' ) }
|
2018-12-19 11:18:43 +00:00
|
|
|
columnPrefsKey="downloads_report_columns"
|
2019-03-21 03:25:05 +00:00
|
|
|
filters={ filters }
|
|
|
|
advancedFilters={ advancedFilters }
|
2018-12-19 11:18:43 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-03-25 03:20:17 +00:00
|
|
|
|
2020-12-14 05:08:19 +00:00
|
|
|
DownloadsReportTable.contextType = CurrencyContext;
|
2020-04-02 21:54:38 +00:00
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
export default withSelect( ( select ) => {
|
|
|
|
const { woocommerce_default_date_range: defaultDateRange } = select(
|
|
|
|
SETTINGS_STORE_NAME
|
|
|
|
).getSetting( 'wc_admin', 'wcAdminSettings' );
|
|
|
|
return { defaultDateRange };
|
2020-12-14 05:08:19 +00:00
|
|
|
} )( DownloadsReportTable );
|