2020-05-04 22:58:39 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Fragment } from '@wordpress/element';
|
2020-06-18 23:44:00 +00:00
|
|
|
import {
|
|
|
|
TabPanel,
|
|
|
|
Card,
|
|
|
|
CardHeader,
|
|
|
|
CardBody,
|
|
|
|
CardFooter,
|
|
|
|
__experimentalText as Text,
|
|
|
|
} from '@wordpress/components';
|
2020-06-10 16:46:46 +00:00
|
|
|
import { get, xor } from 'lodash';
|
2020-05-04 22:58:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
EllipsisMenu,
|
|
|
|
MenuItem,
|
|
|
|
MenuTitle,
|
2020-05-14 03:23:03 +00:00
|
|
|
Link,
|
2020-05-04 22:58:39 +00:00
|
|
|
} from '@woocommerce/components';
|
2020-06-10 16:46:46 +00:00
|
|
|
import { useUserPreferences } from '@woocommerce/data';
|
2020-05-04 22:58:39 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2020-05-14 03:23:03 +00:00
|
|
|
import { getNewPath } from '@woocommerce/navigation';
|
2020-05-04 22:58:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-05-14 03:23:03 +00:00
|
|
|
import './style.scss';
|
2020-05-04 22:58:39 +00:00
|
|
|
import { DEFAULT_STATS, DEFAULT_HIDDEN_STATS } from './defaults';
|
2020-05-14 03:23:03 +00:00
|
|
|
import StatsList from './stats-list';
|
|
|
|
import { recordEvent } from 'lib/tracks';
|
2020-05-27 02:38:57 +00:00
|
|
|
import InstallJetpackCta from './install-jetpack-cta';
|
2020-05-04 22:58:39 +00:00
|
|
|
|
|
|
|
const { performanceIndicators } = getSetting( 'dataEndpoints', {
|
|
|
|
performanceIndicators: [],
|
|
|
|
} );
|
|
|
|
const stats = performanceIndicators.filter( ( indicator ) => {
|
|
|
|
return DEFAULT_STATS.includes( indicator.stat );
|
|
|
|
} );
|
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
export const StatsOverview = () => {
|
|
|
|
const { updateUserPreferences, ...userPrefs } = useUserPreferences();
|
2020-06-18 23:44:00 +00:00
|
|
|
const hiddenStats = get(
|
|
|
|
userPrefs,
|
|
|
|
[ 'homepage_stats', 'hiddenStats' ],
|
|
|
|
DEFAULT_HIDDEN_STATS
|
|
|
|
);
|
2020-05-04 22:58:39 +00:00
|
|
|
|
|
|
|
const toggleStat = ( stat ) => {
|
|
|
|
const nextHiddenStats = xor( hiddenStats, [ stat ] );
|
2020-06-10 16:46:46 +00:00
|
|
|
updateUserPreferences( {
|
|
|
|
homepage_stats: { hiddenStats: nextHiddenStats },
|
2020-05-04 22:58:39 +00:00
|
|
|
} );
|
|
|
|
recordEvent( 'statsoverview_indicators_toggle', {
|
|
|
|
indicator_name: stat,
|
|
|
|
status: nextHiddenStats.includes( stat ) ? 'off' : 'on',
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2020-05-14 03:23:03 +00:00
|
|
|
const activeStats = stats.filter(
|
|
|
|
( item ) => ! hiddenStats.includes( item.stat )
|
|
|
|
);
|
|
|
|
|
2020-05-04 22:58:39 +00:00
|
|
|
return (
|
|
|
|
<Card
|
2020-06-18 23:44:00 +00:00
|
|
|
size="large"
|
|
|
|
className="woocommerce-stats-overview woocommerce-homescreen-card"
|
|
|
|
>
|
|
|
|
<CardHeader size="medium">
|
|
|
|
<Text variant="title.small">
|
|
|
|
{ __( 'Stats overview', 'woocommerce-admin' ) }
|
|
|
|
</Text>
|
2020-05-04 22:58:39 +00:00
|
|
|
<EllipsisMenu
|
|
|
|
label={ __(
|
|
|
|
'Choose which values to display',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
renderContent={ () => (
|
|
|
|
<Fragment>
|
|
|
|
<MenuTitle>
|
|
|
|
{ __( 'Display stats:', 'woocommerce-admin' ) }
|
|
|
|
</MenuTitle>
|
|
|
|
{ stats.map( ( item ) => {
|
|
|
|
const checked = ! hiddenStats.includes(
|
|
|
|
item.stat
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<MenuItem
|
|
|
|
checked={ checked }
|
|
|
|
isCheckbox
|
|
|
|
isClickable
|
|
|
|
key={ item.stat }
|
|
|
|
onInvoke={ () =>
|
|
|
|
toggleStat( item.stat )
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{ item.label }
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
/>
|
2020-06-18 23:44:00 +00:00
|
|
|
</CardHeader>
|
|
|
|
<CardBody>
|
|
|
|
<TabPanel
|
|
|
|
className="woocommerce-stats-overview__tabs"
|
|
|
|
onSelect={ ( period ) => {
|
|
|
|
recordEvent( 'statsoverview_date_picker_update', {
|
|
|
|
period,
|
|
|
|
} );
|
|
|
|
} }
|
|
|
|
tabs={ [
|
|
|
|
{
|
|
|
|
title: __( 'Today', 'woocommerce-admin' ),
|
|
|
|
name: 'today',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: __( 'Week to date', 'woocommerce-admin' ),
|
|
|
|
name: 'week',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: __( 'Month to date', 'woocommerce-admin' ),
|
|
|
|
name: 'month',
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
>
|
|
|
|
{ ( tab ) => (
|
|
|
|
<Fragment>
|
|
|
|
<InstallJetpackCta />
|
|
|
|
<StatsList
|
|
|
|
query={ {
|
|
|
|
period: tab.name,
|
|
|
|
compare: 'previous_period',
|
|
|
|
} }
|
|
|
|
stats={ activeStats }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
</TabPanel>
|
|
|
|
</CardBody>
|
|
|
|
<CardFooter>
|
2020-05-20 22:15:18 +00:00
|
|
|
<Link
|
|
|
|
className="woocommerce-stats-overview__more-btn"
|
|
|
|
href={ getNewPath( {}, '/analytics/overview' ) }
|
|
|
|
type="wc-admin"
|
|
|
|
onClick={ () => {
|
|
|
|
recordEvent( 'statsoverview_indicators_click', {
|
|
|
|
key: 'view_detailed_stats',
|
|
|
|
} );
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ __( 'View detailed stats' ) }
|
|
|
|
</Link>
|
2020-06-18 23:44:00 +00:00
|
|
|
</CardFooter>
|
2020-05-04 22:58:39 +00:00
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
export default StatsOverview;
|