2020-05-14 03:23:03 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import moment from 'moment';
|
|
|
|
import { find } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { getCurrentDates, appendTimestamp } from '@woocommerce/date';
|
|
|
|
import { SETTINGS_STORE_NAME } from '@woocommerce/data';
|
|
|
|
import { getNewPath } from '@woocommerce/navigation';
|
|
|
|
import { calculateDelta, formatValue } from '@woocommerce/number';
|
2020-06-04 23:14:55 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/wc-admin-settings';
|
2020-07-20 22:17:28 +00:00
|
|
|
import { getFilterQuery } from 'wc-api/reports/utils';
|
2020-05-14 03:23:03 +00:00
|
|
|
|
2020-06-04 23:14:55 +00:00
|
|
|
function getReportUrl( href, persistedQuery, primaryItem ) {
|
|
|
|
if ( ! href ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( href === '/jetpack' ) {
|
|
|
|
return getAdminLink( 'admin.php?page=jetpack#/dashboard' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return getNewPath( persistedQuery, href, {
|
|
|
|
chart: primaryItem.chart,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getIndicatorValues = ( {
|
2020-05-14 03:23:03 +00:00
|
|
|
indicator,
|
|
|
|
primaryData,
|
|
|
|
secondaryData,
|
|
|
|
currency,
|
2020-06-17 23:33:40 +00:00
|
|
|
formatAmount,
|
2020-05-14 03:23:03 +00:00
|
|
|
persistedQuery,
|
|
|
|
} ) => {
|
|
|
|
const primaryItem = find(
|
|
|
|
primaryData.data,
|
|
|
|
( data ) => data.stat === indicator.stat
|
|
|
|
);
|
|
|
|
const secondaryItem = find(
|
|
|
|
secondaryData.data,
|
|
|
|
( data ) => data.stat === indicator.stat
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! primaryItem || ! secondaryItem ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const href =
|
|
|
|
( primaryItem._links &&
|
|
|
|
primaryItem._links.report[ 0 ] &&
|
|
|
|
primaryItem._links.report[ 0 ].href ) ||
|
|
|
|
'';
|
2020-06-04 23:14:55 +00:00
|
|
|
const reportUrl = getReportUrl( href, persistedQuery, primaryItem );
|
|
|
|
const reportUrlType = href === '/jetpack' ? 'wp-admin' : 'wc-admin';
|
2020-05-14 03:23:03 +00:00
|
|
|
const isCurrency = primaryItem.format === 'currency';
|
|
|
|
|
|
|
|
const delta = calculateDelta( primaryItem.value, secondaryItem.value );
|
|
|
|
const primaryValue = isCurrency
|
2020-06-17 23:33:40 +00:00
|
|
|
? formatAmount( primaryItem.value )
|
2020-05-14 03:23:03 +00:00
|
|
|
: formatValue( currency, primaryItem.format, primaryItem.value );
|
|
|
|
const secondaryValue = isCurrency
|
2020-06-17 23:33:40 +00:00
|
|
|
? formatAmount( secondaryItem.value )
|
2020-05-14 03:23:03 +00:00
|
|
|
: formatValue( currency, secondaryItem.format, secondaryItem.value );
|
|
|
|
return {
|
|
|
|
primaryValue,
|
|
|
|
secondaryValue,
|
|
|
|
delta,
|
|
|
|
reportUrl,
|
2020-06-04 23:14:55 +00:00
|
|
|
reportUrlType,
|
2020-05-14 03:23:03 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-07-20 22:17:28 +00:00
|
|
|
export const getIndicatorData = ( select, indicators, query, filters ) => {
|
2020-05-14 03:23:03 +00:00
|
|
|
const {
|
|
|
|
getReportItems,
|
|
|
|
getReportItemsError,
|
|
|
|
isReportItemsRequesting,
|
|
|
|
} = select( 'wc-api' );
|
|
|
|
const { woocommerce_default_date_range: defaultDateRange } = select(
|
|
|
|
SETTINGS_STORE_NAME
|
|
|
|
).getSetting( 'wc_admin', 'wcAdminSettings' );
|
|
|
|
const datesFromQuery = getCurrentDates( query, defaultDateRange );
|
|
|
|
const endPrimary = datesFromQuery.primary.before;
|
|
|
|
const endSecondary = datesFromQuery.secondary.before;
|
|
|
|
const statKeys = indicators
|
|
|
|
.map( ( indicator ) => indicator.stat )
|
|
|
|
.join( ',' );
|
2020-07-20 22:17:28 +00:00
|
|
|
const filterQuery = getFilterQuery( { filters, query } );
|
2020-05-14 03:23:03 +00:00
|
|
|
const primaryQuery = {
|
2020-07-20 22:17:28 +00:00
|
|
|
...filterQuery,
|
2020-05-14 03:23:03 +00:00
|
|
|
after: appendTimestamp( datesFromQuery.primary.after, 'start' ),
|
|
|
|
before: appendTimestamp(
|
|
|
|
endPrimary,
|
|
|
|
endPrimary.isSame( moment(), 'day' ) ? 'now' : 'end'
|
|
|
|
),
|
|
|
|
stats: statKeys,
|
|
|
|
};
|
|
|
|
|
|
|
|
const secondaryQuery = {
|
2020-07-20 22:17:28 +00:00
|
|
|
...filterQuery,
|
2020-05-14 03:23:03 +00:00
|
|
|
after: appendTimestamp( datesFromQuery.secondary.after, 'start' ),
|
|
|
|
before: appendTimestamp(
|
|
|
|
endSecondary,
|
|
|
|
endSecondary.isSame( moment(), 'day' ) ? 'now' : 'end'
|
|
|
|
),
|
|
|
|
stats: statKeys,
|
|
|
|
};
|
|
|
|
|
|
|
|
const primaryData = getReportItems(
|
|
|
|
'performance-indicators',
|
|
|
|
primaryQuery
|
|
|
|
);
|
|
|
|
const primaryError =
|
|
|
|
getReportItemsError( 'performance-indicators', primaryQuery ) || null;
|
|
|
|
const primaryRequesting = isReportItemsRequesting(
|
|
|
|
'performance-indicators',
|
|
|
|
primaryQuery
|
|
|
|
);
|
|
|
|
|
|
|
|
const secondaryData = getReportItems(
|
|
|
|
'performance-indicators',
|
|
|
|
secondaryQuery
|
|
|
|
);
|
|
|
|
const secondaryError =
|
|
|
|
getReportItemsError( 'performance-indicators', secondaryQuery ) || null;
|
|
|
|
const secondaryRequesting = isReportItemsRequesting(
|
|
|
|
'performance-indicators',
|
|
|
|
secondaryQuery
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
primaryData,
|
|
|
|
primaryError,
|
|
|
|
primaryRequesting,
|
|
|
|
secondaryData,
|
|
|
|
secondaryError,
|
|
|
|
secondaryRequesting,
|
|
|
|
defaultDateRange,
|
|
|
|
};
|
|
|
|
};
|