2019-05-07 07:18:48 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-07-16 08:56:24 +00:00
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
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';
|
2020-01-09 02:11:39 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2019-05-07 07:18:48 +00:00
|
|
|
import moment from 'moment';
|
2020-08-13 13:23:38 +00:00
|
|
|
import { NOTES_STORE_NAME } from '@woocommerce/data';
|
2020-01-09 02:11:39 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
2019-06-11 12:47:53 +00:00
|
|
|
import { withSpokenMessages } from '@wordpress/components';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
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';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { QUERY_DEFAULTS } from '../../../wc-api/constants';
|
|
|
|
import withSelect from '../../../wc-api/with-select';
|
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-23 03:26:46 +00:00
|
|
|
const { createNotice } = this.props;
|
2019-05-09 09:11:58 +00:00
|
|
|
apiFetch( { path, method: 'POST' } )
|
2020-02-14 02:23:21 +00:00
|
|
|
.then( ( response ) => {
|
|
|
|
if ( response.status === 'success' ) {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice( 'success', response.message );
|
2019-05-09 09:11:58 +00:00
|
|
|
} else {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice( 'error', errorMessage );
|
2019-06-11 12:47:53 +00:00
|
|
|
this.setState( {
|
|
|
|
activeImport: false,
|
|
|
|
lastImportStopTimestamp: Date.now(),
|
|
|
|
} );
|
2019-05-09 09:11:58 +00:00
|
|
|
}
|
|
|
|
} )
|
2020-02-14 02:23:21 +00:00
|
|
|
.catch( ( error ) => {
|
2019-05-09 09:11:58 +00:00
|
|
|
if ( error && error.message ) {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice( 'error', 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() {
|
2020-01-09 02:11:39 +00:00
|
|
|
const { notes, updateNote } = this.props;
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const historicalDataNote = notes.find(
|
|
|
|
( note ) => note.name === 'wc-admin-historical-data'
|
|
|
|
);
|
2020-01-09 02:11:39 +00:00
|
|
|
if ( historicalDataNote ) {
|
|
|
|
updateNote( historicalDataNote.id, { status: 'actioned' } );
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:47:53 +00:00
|
|
|
this.setState( {
|
|
|
|
activeImport: true,
|
|
|
|
lastImportStartTimestamp: Date.now(),
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:11:58 +00:00
|
|
|
onDeletePreviousData() {
|
2019-11-12 18:15:55 +00:00
|
|
|
const path = '/wc-analytics/reports/import/delete';
|
2019-05-09 09:11:58 +00:00
|
|
|
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-07-11 19:50:24 +00:00
|
|
|
recordEvent( 'analytics_import_delete_previous' );
|
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;
|
2019-07-16 08:56:24 +00:00
|
|
|
const path = addQueryArgs(
|
2019-11-12 18:15:55 +00:00
|
|
|
'/wc-analytics/reports/import',
|
2019-07-16 08:56:24 +00:00
|
|
|
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-11-12 18:15:55 +00:00
|
|
|
const path = '/wc-analytics/reports/import/cancel';
|
2019-05-09 09:11:58 +00:00
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 02:11:39 +00:00
|
|
|
export default compose( [
|
2020-02-14 02:23:21 +00:00
|
|
|
withSelect( ( select ) => {
|
2020-08-13 13:23:38 +00:00
|
|
|
const { getNotes } = select( NOTES_STORE_NAME );
|
2020-01-09 02:11:39 +00:00
|
|
|
|
|
|
|
const notesQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
|
|
|
type: 'update',
|
|
|
|
status: 'unactioned',
|
|
|
|
};
|
|
|
|
const notes = getNotes( notesQuery );
|
|
|
|
|
|
|
|
return { notes };
|
|
|
|
} ),
|
2020-02-14 02:23:21 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2020-08-13 13:23:38 +00:00
|
|
|
const { updateNote } = dispatch( NOTES_STORE_NAME );
|
2020-01-09 02:11:39 +00:00
|
|
|
|
|
|
|
return { updateNote };
|
|
|
|
} ),
|
|
|
|
withSpokenMessages,
|
|
|
|
] )( HistoricalData );
|