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-20 20:50:25 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2019-05-07 07:18:48 +00:00
|
|
|
import moment from 'moment';
|
2019-06-11 12:47:53 +00:00
|
|
|
import { withSpokenMessages } from '@wordpress/components';
|
2019-05-07 07:18:48 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2019-05-20 20:50:25 +00:00
|
|
|
import { formatParams } from './utils';
|
|
|
|
import HistoricalDataLayout from './layout';
|
2019-05-07 07:18:48 +00:00
|
|
|
|
|
|
|
class HistoricalData extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
|
|
|
|
this.dateFormat = __( 'MM/DD/YYYY', 'woocommerce-admin' );
|
|
|
|
|
|
|
|
this.state = {
|
2019-06-11 12:47:53 +00:00
|
|
|
// Whether there is an active import (which might have been stopped)
|
|
|
|
// that matches the period and skipChecked settings
|
|
|
|
activeImport: null,
|
|
|
|
lastImportStartTimestamp: 0,
|
|
|
|
lastImportStopTimestamp: 0,
|
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 );
|
2019-06-11 12:47:53 +00:00
|
|
|
this.onImportFinished = this.onImportFinished.bind( this );
|
|
|
|
this.onImportStarted = this.onImportStarted.bind( this );
|
2019-05-09 09:11:58 +00:00
|
|
|
this.onDeletePreviousData = this.onDeletePreviousData.bind( this );
|
2019-06-11 12:47:53 +00:00
|
|
|
this.onReimportData = this.onReimportData.bind( this );
|
2019-05-09 09:11:58 +00:00
|
|
|
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 ) {
|
2019-07-10 16:58:51 +00:00
|
|
|
const { addNotice } = this.props;
|
2019-05-09 09:11:58 +00:00
|
|
|
apiFetch( { path, method: 'POST' } )
|
|
|
|
.then( response => {
|
|
|
|
if ( 'success' === response.status ) {
|
2019-07-10 16:58:51 +00:00
|
|
|
addNotice( { status: 'success', message: response.message } );
|
2019-05-09 09:11:58 +00:00
|
|
|
} else {
|
2019-07-10 16:58:51 +00:00
|
|
|
addNotice( { status: 'error', message: errorMessage } );
|
2019-06-11 12:47:53 +00:00
|
|
|
this.setState( {
|
|
|
|
activeImport: false,
|
|
|
|
lastImportStopTimestamp: Date.now(),
|
|
|
|
} );
|
2019-05-09 09:11:58 +00:00
|
|
|
}
|
|
|
|
} )
|
|
|
|
.catch( error => {
|
|
|
|
if ( error && error.message ) {
|
2019-07-10 16:58:51 +00:00
|
|
|
addNotice( { status: 'error', message: error.message } );
|
2019-06-11 12:47:53 +00:00
|
|
|
this.setState( {
|
|
|
|
activeImport: false,
|
|
|
|
lastImportStopTimestamp: Date.now(),
|
|
|
|
} );
|
2019-05-09 09:11:58 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:47:53 +00:00
|
|
|
onImportFinished() {
|
|
|
|
const { debouncedSpeak } = this.props;
|
|
|
|
debouncedSpeak( 'Import complete' );
|
|
|
|
this.setState( {
|
|
|
|
lastImportStopTimestamp: Date.now(),
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
onImportStarted() {
|
|
|
|
this.setState( {
|
|
|
|
activeImport: true,
|
|
|
|
lastImportStartTimestamp: Date.now(),
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:11:58 +00:00
|
|
|
onDeletePreviousData() {
|
|
|
|
const path = '/wc/v4/reports/import/delete';
|
|
|
|
const errorMessage = __(
|
|
|
|
'There was a problem deleting your previous data.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
this.makeQuery( path, errorMessage );
|
2019-06-11 12:47:53 +00:00
|
|
|
this.setState( {
|
|
|
|
activeImport: false,
|
|
|
|
} );
|
2019-05-09 09:11:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 12:47:53 +00:00
|
|
|
onReimportData() {
|
2019-05-10 09:34:03 +00:00
|
|
|
this.setState( {
|
2019-06-11 12:47:53 +00:00
|
|
|
activeImport: false,
|
2019-05-10 09:34:03 +00:00
|
|
|
} );
|
2019-06-11 12:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onStartImport() {
|
|
|
|
const { period, skipChecked } = this.state;
|
|
|
|
const path =
|
|
|
|
'/wc/v4/reports/import' +
|
|
|
|
stringifyQuery( formatParams( this.dateFormat, period, skipChecked ) );
|
2019-05-09 09:11:58 +00:00
|
|
|
const errorMessage = __(
|
|
|
|
'There was a problem rebuilding your report data.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
this.makeQuery( path, errorMessage );
|
2019-06-11 12:47:53 +00:00
|
|
|
this.onImportStarted();
|
2019-05-09 09:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onStopImport() {
|
2019-05-10 09:34:03 +00:00
|
|
|
this.setState( {
|
2019-06-11 12:47:53 +00:00
|
|
|
lastImportStopTimestamp: Date.now(),
|
2019-05-10 09:34:03 +00:00
|
|
|
} );
|
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( {
|
2019-06-11 12:47:53 +00:00
|
|
|
activeImport: false,
|
2019-05-07 07:18:48 +00:00
|
|
|
period: {
|
|
|
|
...this.state.period,
|
|
|
|
label: val,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
onDateChange( val ) {
|
|
|
|
this.setState( {
|
2019-06-11 12:47:53 +00:00
|
|
|
activeImport: false,
|
2019-05-07 07:18:48 +00:00
|
|
|
period: {
|
|
|
|
date: val,
|
|
|
|
label: 'custom',
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
onSkipChange( val ) {
|
|
|
|
this.setState( {
|
2019-06-11 12:47:53 +00:00
|
|
|
activeImport: false,
|
2019-05-07 07:18:48 +00:00
|
|
|
skipChecked: val,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-06-11 12:47:53 +00:00
|
|
|
const {
|
|
|
|
activeImport,
|
|
|
|
lastImportStartTimestamp,
|
|
|
|
lastImportStopTimestamp,
|
|
|
|
period,
|
|
|
|
skipChecked,
|
|
|
|
} = this.state;
|
2019-05-07 07:18:48 +00:00
|
|
|
|
|
|
|
return (
|
2019-05-20 20:50:25 +00:00
|
|
|
<HistoricalDataLayout
|
2019-06-11 12:47:53 +00:00
|
|
|
activeImport={ activeImport }
|
2019-05-20 20:50:25 +00:00
|
|
|
dateFormat={ this.dateFormat }
|
2019-06-11 12:47:53 +00:00
|
|
|
onImportFinished={ this.onImportFinished }
|
|
|
|
onImportStarted={ this.onImportStarted }
|
|
|
|
lastImportStartTimestamp={ lastImportStartTimestamp }
|
|
|
|
lastImportStopTimestamp={ lastImportStopTimestamp }
|
2019-05-20 20:50:25 +00:00
|
|
|
onPeriodChange={ this.onPeriodChange }
|
|
|
|
onDateChange={ this.onDateChange }
|
|
|
|
onSkipChange={ this.onSkipChange }
|
|
|
|
onDeletePreviousData={ this.onDeletePreviousData }
|
2019-06-11 12:47:53 +00:00
|
|
|
onReimportData={ this.onReimportData }
|
2019-05-20 20:50:25 +00:00
|
|
|
onStartImport={ this.onStartImport }
|
|
|
|
onStopImport={ this.onStopImport }
|
|
|
|
period={ period }
|
|
|
|
skipChecked={ skipChecked }
|
|
|
|
/>
|
2019-05-07 07:18:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:47:53 +00:00
|
|
|
export default withSpokenMessages( HistoricalData );
|