2019-01-08 07:20:01 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-05-22 20:43:45 +00:00
|
|
|
import { SelectControl } from '@wordpress/components';
|
2019-01-08 07:20:01 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { EllipsisMenu, MenuItem, MenuTitle, SectionHeader } from '@woocommerce/components';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-04-11 02:53:05 +00:00
|
|
|
import Leaderboard from 'analytics/components/leaderboard';
|
2019-01-08 07:20:01 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
2019-07-01 10:16:12 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
2019-01-08 07:20:01 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
class Leaderboards extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( ...arguments );
|
2019-05-02 10:22:34 +00:00
|
|
|
|
2019-01-08 07:20:01 +00:00
|
|
|
this.state = {
|
2019-01-15 02:24:06 +00:00
|
|
|
rowsPerTable: parseInt( props.userPrefLeaderboardRows ) || 5,
|
2019-01-08 07:20:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
setRowsPerTable = rows => {
|
|
|
|
this.setState( { rowsPerTable: parseInt( rows ) } );
|
|
|
|
const userDataFields = {
|
|
|
|
[ 'dashboard_leaderboard_rows' ]: parseInt( rows ),
|
|
|
|
};
|
|
|
|
this.props.updateCurrentUserData( userDataFields );
|
|
|
|
};
|
|
|
|
|
|
|
|
renderMenu() {
|
2019-04-30 00:35:37 +00:00
|
|
|
const {
|
|
|
|
allLeaderboards,
|
2019-05-07 07:21:34 +00:00
|
|
|
isFirst,
|
|
|
|
isLast,
|
|
|
|
hiddenBlocks,
|
|
|
|
onMove,
|
|
|
|
onRemove,
|
2019-04-30 00:35:37 +00:00
|
|
|
onTitleBlur,
|
|
|
|
onTitleChange,
|
2019-05-07 07:21:34 +00:00
|
|
|
onToggleHiddenBlock,
|
2019-04-30 00:35:37 +00:00
|
|
|
titleInput,
|
2019-05-09 01:13:14 +00:00
|
|
|
controls: Controls,
|
2019-04-30 00:35:37 +00:00
|
|
|
} = this.props;
|
2019-05-07 07:21:34 +00:00
|
|
|
const { rowsPerTable } = this.state;
|
2019-04-11 02:53:05 +00:00
|
|
|
|
2019-01-08 07:20:01 +00:00
|
|
|
return (
|
2019-02-25 10:17:56 +00:00
|
|
|
<EllipsisMenu
|
2019-03-13 17:14:02 +00:00
|
|
|
label={ __(
|
2019-05-02 10:22:34 +00:00
|
|
|
'Choose which leaderboards to display and other settings',
|
2019-03-13 17:14:02 +00:00
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-05-03 01:37:16 +00:00
|
|
|
renderContent={ ( { onToggle } ) => (
|
2019-04-30 00:35:37 +00:00
|
|
|
<Fragment>
|
|
|
|
<MenuTitle>{ __( 'Leaderboards', 'woocommerce-admin' ) }</MenuTitle>
|
|
|
|
{ allLeaderboards.map( leaderboard => {
|
2019-07-01 10:16:12 +00:00
|
|
|
const checked = ! hiddenBlocks.includes( leaderboard.id );
|
2019-04-30 00:35:37 +00:00
|
|
|
return (
|
|
|
|
<MenuItem
|
2019-07-01 10:16:12 +00:00
|
|
|
checked={ checked }
|
2019-04-30 00:35:37 +00:00
|
|
|
isCheckbox
|
|
|
|
isClickable
|
|
|
|
key={ leaderboard.id }
|
2019-07-01 10:16:12 +00:00
|
|
|
onInvoke={ () => {
|
|
|
|
onToggleHiddenBlock( leaderboard.id )();
|
|
|
|
recordEvent( 'dash_leaderboards_toggle', {
|
|
|
|
status: checked ? 'off' : 'on',
|
|
|
|
key: leaderboard.id,
|
|
|
|
} );
|
|
|
|
} }
|
2019-04-30 00:35:37 +00:00
|
|
|
>
|
|
|
|
{ leaderboard.label }
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
<SelectControl
|
|
|
|
className="woocommerce-dashboard__dashboard-leaderboards__select"
|
2019-05-13 03:51:25 +00:00
|
|
|
label={ __( 'Rows Per Table', 'woocommerce-admin' ) }
|
2019-04-30 00:35:37 +00:00
|
|
|
value={ rowsPerTable }
|
|
|
|
options={ Array.from( { length: 20 }, ( v, key ) => ( {
|
|
|
|
v: key + 1,
|
|
|
|
label: key + 1,
|
|
|
|
} ) ) }
|
|
|
|
onChange={ this.setRowsPerTable }
|
|
|
|
/>
|
2019-05-09 01:13:14 +00:00
|
|
|
{ window.wcAdminFeatures[ 'analytics-dashboard/customizable' ] && (
|
2019-05-22 20:43:45 +00:00
|
|
|
<Controls
|
|
|
|
onToggle={ onToggle }
|
|
|
|
onMove={ onMove }
|
|
|
|
onRemove={ onRemove }
|
|
|
|
isFirst={ isFirst }
|
|
|
|
isLast={ isLast }
|
|
|
|
onTitleBlur={ onTitleBlur }
|
|
|
|
onTitleChange={ onTitleChange }
|
|
|
|
titleInput={ titleInput }
|
|
|
|
/>
|
2019-05-09 01:13:14 +00:00
|
|
|
) }
|
2019-04-30 00:35:37 +00:00
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
/>
|
2019-01-08 07:20:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-11 02:53:05 +00:00
|
|
|
renderLeaderboards() {
|
2019-05-07 07:21:34 +00:00
|
|
|
const { rowsPerTable } = this.state;
|
|
|
|
const { allLeaderboards, hiddenBlocks, query } = this.props;
|
2019-04-11 02:53:05 +00:00
|
|
|
|
|
|
|
return allLeaderboards.map( leaderboard => {
|
2019-05-07 07:21:34 +00:00
|
|
|
if ( hiddenBlocks.includes( leaderboard.id ) ) {
|
2019-04-11 02:53:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Leaderboard
|
|
|
|
headers={ leaderboard.headers }
|
|
|
|
id={ leaderboard.id }
|
|
|
|
key={ leaderboard.id }
|
|
|
|
query={ query }
|
|
|
|
title={ leaderboard.label }
|
|
|
|
totalRows={ rowsPerTable }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-05-02 10:22:34 +00:00
|
|
|
const { title } = this.props;
|
|
|
|
|
2019-01-08 07:20:01 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<div className="woocommerce-dashboard__dashboard-leaderboards">
|
2019-03-13 17:14:02 +00:00
|
|
|
<SectionHeader
|
2019-05-02 10:22:34 +00:00
|
|
|
title={ title || __( 'Leaderboards', 'woocommerce-admin' ) }
|
2019-03-13 17:14:02 +00:00
|
|
|
menu={ this.renderMenu() }
|
|
|
|
/>
|
2019-04-11 02:53:05 +00:00
|
|
|
<div className="woocommerce-dashboard__columns">{ this.renderLeaderboards() }</div>
|
2019-01-08 07:20:01 +00:00
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Leaderboards.propTypes = {
|
|
|
|
query: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( select => {
|
2019-04-11 02:53:05 +00:00
|
|
|
const { getCurrentUserData, getItems, getItemsError, isGetItemsRequesting } = select(
|
|
|
|
'wc-api'
|
|
|
|
);
|
2019-01-08 07:20:01 +00:00
|
|
|
const userData = getCurrentUserData();
|
2019-04-11 02:53:05 +00:00
|
|
|
const allLeaderboards = wcSettings.dataEndpoints.leaderboards;
|
2019-01-08 07:20:01 +00:00
|
|
|
|
|
|
|
return {
|
2019-04-11 02:53:05 +00:00
|
|
|
allLeaderboards,
|
|
|
|
getItems,
|
|
|
|
getItemsError,
|
|
|
|
isGetItemsRequesting,
|
2019-01-08 07:20:01 +00:00
|
|
|
userPrefLeaderboardRows: userData.dashboard_leaderboard_rows,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
|
|
|
const { updateCurrentUserData } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
updateCurrentUserData,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Leaderboards );
|