2019-01-08 07:20:01 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-02-14 02:23:21 +00:00
|
|
|
import {
|
|
|
|
EllipsisMenu,
|
|
|
|
MenuItem,
|
|
|
|
MenuTitle,
|
|
|
|
SectionHeader,
|
|
|
|
} from '@woocommerce/components';
|
2019-09-23 21:47:08 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2019-01-08 07:20:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = {
|
2020-02-14 02:23:21 +00:00
|
|
|
rowsPerTable: parseInt( props.userPrefLeaderboardRows, 10 ) || 5,
|
2019-01-08 07:20:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
setRowsPerTable = ( rows ) => {
|
|
|
|
this.setState( { rowsPerTable: 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
|
|
|
};
|
|
|
|
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>
|
2020-02-14 02:23:21 +00:00
|
|
|
<MenuTitle>
|
|
|
|
{ __( 'Leaderboards', 'woocommerce-admin' ) }
|
|
|
|
</MenuTitle>
|
|
|
|
{ allLeaderboards.map( ( leaderboard ) => {
|
|
|
|
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 )();
|
2020-02-14 02:23:21 +00:00
|
|
|
recordEvent(
|
|
|
|
'dash_leaderboards_toggle',
|
|
|
|
{
|
|
|
|
status: checked ? 'off' : 'on',
|
|
|
|
key: leaderboard.id,
|
|
|
|
}
|
|
|
|
);
|
2019-07-01 10:16:12 +00:00
|
|
|
} }
|
2019-04-30 00:35:37 +00:00
|
|
|
>
|
|
|
|
{ leaderboard.label }
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
<SelectControl
|
|
|
|
className="woocommerce-dashboard__dashboard-leaderboards__select"
|
2020-02-14 02:23:21 +00:00
|
|
|
label={ __(
|
|
|
|
'Rows Per Table',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-04-30 00:35:37 +00:00
|
|
|
value={ rowsPerTable }
|
2020-02-14 02:23:21 +00:00
|
|
|
options={ Array.from(
|
|
|
|
{ length: 20 },
|
|
|
|
( v, key ) => ( {
|
|
|
|
v: key + 1,
|
|
|
|
label: key + 1,
|
|
|
|
} )
|
|
|
|
) }
|
2019-04-30 00:35:37 +00:00
|
|
|
onChange={ this.setRowsPerTable }
|
|
|
|
/>
|
2020-02-14 02:23:21 +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
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return allLeaderboards.map( ( leaderboard ) => {
|
2019-05-07 07:21:34 +00:00
|
|
|
if ( hiddenBlocks.includes( leaderboard.id ) ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
return undefined;
|
2019-04-11 02:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-02-14 02:23:21 +00:00
|
|
|
title={
|
|
|
|
title || __( 'Leaderboards', 'woocommerce-admin' )
|
|
|
|
}
|
2019-03-13 17:14:02 +00:00
|
|
|
menu={ this.renderMenu() }
|
|
|
|
/>
|
2020-02-14 02:23:21 +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(
|
2020-02-14 02:23:21 +00:00
|
|
|
withSelect( ( select ) => {
|
|
|
|
const {
|
|
|
|
getCurrentUserData,
|
|
|
|
getItems,
|
|
|
|
getItemsError,
|
|
|
|
isGetItemsRequesting,
|
|
|
|
} = select( 'wc-api' );
|
2019-01-08 07:20:01 +00:00
|
|
|
const userData = getCurrentUserData();
|
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,
|
|
|
|
isGetItemsRequesting,
|
2019-01-08 07:20:01 +00:00
|
|
|
userPrefLeaderboardRows: userData.dashboard_leaderboard_rows,
|
|
|
|
};
|
|
|
|
} ),
|
2020-02-14 02:23:21 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2019-01-08 07:20:01 +00:00
|
|
|
const { updateCurrentUserData } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
updateCurrentUserData,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Leaderboards );
|