Merge pull request woocommerce/woocommerce-admin#960 from woocommerce/add/variations-table-data

Variations: Add table data
This commit is contained in:
Paul Sealock 2018-12-03 12:26:37 +13:00 committed by GitHub
commit 86c6602d56
3 changed files with 45 additions and 11 deletions

View File

@ -116,7 +116,8 @@ ReportTable.defaultProps = {
export default compose( export default compose(
withSelect( ( select, props ) => { withSelect( ( select, props ) => {
const { endpoint, getSummary, query, tableQuery } = props; const { endpoint, getSummary, query, tableQuery } = props;
const primaryData = getSummary ? getReportChartData( endpoint, 'primary', query, select ) : {}; const chartEndpoint = 'variations' === endpoint ? 'products' : endpoint;
const primaryData = getSummary ? getReportChartData( chartEndpoint, 'primary', query, select ) : {};
const tableData = getReportTableData( endpoint, query, select, tableQuery ); const tableData = getReportTableData( endpoint, query, select, tableQuery );
return { return {

View File

@ -2,9 +2,9 @@
/** /**
* External dependencies * External dependencies
*/ */
import { __ } from '@wordpress/i18n'; import { __, _n } from '@wordpress/i18n';
import { Component } from '@wordpress/element'; import { Component } from '@wordpress/element';
import { map } from 'lodash'; import { map, get } from 'lodash';
/** /**
* WooCommerce dependencies * WooCommerce dependencies
@ -17,6 +17,7 @@ import { getNewPath, getPersistedQuery } from '@woocommerce/navigation';
* Internal dependencies * Internal dependencies
*/ */
import ReportTable from 'analytics/components/report-table'; import ReportTable from 'analytics/components/report-table';
import { numberFormat } from 'lib/number';
export default class VariationsReportTable extends Component { export default class VariationsReportTable extends Component {
constructor() { constructor() {
@ -26,11 +27,11 @@ export default class VariationsReportTable extends Component {
this.getRowsContent = this.getRowsContent.bind( this ); this.getRowsContent = this.getRowsContent.bind( this );
} }
getVariationName( variation ) { getVariationName( row ) {
return variation.attributes.reduce( ( desc, attribute, index, arr ) => { const extendedInfo = get( row, 'extended_info', {} );
desc += `${ attribute.option }${ arr.length === index + 1 ? '' : ', ' }`; const attributes = get( extendedInfo, 'attributes', {} );
return desc;
}, variation.product_name + ' / ' ); return extendedInfo.name + ' / ' + attributes.map( a => a.option ).join( ', ' );
} }
getHeadersContent() { getHeadersContent() {
@ -94,10 +95,15 @@ export default class VariationsReportTable extends Component {
filter: 'advanced', filter: 'advanced',
product_includes: query.products, product_includes: query.products,
} ); } );
const editPostLink = `post.php?post=${ product_id }&action=edit`;
return [ return [
{ {
display: name, display: (
<Link href={ editPostLink } type="wp-admin">
{ name }
</Link>
),
value: name, value: name,
}, },
{ {
@ -118,7 +124,7 @@ export default class VariationsReportTable extends Component {
}, },
{ {
display: ( display: (
<Link href={ 'post.php?action=edit&post=' + product_id } type="wp-admin"> <Link href={ editPostLink } type="wp-admin">
{ stockStatuses[ stock_status ] } { stockStatuses[ stock_status ] }
</Link> </Link>
), ),
@ -132,6 +138,31 @@ export default class VariationsReportTable extends Component {
} ); } );
} }
getSummary( totals ) {
if ( ! totals ) {
return [];
}
return [
{
// @TODO: When primaryData is segmented, fix this to reflect variations, not products.
label: _n( 'variation sold', 'variations sold', totals.products_count, 'wc-admin' ),
value: numberFormat( totals.products_count ),
},
{
label: _n( 'item sold', 'items sold', totals.items_sold, 'wc-admin' ),
value: numberFormat( totals.items_sold ),
},
{
label: __( 'gross revenue', 'wc-admin' ),
value: formatCurrency( totals.gross_revenue ),
},
{
label: _n( 'orders', 'orders', totals.orders_count, 'wc-admin' ),
value: numberFormat( totals.orders_count ),
},
];
}
render() { render() {
const { query } = this.props; const { query } = this.props;
@ -150,9 +181,11 @@ export default class VariationsReportTable extends Component {
itemIdField="product_id" itemIdField="product_id"
labels={ labels } labels={ labels }
query={ query } query={ query }
getSummary={ this.getSummary }
tableQuery={ { tableQuery={ {
orderby: query.orderby || 'items_sold', orderby: query.orderby || 'items_sold',
order: query.order || 'desc', order: query.order || 'desc',
extended_info: true,
} } } }
title={ __( 'Variations', 'wc-admin' ) } title={ __( 'Variations', 'wc-admin' ) }
/> />

View File

@ -20,7 +20,7 @@ export default {
async getReportItems( ...args ) { async getReportItems( ...args ) {
const [ endpoint, query ] = args.slice( -2 ); const [ endpoint, query ] = args.slice( -2 );
const swaggerEndpoints = [ 'categories', 'coupons', 'taxes', 'variations' ]; const swaggerEndpoints = [ 'categories', 'coupons', 'taxes' ];
if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) { if ( swaggerEndpoints.indexOf( endpoint ) >= 0 ) {
try { try {
const response = await fetch( const response = await fetch(