2018-05-15 15:06:15 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-07-05 08:15:49 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2019-02-01 09:55:19 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2020-08-20 23:37:41 +00:00
|
|
|
import { withSelect } from '@wordpress/data';
|
2018-06-14 20:15:11 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-08-20 21:24:17 +00:00
|
|
|
import { find } from 'lodash';
|
2019-02-26 10:06:37 +00:00
|
|
|
import { getQuery, getSearchWords } from '@woocommerce/navigation';
|
2021-08-09 09:28:49 +00:00
|
|
|
import { searchItemsByString } from '@woocommerce/data';
|
2018-11-15 18:16:23 +00:00
|
|
|
|
2018-05-15 15:06:15 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
Add loading indicators, error state, and EmptyContent to the revenue report. (#347, woocommerce/woocommerce-admin#348)
* Add loading indiciators for the revenue report.
* Improve accessibility, and fix up some documentation comments.
* Fix top border on mobile
* Add EmptyContent Component and revenue error/empty states. (https://github.com/woocommerce/woocommerce-admin/pull/348)
* Add EmptyContent Component and revenue error/empty states.
* Move relative image handling to ImageAsset, combine secondary and primary action rendering, add some missing isRequired proptypes, add empty error handling.
* Handle PR Feedback: Clean up button css, set a default for illustration, fix deprecation typo, some code cleanup.
2018-09-05 16:45:49 +00:00
|
|
|
import './style.scss';
|
2020-08-13 02:05:22 +00:00
|
|
|
import ReportError from '../components/report-error';
|
2020-04-02 21:54:38 +00:00
|
|
|
import {
|
|
|
|
CurrencyContext,
|
|
|
|
getFilteredCurrencyInstance,
|
2020-08-13 02:05:22 +00:00
|
|
|
} from '../../lib/currency-context';
|
2020-04-29 18:01:27 +00:00
|
|
|
import getReports from './get-reports';
|
2018-08-20 21:24:17 +00:00
|
|
|
|
2020-03-02 22:22:32 +00:00
|
|
|
/**
|
|
|
|
* The Customers Report will not have the `report` param supplied by the router/
|
|
|
|
* because it no longer exists under the path `/analytics/:report`. Use `props.path`/
|
|
|
|
* instead to determine if the Customers Report is being rendered.
|
|
|
|
*
|
2020-08-17 21:36:24 +00:00
|
|
|
* @param {Object} args
|
|
|
|
* @param {Object} args.params - url parameters
|
|
|
|
* @param {string} args.path
|
2020-03-02 22:22:32 +00:00
|
|
|
* @return {string} - report parameter
|
|
|
|
*/
|
|
|
|
const getReportParam = ( { params, path } ) => {
|
|
|
|
return params.report || path.replace( /^\/+/, '' );
|
|
|
|
};
|
|
|
|
|
2018-06-14 20:15:11 +00:00
|
|
|
class Report extends Component {
|
2018-08-20 21:24:17 +00:00
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
hasError: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidCatch( error ) {
|
|
|
|
this.setState( {
|
|
|
|
hasError: true,
|
|
|
|
} );
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.warn( error );
|
|
|
|
/* eslint-enable no-console */
|
|
|
|
}
|
|
|
|
|
2018-05-15 15:06:15 +00:00
|
|
|
render() {
|
2018-08-20 21:24:17 +00:00
|
|
|
if ( this.state.hasError ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-03-02 22:22:32 +00:00
|
|
|
const { isError } = this.props;
|
2019-02-26 09:28:50 +00:00
|
|
|
|
|
|
|
if ( isError ) {
|
2021-06-01 10:07:35 +00:00
|
|
|
return <ReportError />;
|
2019-02-26 09:28:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 22:22:32 +00:00
|
|
|
const reportParam = getReportParam( this.props );
|
2020-02-20 11:59:02 +00:00
|
|
|
|
|
|
|
const report = find( getReports(), { report: reportParam } );
|
2018-08-20 21:24:17 +00:00
|
|
|
if ( ! report ) {
|
|
|
|
return null;
|
2018-06-14 20:15:11 +00:00
|
|
|
}
|
2018-08-20 21:24:17 +00:00
|
|
|
const Container = report.component;
|
2020-04-02 21:54:38 +00:00
|
|
|
return (
|
|
|
|
<CurrencyContext.Provider
|
|
|
|
value={ getFilteredCurrencyInstance( getQuery() ) }
|
|
|
|
>
|
|
|
|
<Container { ...this.props } />
|
|
|
|
</CurrencyContext.Provider>
|
|
|
|
);
|
2018-05-15 15:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-14 20:15:11 +00:00
|
|
|
|
|
|
|
Report.propTypes = {
|
|
|
|
params: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
2019-02-01 09:55:19 +00:00
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
2019-02-26 10:06:37 +00:00
|
|
|
const query = getQuery();
|
|
|
|
const { search } = query;
|
2019-02-01 09:55:19 +00:00
|
|
|
|
|
|
|
if ( ! search ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-03-02 22:22:32 +00:00
|
|
|
const report = getReportParam( props );
|
2019-02-26 10:06:37 +00:00
|
|
|
const searchWords = getSearchWords( query );
|
2019-03-07 02:57:22 +00:00
|
|
|
// Single Category view in Categories Report uses the products endpoint, so search must also.
|
|
|
|
const mappedReport =
|
2020-02-14 02:23:21 +00:00
|
|
|
report === 'categories' && query.filter === 'single_category'
|
|
|
|
? 'products'
|
|
|
|
: report;
|
|
|
|
const itemsResult = searchItemsByString(
|
2021-08-09 09:28:49 +00:00
|
|
|
select,
|
2020-02-14 02:23:21 +00:00
|
|
|
mappedReport,
|
2021-07-27 20:11:32 +00:00
|
|
|
searchWords,
|
|
|
|
{
|
|
|
|
per_page: 100,
|
|
|
|
}
|
2020-02-14 02:23:21 +00:00
|
|
|
);
|
2019-02-26 09:28:50 +00:00
|
|
|
const { isError, isRequesting, items } = itemsResult;
|
2019-02-01 09:55:19 +00:00
|
|
|
const ids = Object.keys( items );
|
|
|
|
if ( ! ids.length ) {
|
2019-02-20 09:21:05 +00:00
|
|
|
return {
|
2019-02-26 09:28:50 +00:00
|
|
|
isError,
|
|
|
|
isRequesting,
|
2019-02-20 09:21:05 +00:00
|
|
|
};
|
2019-02-01 09:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2019-02-26 09:28:50 +00:00
|
|
|
isError,
|
|
|
|
isRequesting,
|
2019-02-01 09:55:19 +00:00
|
|
|
query: {
|
|
|
|
...props.query,
|
2019-03-07 02:57:22 +00:00
|
|
|
[ mappedReport ]: ids.join( ',' ),
|
2019-02-01 09:55:19 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Report );
|