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';
|
2019-02-26 03:34:43 +00:00
|
|
|
import { xor } from 'lodash';
|
2019-01-08 07:20:01 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-02-25 19:25:19 +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';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
class Leaderboards extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( ...arguments );
|
|
|
|
this.state = {
|
2019-04-11 02:53:05 +00:00
|
|
|
hiddenLeaderboardKeys: props.userPrefLeaderboards || [ 'coupons', 'customers' ],
|
2019-01-15 02:24:06 +00:00
|
|
|
rowsPerTable: parseInt( props.userPrefLeaderboardRows ) || 5,
|
2019-01-08 07:20:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.toggle = this.toggle.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle( key ) {
|
|
|
|
return () => {
|
|
|
|
const hiddenLeaderboardKeys = xor( this.state.hiddenLeaderboardKeys, [ key ] );
|
|
|
|
this.setState( { hiddenLeaderboardKeys } );
|
|
|
|
const userDataFields = {
|
|
|
|
[ 'dashboard_leaderboards' ]: hiddenLeaderboardKeys,
|
|
|
|
};
|
|
|
|
this.props.updateCurrentUserData( userDataFields );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
setRowsPerTable = rows => {
|
|
|
|
this.setState( { rowsPerTable: parseInt( rows ) } );
|
|
|
|
const userDataFields = {
|
|
|
|
[ 'dashboard_leaderboard_rows' ]: parseInt( rows ),
|
|
|
|
};
|
|
|
|
this.props.updateCurrentUserData( userDataFields );
|
|
|
|
};
|
|
|
|
|
|
|
|
renderMenu() {
|
|
|
|
const { hiddenLeaderboardKeys, rowsPerTable } = this.state;
|
2019-04-11 02:53:05 +00:00
|
|
|
const { allLeaderboards } = this.props;
|
|
|
|
|
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={ __(
|
|
|
|
'Choose which leaderboards to display and the number of rows',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-02-25 10:17:56 +00:00
|
|
|
>
|
2019-01-08 07:20:01 +00:00
|
|
|
<Fragment>
|
2019-03-13 17:14:02 +00:00
|
|
|
<MenuTitle>{ __( 'Leaderboards', 'woocommerce-admin' ) }</MenuTitle>
|
2019-01-08 07:20:01 +00:00
|
|
|
{ allLeaderboards.map( leaderboard => {
|
|
|
|
return (
|
2019-02-25 19:25:19 +00:00
|
|
|
<MenuItem
|
2019-04-11 02:53:05 +00:00
|
|
|
checked={ ! hiddenLeaderboardKeys.includes( leaderboard.id ) }
|
2019-02-25 19:25:19 +00:00
|
|
|
isCheckbox
|
|
|
|
isClickable
|
2019-04-11 02:53:05 +00:00
|
|
|
key={ leaderboard.id }
|
|
|
|
onInvoke={ this.toggle( leaderboard.id ) }
|
2019-02-25 19:25:19 +00:00
|
|
|
>
|
|
|
|
{ leaderboard.label }
|
2019-01-08 07:20:01 +00:00
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
<SelectControl
|
2019-02-25 10:17:56 +00:00
|
|
|
className="woocommerce-dashboard__dashboard-leaderboards__select"
|
2019-03-13 17:14:02 +00:00
|
|
|
label={ <MenuTitle>{ __( 'Rows Per Table', 'woocommerce-admin' ) }</MenuTitle> }
|
2019-01-08 07:20:01 +00:00
|
|
|
value={ rowsPerTable }
|
|
|
|
options={ Array.from( { length: 20 }, ( v, key ) => ( {
|
|
|
|
v: key + 1,
|
|
|
|
label: key + 1,
|
|
|
|
} ) ) }
|
|
|
|
onChange={ this.setRowsPerTable }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
</EllipsisMenu>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-11 02:53:05 +00:00
|
|
|
renderLeaderboards() {
|
2019-01-08 07:20:01 +00:00
|
|
|
const { hiddenLeaderboardKeys, rowsPerTable } = this.state;
|
2019-04-11 02:53:05 +00:00
|
|
|
const { allLeaderboards, query } = this.props;
|
|
|
|
|
|
|
|
return allLeaderboards.map( leaderboard => {
|
|
|
|
if ( hiddenLeaderboardKeys.includes( leaderboard.id ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Leaderboard
|
|
|
|
headers={ leaderboard.headers }
|
|
|
|
id={ leaderboard.id }
|
|
|
|
key={ leaderboard.id }
|
|
|
|
query={ query }
|
|
|
|
title={ leaderboard.label }
|
|
|
|
totalRows={ rowsPerTable }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
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
|
|
|
|
title={ __( 'Leaderboards', 'woocommerce-admin' ) }
|
|
|
|
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
|
|
|
userPrefLeaderboards: userData.dashboard_leaderboards,
|
|
|
|
userPrefLeaderboardRows: userData.dashboard_leaderboard_rows,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
|
|
|
const { updateCurrentUserData } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
updateCurrentUserData,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Leaderboards );
|