/** @format */ /** * External dependencies */ import { __ } from '@wordpress/i18n'; import { Component, Fragment } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import PropTypes from 'prop-types'; import { SelectControl } from '@wordpress/components'; import { withDispatch } from '@wordpress/data'; /** * WooCommerce dependencies */ import { EllipsisMenu, MenuItem, MenuTitle, SectionHeader } from '@woocommerce/components'; import { getSetting } from '@woocommerce/wc-admin-settings'; /** * Internal dependencies */ import Leaderboard from 'analytics/components/leaderboard'; import withSelect from 'wc-api/with-select'; import { recordEvent } from 'lib/tracks'; import './style.scss'; class Leaderboards extends Component { constructor( props ) { super( ...arguments ); this.state = { rowsPerTable: parseInt( props.userPrefLeaderboardRows ) || 5, }; } setRowsPerTable = rows => { this.setState( { rowsPerTable: parseInt( rows ) } ); const userDataFields = { [ 'dashboard_leaderboard_rows' ]: parseInt( rows ), }; this.props.updateCurrentUserData( userDataFields ); }; renderMenu() { const { allLeaderboards, isFirst, isLast, hiddenBlocks, onMove, onRemove, onTitleBlur, onTitleChange, onToggleHiddenBlock, titleInput, controls: Controls, } = this.props; const { rowsPerTable } = this.state; return ( ( { __( 'Leaderboards', 'woocommerce-admin' ) } { allLeaderboards.map( leaderboard => { const checked = ! hiddenBlocks.includes( leaderboard.id ); return ( { onToggleHiddenBlock( leaderboard.id )(); recordEvent( 'dash_leaderboards_toggle', { status: checked ? 'off' : 'on', key: leaderboard.id, } ); } } > { leaderboard.label } ); } ) } ( { v: key + 1, label: key + 1, } ) ) } onChange={ this.setRowsPerTable } /> { window.wcAdminFeatures[ 'analytics-dashboard/customizable' ] && ( ) } ) } /> ); } renderLeaderboards() { const { rowsPerTable } = this.state; const { allLeaderboards, hiddenBlocks, query } = this.props; return allLeaderboards.map( leaderboard => { if ( hiddenBlocks.includes( leaderboard.id ) ) { return; } return ( ); } ); } render() { const { title } = this.props; return (
{ this.renderLeaderboards() }
); } } Leaderboards.propTypes = { query: PropTypes.object.isRequired, }; export default compose( withSelect( select => { const { getCurrentUserData, getItems, getItemsError, isGetItemsRequesting } = select( 'wc-api' ); const userData = getCurrentUserData(); const { allLeaderboards } = getSetting( 'dataEndpoints', { leaderboards: {} } ); return { allLeaderboards, getItems, getItemsError, isGetItemsRequesting, userPrefLeaderboardRows: userData.dashboard_leaderboard_rows, }; } ), withDispatch( dispatch => { const { updateCurrentUserData } = dispatch( 'wc-api' ); return { updateCurrentUserData, }; } ) )( Leaderboards );