/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { Card, CardBody, CardHeader } from '@wordpress/components'; import { Component } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import { EmptyTable, TableCard } from '@woocommerce/components'; import { withSelect } from '@wordpress/data'; import PropTypes from 'prop-types'; import { getPersistedQuery } from '@woocommerce/navigation'; import { getFilterQuery, getLeaderboard, SETTINGS_STORE_NAME, } from '@woocommerce/data'; import { CurrencyContext } from '@woocommerce/currency'; import { formatValue } from '@woocommerce/number'; import { Text } from '@woocommerce/experimental'; /** * Internal dependencies */ import ReportError from '../report-error'; import sanitizeHTML from '../../../lib/sanitize-html'; import './style.scss'; const formattable = new Set( [ 'currency', 'number' ] ); export class Leaderboard extends Component { getFormattedColumn = ( column ) => { const { format } = column; /* * The format property is used for numeric columns and is optional. * The `value` property type is specified as string in the API schema * and it's extensible from other extensions. Therefore, even if the * actual type of numeric columns returned by WooCoomerce's own API is * number, there is no guarantee the value will be a number. */ if ( formattable.has( column.format ) && isFinite( column.value ) ) { const value = parseFloat( column.value ); if ( ! Number.isNaN( value ) ) { const { formatAmount, getCurrencyConfig } = this.context; const display = format === 'currency' ? formatAmount( value ) : formatValue( getCurrencyConfig(), format, value ); return { display, value, }; } } return { display: (
), value: column.value, }; }; getFormattedHeaders() { return this.props.headers.map( ( header, i ) => { return { isLeftAligned: i === 0, hiddenByDefault: false, isSortable: false, key: header.label, label: header.label, }; } ); } getFormattedRows() { return this.props.rows.map( ( row ) => { return row.map( this.getFormattedColumn ); } ); } render() { const { isRequesting, isError, totalRows, title } = this.props; const classes = 'woocommerce-leaderboard'; if ( isError ) { return