2019-05-07 07:18:48 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-05-09 09:11:58 +00:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2019-05-07 07:18:48 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
2019-05-09 09:11:58 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { stringifyQuery } from '@woocommerce/navigation';
|
|
|
|
|
2019-05-07 07:18:48 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
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 HistoricalData extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
|
|
|
|
this.dateFormat = __( 'MM/DD/YYYY', 'woocommerce-admin' );
|
|
|
|
|
|
|
|
this.state = {
|
2019-05-10 09:34:03 +00:00
|
|
|
inProgress: false,
|
2019-05-07 07:18:48 +00:00
|
|
|
period: {
|
|
|
|
date: moment().format( this.dateFormat ),
|
|
|
|
label: 'all',
|
|
|
|
},
|
|
|
|
skipChecked: true,
|
|
|
|
};
|
|
|
|
|
2019-05-09 09:11:58 +00:00
|
|
|
this.makeQuery = this.makeQuery.bind( this );
|
|
|
|
this.onDeletePreviousData = this.onDeletePreviousData.bind( this );
|
|
|
|
this.onStartImport = this.onStartImport.bind( this );
|
|
|
|
this.onStopImport = this.onStopImport.bind( this );
|
2019-05-07 07:18:48 +00:00
|
|
|
this.onDateChange = this.onDateChange.bind( this );
|
|
|
|
this.onPeriodChange = this.onPeriodChange.bind( this );
|
|
|
|
this.onSkipChange = this.onSkipChange.bind( this );
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:11:58 +00:00
|
|
|
makeQuery( path, errorMessage ) {
|
|
|
|
const { addNotice } = this.props;
|
|
|
|
apiFetch( { path, method: 'POST' } )
|
|
|
|
.then( response => {
|
|
|
|
if ( 'success' === response.status ) {
|
|
|
|
addNotice( { status: 'success', message: response.message } );
|
|
|
|
} else {
|
|
|
|
addNotice( { status: 'error', message: errorMessage } );
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.catch( error => {
|
|
|
|
if ( error && error.message ) {
|
|
|
|
addNotice( { status: 'error', message: error.message } );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
onDeletePreviousData() {
|
|
|
|
const path = '/wc/v4/reports/import/delete';
|
|
|
|
const errorMessage = __(
|
|
|
|
'There was a problem deleting your previous data.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
this.makeQuery( path, errorMessage );
|
|
|
|
}
|
|
|
|
|
|
|
|
onStartImport() {
|
|
|
|
const { period, skipChecked } = this.state;
|
2019-05-10 09:34:03 +00:00
|
|
|
this.setState( {
|
|
|
|
inProgress: true,
|
|
|
|
} );
|
2019-05-09 09:11:58 +00:00
|
|
|
const params = {};
|
|
|
|
if ( skipChecked ) {
|
|
|
|
params.skip_existing = true;
|
|
|
|
}
|
|
|
|
if ( period.label !== 'all' ) {
|
|
|
|
if ( period.label === 'custom' ) {
|
|
|
|
const daysDifference = moment().diff(
|
|
|
|
moment( period.date, this.dateFormat ),
|
|
|
|
'days',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
params.days = Math.ceil( daysDifference );
|
|
|
|
} else {
|
|
|
|
params.days = parseInt( period.label, 10 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const path = '/wc/v4/reports/import' + stringifyQuery( params );
|
|
|
|
const errorMessage = __(
|
|
|
|
'There was a problem rebuilding your report data.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
this.makeQuery( path, errorMessage );
|
|
|
|
}
|
|
|
|
|
|
|
|
onStopImport() {
|
2019-05-10 09:34:03 +00:00
|
|
|
this.setState( {
|
|
|
|
inProgress: false,
|
|
|
|
} );
|
2019-05-09 09:11:58 +00:00
|
|
|
const path = '/wc/v4/reports/import/cancel';
|
|
|
|
const errorMessage = __(
|
|
|
|
'There was a problem stopping your current import.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
this.makeQuery( path, errorMessage );
|
|
|
|
}
|
|
|
|
|
2019-05-07 07:18:48 +00:00
|
|
|
onPeriodChange( val ) {
|
|
|
|
this.setState( {
|
|
|
|
period: {
|
|
|
|
...this.state.period,
|
|
|
|
label: val,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
onDateChange( val ) {
|
|
|
|
this.setState( {
|
|
|
|
period: {
|
|
|
|
date: val,
|
|
|
|
label: 'custom',
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
onSkipChange( val ) {
|
|
|
|
this.setState( {
|
|
|
|
skipChecked: val,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
getStatus() {
|
2019-05-10 09:34:03 +00:00
|
|
|
const { customersProgress, customersTotal, ordersProgress, ordersTotal } = this.props;
|
|
|
|
const { inProgress } = this.state;
|
2019-05-07 07:18:48 +00:00
|
|
|
|
|
|
|
if ( inProgress ) {
|
|
|
|
if ( customersProgress < customersTotal ) {
|
|
|
|
return 'customers';
|
|
|
|
}
|
|
|
|
if ( ordersProgress < ordersTotal ) {
|
|
|
|
return 'orders';
|
|
|
|
}
|
|
|
|
return 'finalizing';
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
( customersTotal > 0 || ordersTotal > 0 ) &&
|
|
|
|
customersProgress === customersTotal &&
|
|
|
|
ordersProgress === ordersTotal
|
|
|
|
) {
|
|
|
|
return 'finished';
|
|
|
|
}
|
|
|
|
return 'ready';
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
customersProgress,
|
|
|
|
customersTotal,
|
|
|
|
hasImportedData,
|
|
|
|
importDate,
|
|
|
|
ordersProgress,
|
|
|
|
ordersTotal,
|
|
|
|
} = this.props;
|
2019-05-10 09:34:03 +00:00
|
|
|
const { inProgress, period, skipChecked } = this.state;
|
2019-05-07 07:18:48 +00:00
|
|
|
const hasImportedAllData =
|
|
|
|
! inProgress &&
|
|
|
|
hasImportedData &&
|
|
|
|
customersProgress === customersTotal &&
|
|
|
|
ordersProgress === ordersTotal;
|
2019-05-10 09:34:03 +00:00
|
|
|
// @todo When the import status endpoint is hooked up,
|
|
|
|
// this bool should be removed and assume it's true.
|
|
|
|
const showImportStatus = false;
|
2019-05-07 07:18:48 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<div className="woocommerce-setting">
|
|
|
|
<div className="woocommerce-setting__label" id="import-historical-data-label">
|
|
|
|
{ __( 'Import Historical Data:', 'woocommerce-admin' ) }
|
|
|
|
</div>
|
|
|
|
<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>
|
|
|
|
{ ! hasImportedAllData && (
|
|
|
|
<Fragment>
|
|
|
|
<HistoricalDataPeriodSelector
|
|
|
|
dateFormat={ this.dateFormat }
|
|
|
|
disabled={ inProgress }
|
|
|
|
onPeriodChange={ this.onPeriodChange }
|
|
|
|
onDateChange={ this.onDateChange }
|
|
|
|
value={ period }
|
|
|
|
/>
|
|
|
|
<HistoricalDataSkipCheckbox
|
|
|
|
disabled={ inProgress }
|
|
|
|
checked={ skipChecked }
|
|
|
|
onChange={ this.onSkipChange }
|
|
|
|
/>
|
2019-05-10 09:34:03 +00:00
|
|
|
{ showImportStatus && (
|
|
|
|
<Fragment>
|
|
|
|
<HistoricalDataProgress
|
|
|
|
label={ __( 'Registered Customers', 'woocommerce-admin' ) }
|
|
|
|
progress={ customersProgress }
|
|
|
|
total={ customersTotal }
|
|
|
|
/>
|
|
|
|
<HistoricalDataProgress
|
|
|
|
label={ __( 'Orders', 'woocommerce-admin' ) }
|
|
|
|
progress={ ordersProgress }
|
|
|
|
total={ ordersTotal }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
) }
|
2019-05-07 07:18:48 +00:00
|
|
|
</Fragment>
|
|
|
|
) }
|
2019-05-10 09:34:03 +00:00
|
|
|
{ showImportStatus && (
|
|
|
|
<HistoricalDataStatus importDate={ importDate } status={ this.getStatus() } />
|
|
|
|
) }
|
2019-05-07 07:18:48 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<HistoricalDataActions
|
|
|
|
customersProgress={ customersProgress }
|
|
|
|
customersTotal={ customersTotal }
|
|
|
|
hasImportedData={ hasImportedData }
|
|
|
|
inProgress={ inProgress }
|
2019-05-09 09:11:58 +00:00
|
|
|
onDeletePreviousData={ this.onDeletePreviousData }
|
|
|
|
onStartImport={ this.onStartImport }
|
|
|
|
onStopImport={ this.onStopImport }
|
2019-05-07 07:18:48 +00:00
|
|
|
ordersProgress={ ordersProgress }
|
|
|
|
ordersTotal={ ordersTotal }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withSelect( () => {
|
|
|
|
return {
|
|
|
|
customersProgress: 0,
|
|
|
|
customersTotal: 0,
|
|
|
|
hasImportedData: false,
|
|
|
|
importDate: '2019-04-01',
|
|
|
|
ordersProgress: 0,
|
|
|
|
ordersTotal: 0,
|
|
|
|
};
|
|
|
|
} )( HistoricalData );
|