2019-01-08 07:20:01 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-06-10 16:46:46 +00:00
|
|
|
import { Fragment, useState } from '@wordpress/element';
|
2019-01-08 07:20:01 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-05-22 20:43:45 +00:00
|
|
|
import { SelectControl } from '@wordpress/components';
|
2020-08-20 23:37:41 +00:00
|
|
|
import { withSelect } from '@wordpress/data';
|
2020-02-14 02:23:21 +00:00
|
|
|
import {
|
|
|
|
EllipsisMenu,
|
|
|
|
MenuItem,
|
|
|
|
MenuTitle,
|
|
|
|
SectionHeader,
|
|
|
|
} from '@woocommerce/components';
|
2020-08-20 23:37:41 +00:00
|
|
|
import { useUserPreferences, ITEMS_STORE_NAME } from '@woocommerce/data';
|
2019-09-23 21:47:08 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2019-01-08 07:20:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import Leaderboard from '../../analytics/components/leaderboard';
|
2019-01-08 07:20:01 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
2020-07-20 22:17:28 +00:00
|
|
|
const renderLeaderboardToggles = ( {
|
|
|
|
allLeaderboards,
|
|
|
|
hiddenBlocks,
|
|
|
|
onToggleHiddenBlock,
|
|
|
|
} ) => {
|
2020-06-10 16:46:46 +00:00
|
|
|
return allLeaderboards.map( ( leaderboard ) => {
|
2020-07-20 22:17:28 +00:00
|
|
|
const checked = ! hiddenBlocks.includes( leaderboard.id );
|
2020-06-10 16:46:46 +00:00
|
|
|
return (
|
|
|
|
<MenuItem
|
|
|
|
checked={ checked }
|
|
|
|
isCheckbox
|
|
|
|
isClickable
|
|
|
|
key={ leaderboard.id }
|
|
|
|
onInvoke={ () => {
|
|
|
|
onToggleHiddenBlock( leaderboard.id )();
|
2020-07-20 22:17:28 +00:00
|
|
|
recordEvent( 'dash_leaderboards_toggle', {
|
|
|
|
status: checked ? 'off' : 'on',
|
|
|
|
key: leaderboard.id,
|
|
|
|
} );
|
2020-06-10 16:46:46 +00:00
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ leaderboard.label }
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
};
|
2019-05-02 10:22:34 +00:00
|
|
|
|
2020-07-20 22:17:28 +00:00
|
|
|
const renderLeaderboards = ( {
|
|
|
|
allLeaderboards,
|
|
|
|
hiddenBlocks,
|
|
|
|
query,
|
|
|
|
rowsPerTable,
|
|
|
|
filters,
|
|
|
|
} ) => {
|
2020-06-10 16:46:46 +00:00
|
|
|
return allLeaderboards.map( ( leaderboard ) => {
|
|
|
|
if ( hiddenBlocks.includes( leaderboard.id ) ) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-01-08 07:20:01 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
return (
|
|
|
|
<Leaderboard
|
|
|
|
headers={ leaderboard.headers }
|
|
|
|
id={ leaderboard.id }
|
|
|
|
key={ leaderboard.id }
|
|
|
|
query={ query }
|
|
|
|
title={ leaderboard.label }
|
|
|
|
totalRows={ rowsPerTable }
|
2020-07-20 22:17:28 +00:00
|
|
|
filters={ filters }
|
2020-06-10 16:46:46 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const Leaderboards = ( props ) => {
|
|
|
|
const {
|
|
|
|
allLeaderboards,
|
|
|
|
controls: Controls,
|
|
|
|
isFirst,
|
|
|
|
isLast,
|
|
|
|
hiddenBlocks,
|
|
|
|
onMove,
|
|
|
|
onRemove,
|
|
|
|
onTitleBlur,
|
|
|
|
onTitleChange,
|
|
|
|
onToggleHiddenBlock,
|
|
|
|
query,
|
|
|
|
title,
|
|
|
|
titleInput,
|
2020-07-20 22:17:28 +00:00
|
|
|
filters,
|
2020-06-10 16:46:46 +00:00
|
|
|
} = props;
|
|
|
|
const { updateUserPreferences, ...userPrefs } = useUserPreferences();
|
2020-07-20 22:17:28 +00:00
|
|
|
const [ rowsPerTable, setRowsPerTableState ] = useState(
|
|
|
|
parseInt( userPrefs.dashboard_leaderboard_rows || 5, 10 )
|
|
|
|
);
|
2020-06-10 16:46:46 +00:00
|
|
|
|
|
|
|
const setRowsPerTable = ( rows ) => {
|
|
|
|
setRowsPerTableState( parseInt( rows, 10 ) );
|
2019-01-08 07:20:01 +00:00
|
|
|
const userDataFields = {
|
2020-02-14 02:23:21 +00:00
|
|
|
dashboard_leaderboard_rows: parseInt( rows, 10 ),
|
2019-01-08 07:20:01 +00:00
|
|
|
};
|
2020-06-10 16:46:46 +00:00
|
|
|
updateUserPreferences( userDataFields );
|
2019-01-08 07:20:01 +00:00
|
|
|
};
|
|
|
|
|
2020-07-20 22:17:28 +00:00
|
|
|
const renderMenu = () => (
|
2020-06-10 16:46:46 +00:00
|
|
|
<EllipsisMenu
|
|
|
|
label={ __(
|
|
|
|
'Choose which leaderboards to display and other settings',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
renderContent={ ( { onToggle } ) => (
|
|
|
|
<Fragment>
|
|
|
|
<MenuTitle>
|
|
|
|
{ __( 'Leaderboards', 'woocommerce-admin' ) }
|
|
|
|
</MenuTitle>
|
2020-07-20 22:17:28 +00:00
|
|
|
{ renderLeaderboardToggles( {
|
|
|
|
allLeaderboards,
|
|
|
|
hiddenBlocks,
|
|
|
|
onToggleHiddenBlock,
|
|
|
|
} ) }
|
2020-06-10 16:46:46 +00:00
|
|
|
<SelectControl
|
|
|
|
className="woocommerce-dashboard__dashboard-leaderboards__select"
|
2020-07-20 22:17:28 +00:00
|
|
|
label={ __( 'Rows Per Table', 'woocommerce-admin' ) }
|
2020-06-10 16:46:46 +00:00
|
|
|
value={ rowsPerTable }
|
2020-07-20 22:17:28 +00:00
|
|
|
options={ Array.from( { length: 20 }, ( v, key ) => ( {
|
|
|
|
v: key + 1,
|
|
|
|
label: key + 1,
|
|
|
|
} ) ) }
|
2020-06-10 16:46:46 +00:00
|
|
|
onChange={ setRowsPerTable }
|
2019-03-13 17:14:02 +00:00
|
|
|
/>
|
2021-06-29 23:47:38 +00:00
|
|
|
<Controls
|
|
|
|
onToggle={ onToggle }
|
|
|
|
onMove={ onMove }
|
|
|
|
onRemove={ onRemove }
|
|
|
|
isFirst={ isFirst }
|
|
|
|
isLast={ isLast }
|
|
|
|
onTitleBlur={ onTitleBlur }
|
|
|
|
onTitleChange={ onTitleChange }
|
|
|
|
titleInput={ titleInput }
|
|
|
|
/>
|
2020-06-10 16:46:46 +00:00
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<div className="woocommerce-dashboard__dashboard-leaderboards">
|
|
|
|
<SectionHeader
|
2020-07-20 22:17:28 +00:00
|
|
|
title={ title || __( 'Leaderboards', 'woocommerce-admin' ) }
|
2020-06-10 16:46:46 +00:00
|
|
|
menu={ renderMenu() }
|
|
|
|
/>
|
|
|
|
<div className="woocommerce-dashboard__columns">
|
2020-07-20 22:17:28 +00:00
|
|
|
{ renderLeaderboards( {
|
|
|
|
allLeaderboards,
|
|
|
|
hiddenBlocks,
|
|
|
|
query,
|
|
|
|
rowsPerTable,
|
|
|
|
filters,
|
|
|
|
} ) }
|
2019-01-08 07:20:01 +00:00
|
|
|
</div>
|
2020-06-10 16:46:46 +00:00
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
2020-07-20 22:17:28 +00:00
|
|
|
};
|
2019-01-08 07:20:01 +00:00
|
|
|
|
|
|
|
Leaderboards.propTypes = {
|
|
|
|
query: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
2020-02-14 02:23:21 +00:00
|
|
|
withSelect( ( select ) => {
|
2020-08-20 23:37:41 +00:00
|
|
|
const { getItems, getItemsError } = select( ITEMS_STORE_NAME );
|
2020-02-14 02:23:21 +00:00
|
|
|
const { leaderboards: allLeaderboards } = getSetting( 'dataEndpoints', {
|
|
|
|
leaderboards: [],
|
|
|
|
} );
|
2019-01-08 07:20:01 +00:00
|
|
|
|
|
|
|
return {
|
2019-04-11 02:53:05 +00:00
|
|
|
allLeaderboards,
|
|
|
|
getItems,
|
|
|
|
getItemsError,
|
2019-01-08 07:20:01 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Leaderboards );
|