2019-05-20 20:50:25 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2019-06-11 12:47:53 +00:00
|
|
|
import { isNil } from 'lodash';
|
|
|
|
import { SECOND } from '@fresh-data/framework';
|
2019-07-02 20:50:59 +00:00
|
|
|
import { SectionHeader } from '@woocommerce/components';
|
|
|
|
|
2019-05-20 20:50:25 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import { DEFAULT_REQUIREMENT } from '../../../wc-api/constants';
|
2019-06-11 12:47:53 +00:00
|
|
|
import { formatParams, getStatus } from './utils';
|
2019-05-20 20:50:25 +00:00
|
|
|
import HistoricalDataActions from './actions';
|
|
|
|
import HistoricalDataPeriodSelector from './period-selector';
|
|
|
|
import HistoricalDataProgress from './progress';
|
|
|
|
import HistoricalDataStatus from './status';
|
|
|
|
import HistoricalDataSkipCheckbox from './skip-checkbox';
|
2020-08-13 02:05:22 +00:00
|
|
|
import withSelect from '../../../wc-api/with-select';
|
2019-05-20 20:50:25 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
class HistoricalDataLayout extends Component {
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
customersProgress,
|
|
|
|
customersTotal,
|
|
|
|
dateFormat,
|
|
|
|
importDate,
|
|
|
|
inProgress,
|
|
|
|
onPeriodChange,
|
|
|
|
onDateChange,
|
|
|
|
onSkipChange,
|
|
|
|
onDeletePreviousData,
|
2019-06-11 12:47:53 +00:00
|
|
|
onReimportData,
|
2019-05-20 20:50:25 +00:00
|
|
|
onStartImport,
|
|
|
|
onStopImport,
|
|
|
|
ordersProgress,
|
|
|
|
ordersTotal,
|
|
|
|
period,
|
|
|
|
skipChecked,
|
|
|
|
} = this.props;
|
2019-06-11 12:47:53 +00:00
|
|
|
const status = getStatus( {
|
|
|
|
customersProgress,
|
|
|
|
customersTotal,
|
|
|
|
inProgress,
|
|
|
|
ordersProgress,
|
|
|
|
ordersTotal,
|
|
|
|
} );
|
2019-05-20 20:50:25 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
2020-02-14 02:23:21 +00:00
|
|
|
<SectionHeader
|
|
|
|
title={ __(
|
|
|
|
'Import Historical Data',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
/>
|
2019-07-02 20:50:59 +00:00
|
|
|
<div className="woocommerce-settings__wrapper">
|
|
|
|
<div className="woocommerce-setting">
|
|
|
|
<div className="woocommerce-setting__input">
|
|
|
|
<span className="woocommerce-setting__help">
|
|
|
|
{ __(
|
|
|
|
'This tool populates historical analytics data by processing customers ' +
|
|
|
|
'and orders created prior to activating WooCommerce Admin.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</span>
|
|
|
|
{ status !== 'finished' && (
|
|
|
|
<Fragment>
|
|
|
|
<HistoricalDataPeriodSelector
|
|
|
|
dateFormat={ dateFormat }
|
|
|
|
disabled={ inProgress }
|
|
|
|
onPeriodChange={ onPeriodChange }
|
|
|
|
onDateChange={ onDateChange }
|
|
|
|
value={ period }
|
|
|
|
/>
|
|
|
|
<HistoricalDataSkipCheckbox
|
|
|
|
disabled={ inProgress }
|
|
|
|
checked={ skipChecked }
|
|
|
|
onChange={ onSkipChange }
|
|
|
|
/>
|
|
|
|
<HistoricalDataProgress
|
2020-02-14 02:23:21 +00:00
|
|
|
label={ __(
|
|
|
|
'Registered Customers',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-07-02 20:50:59 +00:00
|
|
|
progress={ customersProgress }
|
|
|
|
total={ customersTotal }
|
|
|
|
/>
|
|
|
|
<HistoricalDataProgress
|
2020-02-14 02:23:21 +00:00
|
|
|
label={ __(
|
|
|
|
'Orders and Refunds',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-07-02 20:50:59 +00:00
|
|
|
progress={ ordersProgress }
|
|
|
|
total={ ordersTotal }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
2019-05-20 20:50:25 +00:00
|
|
|
) }
|
2020-02-14 02:23:21 +00:00
|
|
|
<HistoricalDataStatus
|
|
|
|
importDate={ importDate }
|
|
|
|
status={ status }
|
|
|
|
/>
|
2019-07-02 20:50:59 +00:00
|
|
|
</div>
|
2019-05-20 20:50:25 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<HistoricalDataActions
|
2019-06-11 12:47:53 +00:00
|
|
|
importDate={ importDate }
|
2019-05-20 20:50:25 +00:00
|
|
|
onDeletePreviousData={ onDeletePreviousData }
|
2019-06-11 12:47:53 +00:00
|
|
|
onReimportData={ onReimportData }
|
2019-05-20 20:50:25 +00:00
|
|
|
onStartImport={ onStartImport }
|
|
|
|
onStopImport={ onStopImport }
|
2019-06-11 12:47:53 +00:00
|
|
|
status={ status }
|
2019-05-20 20:50:25 +00:00
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withSelect( ( select, props ) => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const {
|
|
|
|
getImportStatus,
|
|
|
|
isGetImportStatusRequesting,
|
|
|
|
getImportTotals,
|
|
|
|
} = select( 'wc-api' );
|
2019-06-11 12:47:53 +00:00
|
|
|
const {
|
|
|
|
activeImport,
|
|
|
|
dateFormat,
|
|
|
|
lastImportStartTimestamp,
|
|
|
|
lastImportStopTimestamp,
|
|
|
|
onImportStarted,
|
|
|
|
onImportFinished,
|
|
|
|
period,
|
|
|
|
skipChecked,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const inProgress =
|
|
|
|
( typeof lastImportStartTimestamp !== 'undefined' &&
|
|
|
|
typeof lastImportStopTimestamp === 'undefined' ) ||
|
|
|
|
lastImportStartTimestamp > lastImportStopTimestamp;
|
|
|
|
|
|
|
|
const params = formatParams( dateFormat, period, skipChecked );
|
|
|
|
// Use timestamp to invalidate previous totals when the import finished/stopped
|
2020-02-14 02:23:21 +00:00
|
|
|
const { customers, orders } = getImportTotals(
|
|
|
|
params,
|
|
|
|
lastImportStopTimestamp
|
|
|
|
);
|
2019-06-11 12:47:53 +00:00
|
|
|
const requirement = inProgress
|
|
|
|
? {
|
|
|
|
freshness: 3 * SECOND,
|
|
|
|
timeout: 3 * SECOND,
|
2020-02-14 02:23:21 +00:00
|
|
|
}
|
2019-06-11 12:47:53 +00:00
|
|
|
: DEFAULT_REQUIREMENT;
|
2019-05-20 20:50:25 +00:00
|
|
|
|
2019-06-11 12:47:53 +00:00
|
|
|
// Use timestamp to invalidate previous status when a new import starts
|
|
|
|
const {
|
2020-01-02 16:00:37 +00:00
|
|
|
customers: customersStatus,
|
2019-06-11 12:47:53 +00:00
|
|
|
imported_from: importDate,
|
|
|
|
is_importing: isImporting,
|
2020-01-02 16:00:37 +00:00
|
|
|
orders: ordersStatus,
|
2019-06-11 12:47:53 +00:00
|
|
|
} = getImportStatus( lastImportStartTimestamp, requirement );
|
2020-02-14 02:23:21 +00:00
|
|
|
const { imported: customersProgress, total: customersTotal } =
|
|
|
|
customersStatus || {};
|
2020-01-02 16:00:37 +00:00
|
|
|
const { imported: ordersProgress, total: ordersTotal } = ordersStatus || {};
|
2020-02-14 02:23:21 +00:00
|
|
|
const isStatusLoading = isGetImportStatusRequesting(
|
|
|
|
lastImportStartTimestamp
|
|
|
|
);
|
2019-06-11 12:47:53 +00:00
|
|
|
|
|
|
|
const hasImportStarted = Boolean(
|
2020-02-14 02:23:21 +00:00
|
|
|
! lastImportStartTimestamp &&
|
|
|
|
! isStatusLoading &&
|
|
|
|
! inProgress &&
|
|
|
|
isImporting === true
|
2019-06-11 12:47:53 +00:00
|
|
|
);
|
|
|
|
if ( hasImportStarted ) {
|
|
|
|
onImportStarted();
|
|
|
|
}
|
|
|
|
const hasImportFinished = Boolean(
|
|
|
|
! isStatusLoading &&
|
|
|
|
inProgress &&
|
|
|
|
isImporting === false &&
|
|
|
|
( ( customersProgress === customersTotal && customersTotal > 0 ) ||
|
|
|
|
( ordersProgress === ordersTotal && ordersTotal > 0 ) )
|
2019-05-20 20:50:25 +00:00
|
|
|
);
|
2019-06-11 12:47:53 +00:00
|
|
|
if ( hasImportFinished ) {
|
|
|
|
onImportFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! activeImport ) {
|
|
|
|
return {
|
|
|
|
customersTotal: customers,
|
|
|
|
importDate,
|
|
|
|
ordersTotal: orders,
|
|
|
|
};
|
|
|
|
}
|
2019-05-20 20:50:25 +00:00
|
|
|
|
|
|
|
return {
|
2019-06-11 12:47:53 +00:00
|
|
|
customersProgress,
|
|
|
|
customersTotal: isNil( customersTotal ) ? customers : customersTotal,
|
|
|
|
importDate,
|
|
|
|
inProgress,
|
|
|
|
ordersProgress,
|
|
|
|
ordersTotal: isNil( ordersTotal ) ? orders : ordersTotal,
|
2019-05-20 20:50:25 +00:00
|
|
|
};
|
|
|
|
} )( HistoricalDataLayout );
|