2018-12-31 06:42:46 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { Card, EmptyTable, TableCard } from '@woocommerce/components';
|
2019-04-11 02:53:05 +00:00
|
|
|
import { getPersistedQuery } from '@woocommerce/navigation';
|
2018-12-31 06:42:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-04-11 02:53:05 +00:00
|
|
|
import { getLeaderboard } from 'wc-api/items/utils';
|
2018-12-31 06:42:46 +00:00
|
|
|
import ReportError from 'analytics/components/report-error';
|
2019-04-11 02:53:05 +00:00
|
|
|
import sanitizeHTML from 'lib/sanitize-html';
|
2018-12-31 06:42:46 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
export class Leaderboard extends Component {
|
2019-04-11 02:53:05 +00:00
|
|
|
getFormattedHeaders() {
|
|
|
|
return this.props.headers.map( ( header, i ) => {
|
|
|
|
return {
|
|
|
|
isLeftAligned: 0 === i,
|
|
|
|
hiddenByDefault: false,
|
|
|
|
isSortable: false,
|
|
|
|
key: header.label,
|
|
|
|
label: header.label,
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
getFormattedRows() {
|
|
|
|
return this.props.rows.map( row => {
|
|
|
|
return row.map( column => {
|
|
|
|
return {
|
|
|
|
display: <div dangerouslySetInnerHTML={ sanitizeHTML( column.display ) } />,
|
|
|
|
value: column.value,
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-12-31 06:42:46 +00:00
|
|
|
render() {
|
2019-04-11 02:53:05 +00:00
|
|
|
const { isRequesting, isError, totalRows, title } = this.props;
|
|
|
|
const rows = this.getFormattedRows();
|
2019-11-21 12:10:37 +00:00
|
|
|
const classes = 'woocommerce-leaderboard woocommerce-analytics__card';
|
2018-12-31 06:42:46 +00:00
|
|
|
|
|
|
|
if ( isError ) {
|
2019-11-21 12:10:37 +00:00
|
|
|
return <ReportError className={ classes } isError />;
|
2018-12-31 06:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isRequesting && rows.length === 0 ) {
|
|
|
|
return (
|
2019-11-21 12:10:37 +00:00
|
|
|
<Card title={ title } className={ classes }>
|
2018-12-31 06:42:46 +00:00
|
|
|
<EmptyTable>
|
2019-03-13 17:14:02 +00:00
|
|
|
{ __( 'No data recorded for the selected time period.', 'woocommerce-admin' ) }
|
2018-12-31 06:42:46 +00:00
|
|
|
</EmptyTable>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TableCard
|
2019-11-21 12:10:37 +00:00
|
|
|
className={ classes }
|
2019-04-11 02:53:05 +00:00
|
|
|
headers={ this.getFormattedHeaders() }
|
2018-12-31 06:42:46 +00:00
|
|
|
isLoading={ isRequesting }
|
|
|
|
rows={ rows }
|
2019-01-08 07:20:01 +00:00
|
|
|
rowsPerPage={ totalRows }
|
2019-01-15 02:13:15 +00:00
|
|
|
showMenu={ false }
|
2018-12-31 06:42:46 +00:00
|
|
|
title={ title }
|
2019-01-08 07:20:01 +00:00
|
|
|
totalRows={ totalRows }
|
2018-12-31 06:42:46 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Leaderboard.propTypes = {
|
|
|
|
/**
|
2019-04-11 02:53:05 +00:00
|
|
|
* An array of column headers.
|
2018-12-31 06:42:46 +00:00
|
|
|
*/
|
2019-04-11 02:53:05 +00:00
|
|
|
headers: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
|
|
|
label: PropTypes.string,
|
|
|
|
} )
|
|
|
|
),
|
2018-12-31 06:42:46 +00:00
|
|
|
/**
|
2019-04-11 02:53:05 +00:00
|
|
|
* String of leaderboard ID to display.
|
2018-12-31 06:42:46 +00:00
|
|
|
*/
|
2019-04-11 02:53:05 +00:00
|
|
|
id: PropTypes.string.isRequired,
|
2018-12-31 06:42:46 +00:00
|
|
|
/**
|
|
|
|
* Query args added to the report table endpoint request.
|
|
|
|
*/
|
|
|
|
query: PropTypes.object,
|
|
|
|
/**
|
2019-04-11 02:53:05 +00:00
|
|
|
* Which column should be the row header, defaults to the first item (`0`) (see `Table` props).
|
2018-12-31 06:42:46 +00:00
|
|
|
*/
|
2019-04-11 02:53:05 +00:00
|
|
|
rows: PropTypes.arrayOf(
|
|
|
|
PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
|
|
|
display: PropTypes.node,
|
|
|
|
value: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number, PropTypes.bool ] ),
|
|
|
|
} )
|
|
|
|
)
|
|
|
|
).isRequired,
|
2018-12-31 06:42:46 +00:00
|
|
|
/**
|
|
|
|
* String to display as the title of the table.
|
|
|
|
*/
|
|
|
|
title: PropTypes.string.isRequired,
|
2019-04-11 02:53:05 +00:00
|
|
|
/**
|
|
|
|
* Number of table rows.
|
|
|
|
*/
|
|
|
|
totalRows: PropTypes.number.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
Leaderboard.defaultProps = {
|
|
|
|
rows: [],
|
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
2018-12-31 06:42:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select, props ) => {
|
2019-04-11 02:53:05 +00:00
|
|
|
const { id, query, totalRows } = props;
|
|
|
|
const leaderboardQuery = {
|
|
|
|
id,
|
|
|
|
per_page: totalRows,
|
|
|
|
persisted_query: getPersistedQuery( query ),
|
2019-03-21 03:25:05 +00:00
|
|
|
query,
|
|
|
|
select,
|
2019-04-11 02:53:05 +00:00
|
|
|
};
|
|
|
|
const leaderboardData = getLeaderboard( leaderboardQuery );
|
2018-12-31 06:42:46 +00:00
|
|
|
|
2019-04-11 02:53:05 +00:00
|
|
|
return leaderboardData;
|
2018-12-31 06:42:46 +00:00
|
|
|
} )
|
|
|
|
)( Leaderboard );
|