woocommerce/plugins/woocommerce-admin/client/analytics/report/orders/index.js

125 lines
3.4 KiB
JavaScript
Raw Normal View History

/** @format */
/**
* External dependencies
*/
2018-10-10 20:29:12 +00:00
import { __ } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import { compose } from '@wordpress/compose';
2018-10-11 17:07:59 +00:00
import PropTypes from 'prop-types';
import { withSelect } from '@wordpress/data';
import { get } from 'lodash';
/**
* Internal dependencies
*/
import { EmptyContent, ReportFilters } from '@woocommerce/components';
import { filters, advancedFilterConfig } from './config';
import { getAdminLink } from 'lib/nav-utils';
import { appendTimestamp, getCurrentDates } from 'lib/date';
import { getReportChartData } from 'store/reports/utils';
import OrdersReportChart from './chart';
import OrdersReportTable from './table';
class OrdersReport extends Component {
constructor( props ) {
super( props );
2018-10-11 16:00:06 +00:00
}
render() {
2018-10-11 19:27:29 +00:00
const {
isTableDataError,
isTableDataRequesting,
2018-10-11 19:27:29 +00:00
orders,
path,
query,
primaryData,
secondaryData,
} = this.props;
if ( primaryData.isError || isTableDataError ) {
2018-10-11 19:27:29 +00:00
let title, actionLabel, actionURL, actionCallback;
if ( primaryData.isError || secondaryData.isError || isTableDataError ) {
2018-10-11 19:27:29 +00:00
title = __( 'There was an error getting your stats. Please try again.', 'wc-admin' );
actionLabel = __( 'Reload', 'wc-admin' );
actionCallback = () => {
// TODO Add tracking for how often an error is displayed, and the reload action is clicked.
window.location.reload();
};
} else {
title = __( 'No results could be found for this date range.', 'wc-admin' );
actionLabel = __( 'View Orders', 'wc-admin' );
actionURL = getAdminLink( 'edit.php?post_type=shop_order' );
}
return (
<Fragment>
<ReportFilters query={ query } path={ path } />
<EmptyContent
title={ title }
actionLabel={ actionLabel }
actionURL={ actionURL }
actionCallback={ actionCallback }
/>
</Fragment>
);
}
return (
<Fragment>
<ReportFilters
query={ query }
path={ path }
filters={ filters }
advancedConfig={ advancedFilterConfig }
/>
<OrdersReportChart query={ query } />
<OrdersReportTable
isRequesting={ isTableDataRequesting }
orders={ orders }
query={ query }
totalRows={ get(
primaryData,
[ 'data', 'totals', 'orders_count' ],
Object.keys( orders ).length
) }
/>
</Fragment>
);
}
}
2018-10-11 17:07:59 +00:00
OrdersReport.propTypes = {
params: PropTypes.object.isRequired,
path: PropTypes.string.isRequired,
query: PropTypes.object.isRequired,
};
export default compose(
2018-10-10 20:29:12 +00:00
withSelect( ( select, props ) => {
const { query } = props;
const datesFromQuery = getCurrentDates( query );
const primaryData = getReportChartData( 'orders', 'primary', query, select );
2018-10-10 20:29:12 +00:00
const { getOrders, isGetOrdersError, isGetOrdersRequesting } = select( 'wc-admin' );
const tableQuery = {
orderby: query.orderby || 'date',
order: query.order || 'asc',
page: query.page || 1,
per_page: query.per_page || 25,
after: appendTimestamp( datesFromQuery.primary.after, 'start' ),
before: appendTimestamp( datesFromQuery.primary.before, 'end' ),
status: [ 'processing', 'on-hold', 'completed' ],
};
const orders = getOrders( tableQuery );
const isTableDataError = isGetOrdersError( tableQuery );
const isTableDataRequesting = isGetOrdersRequesting( tableQuery );
return {
isTableDataError,
isTableDataRequesting,
2018-10-11 15:43:25 +00:00
orders,
2018-10-10 20:29:12 +00:00
primaryData,
};
} )
)( OrdersReport );