2019-05-20 20:50:25 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* 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-05-20 20:50:25 +00:00
|
|
|
|
2019-07-02 20:50:59 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { SectionHeader } from '@woocommerce/components';
|
|
|
|
|
2019-05-20 20:50:25 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-06-11 12:47:53 +00:00
|
|
|
import { DEFAULT_REQUIREMENT } from 'wc-api/constants';
|
|
|
|
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';
|
|
|
|
import withSelect from 'wc-api/with-select';
|
|
|
|
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>
|
2019-07-02 20:50:59 +00:00
|
|
|
<SectionHeader title={ __( 'Import Historical Data', 'woocommerce-admin' ) } />
|
|
|
|
<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
|
|
|
|
label={ __( 'Registered Customers', 'woocommerce-admin' ) }
|
|
|
|
progress={ customersProgress }
|
|
|
|
total={ customersTotal }
|
|
|
|
/>
|
|
|
|
<HistoricalDataProgress
|
2019-07-09 12:53:41 +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
|
|
|
) }
|
2019-07-02 20:50:59 +00:00
|
|
|
<HistoricalDataStatus importDate={ importDate } status={ status } />
|
|
|
|
</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 ) => {
|
2019-06-11 12:47:53 +00:00
|
|
|
const { getImportStatus, isGetImportStatusRequesting, getImportTotals } = select( 'wc-api' );
|
|
|
|
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
|
|
|
|
const { customers, orders } = getImportTotals( params, lastImportStopTimestamp );
|
|
|
|
const requirement = inProgress
|
|
|
|
? {
|
|
|
|
freshness: 3 * SECOND,
|
|
|
|
timeout: 3 * SECOND,
|
|
|
|
}
|
|
|
|
: 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-01-02 16:00:37 +00:00
|
|
|
const { imported: customersProgress, total: customersTotal } = customersStatus || {};
|
|
|
|
const { imported: ordersProgress, total: ordersTotal } = ordersStatus || {};
|
2019-06-11 12:47:53 +00:00
|
|
|
const isStatusLoading = isGetImportStatusRequesting( lastImportStartTimestamp );
|
|
|
|
|
|
|
|
const hasImportStarted = Boolean(
|
|
|
|
! lastImportStartTimestamp && ! isStatusLoading && ! inProgress && isImporting === true
|
|
|
|
);
|
|
|
|
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 );
|