From 68a95f8a8379f23b39e097f67002757dc86e0071 Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Mon, 17 Jun 2019 16:38:42 +1200 Subject: [PATCH] Report Tables: extend columns --- .../components/report-table/index.js | 20 ++++++-- .../extensions/table-column/js/index.js | 47 +++++++++++++++++ .../woocommerce-admin-table-column.php | 50 +++++++++++++++++++ .../packages/components/src/rating/style.scss | 1 + 4 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 plugins/woocommerce-admin/docs/examples/extensions/table-column/js/index.js create mode 100644 plugins/woocommerce-admin/docs/examples/extensions/table-column/woocommerce-admin-table-column.php diff --git a/plugins/woocommerce-admin/client/analytics/components/report-table/index.js b/plugins/woocommerce-admin/client/analytics/components/report-table/index.js index cb611e85e1b..d898e52ea1e 100644 --- a/plugins/woocommerce-admin/client/analytics/components/report-table/index.js +++ b/plugins/woocommerce-admin/client/analytics/components/report-table/index.js @@ -106,13 +106,27 @@ class ReportTable extends Component { const totals = get( primaryData, [ 'data', 'totals' ], {} ); const totalResults = items.totalResults; const downloadable = 0 < totalResults; + + /** + * Filter report table. + * + * Enables manipulation of data used to create a report table. + * + * @param {object} reportTableData - data used to create the table. + * @param {string} reportTableData.endpoint - table api endpoint. + * @param {array} reportTableData.headers - table headers data. + * @param {array} reportTableData.rows - table rows data. + * @param {object} reportTableData.totals - total aggregates for request. + * @param {array} reportTableData.summary - summary numbers data. + * @param {array} reportTableData.items - response from api requerst. + */ const { headers, ids, rows, summary } = applyFilters( TABLE_FILTER, { - endpoint: endpoint, + endpoint, headers: getHeadersContent(), - ids: itemIdField ? items.data.map( item => item[ itemIdField ] ) : [], rows: getRowsContent( items.data ), - totals: totals, + totals, summary: getSummary ? getSummary( totals, totalResults ) : null, + items, } ); // Hide any headers based on user prefs, if loaded. diff --git a/plugins/woocommerce-admin/docs/examples/extensions/table-column/js/index.js b/plugins/woocommerce-admin/docs/examples/extensions/table-column/js/index.js new file mode 100644 index 00000000000..d5b2017dacb --- /dev/null +++ b/plugins/woocommerce-admin/docs/examples/extensions/table-column/js/index.js @@ -0,0 +1,47 @@ +/** @format */ +/** + * External dependencies + */ + +import { addFilter } from '@wordpress/hooks'; +import { Rating } from '@woocommerce/components'; + +addFilter( 'woocommerce_admin_report_table', 'plugin-domain', reportTableData => { + if ( 'products' !== reportTableData.endpoint || ! reportTableData.items.data.length ) { + return reportTableData; + } + + const newHeaders = [ + ...reportTableData.headers, + { + label: 'ID', + key: 'product_id', + }, + { + label: 'Rating', + key: 'product_rating', + }, + ]; + const newRows = reportTableData.rows.map( ( row, index ) => { + const product = reportTableData.items.data[ index ]; + const newRow = [ + ...row, + // product_id is already returned in the response for productData. + { + display: product.product_id, + value: product.product_id, + }, + // average_rating can be found on extended_info on productData. + { + display: , + value: product.extended_info.average_rating, + }, + ]; + return newRow; + } ); + + reportTableData.headers = newHeaders; + reportTableData.rows = newRows; + + return reportTableData; +} ); diff --git a/plugins/woocommerce-admin/docs/examples/extensions/table-column/woocommerce-admin-table-column.php b/plugins/woocommerce-admin/docs/examples/extensions/table-column/woocommerce-admin-table-column.php new file mode 100644 index 00000000000..8482845cf8c --- /dev/null +++ b/plugins/woocommerce-admin/docs/examples/extensions/table-column/woocommerce-admin-table-column.php @@ -0,0 +1,50 @@ +