/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { Fragment, useState } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import PropTypes from 'prop-types'; import { SelectControl } from '@wordpress/components'; import { withSelect } from '@wordpress/data'; import { EllipsisMenu, MenuItem, MenuTitle, SectionHeader, } from '@woocommerce/components'; import { useUserPreferences, ITEMS_STORE_NAME } from '@woocommerce/data'; import { recordEvent } from '@woocommerce/tracks'; /** * Internal dependencies */ import Leaderboard from '../../analytics/components/leaderboard'; import { getAdminSetting } from '~/utils/admin-settings'; import './style.scss'; const renderLeaderboardToggles = ( { allLeaderboards, hiddenBlocks, onToggleHiddenBlock, } ) => { return 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 } ); } ); }; const renderLeaderboards = ( { allLeaderboards, hiddenBlocks, query, rowsPerTable, filters, } ) => { return allLeaderboards.map( ( leaderboard ) => { if ( hiddenBlocks.includes( leaderboard.id ) ) { return undefined; } return ( ); } ); }; const Leaderboards = ( props ) => { const { allLeaderboards, controls: Controls, isFirst, isLast, hiddenBlocks, onMove, onRemove, onTitleBlur, onTitleChange, onToggleHiddenBlock, query, title, titleInput, filters, } = props; const { updateUserPreferences, ...userPrefs } = useUserPreferences(); const [ rowsPerTable, setRowsPerTableState ] = useState( parseInt( userPrefs.dashboard_leaderboard_rows || 5, 10 ) ); const setRowsPerTable = ( rows ) => { setRowsPerTableState( parseInt( rows, 10 ) ); const userDataFields = { dashboard_leaderboard_rows: parseInt( rows, 10 ), }; updateUserPreferences( userDataFields ); }; const renderMenu = () => ( ( { __( 'Leaderboards', 'woocommerce' ) } { renderLeaderboardToggles( { allLeaderboards, hiddenBlocks, onToggleHiddenBlock, } ) } ( { v: key + 1, label: key + 1, } ) ) } onChange={ setRowsPerTable } /> ) } /> ); return ( { renderLeaderboards( { allLeaderboards, hiddenBlocks, query, rowsPerTable, filters, } ) } ); }; Leaderboards.propTypes = { query: PropTypes.object.isRequired, }; export default compose( withSelect( ( select ) => { const { getItems, getItemsError } = select( ITEMS_STORE_NAME ); const { leaderboards: allLeaderboards } = getAdminSetting( 'dataEndpoints', { leaderboards: [], } ); return { allLeaderboards, getItems, getItemsError, }; } ) )( Leaderboards );