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
|
|
|
|
*/
|
|
|
|
import withSelect from 'wc-api/with-select';
|
2019-01-11 15:11:10 +00:00
|
|
|
import TopSellingCategories from './top-selling-categories';
|
2019-01-08 07:20:01 +00:00
|
|
|
import TopSellingProducts from './top-selling-products';
|
2019-01-25 17:59:53 +00:00
|
|
|
import TopCoupons from './top-coupons';
|
2019-01-08 07:20:01 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
class Leaderboards extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( ...arguments );
|
|
|
|
this.state = {
|
|
|
|
hiddenLeaderboardKeys: props.userPrefLeaderboards || [],
|
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;
|
|
|
|
const allLeaderboards = [
|
|
|
|
{
|
|
|
|
key: 'top-products',
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Top Products - Items Sold', 'woocommerce-admin' ),
|
2019-01-08 07:20:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'top-categories',
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Top Categories - Items Sold', 'woocommerce-admin' ),
|
2019-01-08 07:20:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'top-coupons',
|
2019-03-13 17:14:02 +00:00
|
|
|
label: __( 'Top Coupons', 'woocommerce-admin' ),
|
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
|
|
|
|
checked={ ! hiddenLeaderboardKeys.includes( leaderboard.key ) }
|
|
|
|
isCheckbox
|
|
|
|
isClickable
|
|
|
|
key={ leaderboard.key }
|
|
|
|
onInvoke={ this.toggle( leaderboard.key ) }
|
|
|
|
>
|
|
|
|
{ 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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { hiddenLeaderboardKeys, rowsPerTable } = this.state;
|
2019-01-11 15:11:10 +00:00
|
|
|
const { query } = 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
|
|
|
|
title={ __( 'Leaderboards', 'woocommerce-admin' ) }
|
|
|
|
menu={ this.renderMenu() }
|
|
|
|
/>
|
2019-01-08 07:20:01 +00:00
|
|
|
<div className="woocommerce-dashboard__columns">
|
2019-01-11 15:11:10 +00:00
|
|
|
{ ! hiddenLeaderboardKeys.includes( 'top-products' ) && (
|
|
|
|
<TopSellingProducts query={ query } totalRows={ rowsPerTable } />
|
|
|
|
) }
|
|
|
|
{ ! hiddenLeaderboardKeys.includes( 'top-categories' ) && (
|
|
|
|
<TopSellingCategories query={ query } totalRows={ rowsPerTable } />
|
|
|
|
) }
|
2019-01-25 17:59:53 +00:00
|
|
|
{ ! hiddenLeaderboardKeys.includes( 'top-coupons' ) && (
|
|
|
|
<TopCoupons query={ query } totalRows={ rowsPerTable } />
|
|
|
|
) }
|
2019-01-08 07:20:01 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Leaderboards.propTypes = {
|
|
|
|
query: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( select => {
|
|
|
|
const { getCurrentUserData } = select( 'wc-api' );
|
|
|
|
const userData = getCurrentUserData();
|
|
|
|
|
|
|
|
return {
|
|
|
|
userPrefLeaderboards: userData.dashboard_leaderboards,
|
|
|
|
userPrefLeaderboardRows: userData.dashboard_leaderboard_rows,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
|
|
|
const { updateCurrentUserData } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
updateCurrentUserData,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Leaderboards );
|