2019-05-07 07:18:48 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-09-01 13:21:31 +00:00
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
2019-05-07 07:18:48 +00:00
|
|
|
import { Button } from '@wordpress/components';
|
|
|
|
import { Fragment } from '@wordpress/element';
|
2020-09-01 13:21:31 +00:00
|
|
|
import { IMPORT_STORE_NAME } from '@woocommerce/data';
|
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { formatParams } from './utils';
|
2019-05-07 07:18:48 +00:00
|
|
|
|
|
|
|
function HistoricalDataActions( {
|
2020-09-01 13:21:31 +00:00
|
|
|
clearStatusAndTotalsCache,
|
|
|
|
createNotice,
|
|
|
|
dateFormat,
|
2019-06-11 12:47:53 +00:00
|
|
|
importDate,
|
2020-09-01 13:21:31 +00:00
|
|
|
onImportStarted,
|
|
|
|
selectedPeriod,
|
|
|
|
stopImport,
|
|
|
|
skipChecked,
|
2019-06-11 12:47:53 +00:00
|
|
|
status,
|
2020-09-01 13:21:31 +00:00
|
|
|
setImportStarted,
|
|
|
|
updateImportation,
|
2019-05-07 07:18:48 +00:00
|
|
|
} ) {
|
2020-09-01 13:21:31 +00:00
|
|
|
const onStartImport = () => {
|
|
|
|
const path = addQueryArgs(
|
|
|
|
'/wc-analytics/reports/import',
|
|
|
|
formatParams( dateFormat, selectedPeriod, skipChecked )
|
|
|
|
);
|
|
|
|
const errorMessage = __(
|
|
|
|
'There was a problem rebuilding your report data.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
|
|
|
|
const importStarted = true;
|
|
|
|
makeQuery( path, errorMessage, importStarted );
|
|
|
|
onImportStarted();
|
|
|
|
};
|
|
|
|
|
|
|
|
const onStopImport = () => {
|
|
|
|
stopImport();
|
|
|
|
const path = '/wc-analytics/reports/import/cancel';
|
|
|
|
const errorMessage = __(
|
|
|
|
'There was a problem stopping your current import.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
makeQuery( path, errorMessage );
|
|
|
|
};
|
|
|
|
|
|
|
|
const makeQuery = ( path, errorMessage, importStarted = false ) => {
|
|
|
|
updateImportation( path, importStarted )
|
|
|
|
.then( ( response ) => {
|
|
|
|
if ( response.status === 'success' ) {
|
|
|
|
createNotice( 'success', response.message );
|
|
|
|
} else {
|
|
|
|
createNotice( 'error', errorMessage );
|
|
|
|
setImportStarted( false );
|
|
|
|
stopImport();
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
|
|
|
if ( error && error.message ) {
|
|
|
|
createNotice( 'error', error.message );
|
|
|
|
setImportStarted( false );
|
|
|
|
stopImport();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const deletePreviousData = () => {
|
|
|
|
const path = '/wc-analytics/reports/import/delete';
|
|
|
|
const errorMessage = __(
|
|
|
|
'There was a problem deleting your previous data.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
makeQuery( path, errorMessage );
|
|
|
|
|
|
|
|
recordEvent( 'analytics_import_delete_previous' );
|
|
|
|
|
|
|
|
setImportStarted( false );
|
|
|
|
};
|
|
|
|
const reimportData = () => {
|
|
|
|
setImportStarted( false );
|
|
|
|
// We need to clear the cache of the selectors `getImportTotals` and `getImportStatus`
|
|
|
|
clearStatusAndTotalsCache();
|
|
|
|
};
|
2019-05-07 07:18:48 +00:00
|
|
|
const getActions = () => {
|
2019-06-11 12:47:53 +00:00
|
|
|
const importDisabled = status !== 'ready';
|
|
|
|
|
2019-05-07 07:18:48 +00:00
|
|
|
// An import is currently in progress
|
2020-02-14 02:23:21 +00:00
|
|
|
if (
|
|
|
|
[ 'initializing', 'customers', 'orders', 'finalizing' ].includes(
|
|
|
|
status
|
|
|
|
)
|
|
|
|
) {
|
2019-05-07 07:18:48 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<Button
|
|
|
|
className="woocommerce-settings-historical-data__action-button"
|
|
|
|
isPrimary
|
2019-05-09 09:11:58 +00:00
|
|
|
onClick={ onStopImport }
|
2019-05-07 07:18:48 +00:00
|
|
|
>
|
|
|
|
{ __( 'Stop Import', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
<div className="woocommerce-setting__help woocommerce-settings-historical-data__action-help">
|
|
|
|
{ __(
|
|
|
|
'Imported data will not be lost if the import is stopped.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
<br />
|
|
|
|
{ __(
|
|
|
|
'Navigating away from this page will not affect the import.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:47:53 +00:00
|
|
|
if ( [ 'ready', 'nothing' ].includes( status ) ) {
|
|
|
|
if ( importDate ) {
|
|
|
|
return (
|
|
|
|
<Fragment>
|
2020-02-14 02:23:21 +00:00
|
|
|
<Button
|
|
|
|
isPrimary
|
|
|
|
onClick={ onStartImport }
|
|
|
|
disabled={ importDisabled }
|
|
|
|
>
|
2019-06-11 12:47:53 +00:00
|
|
|
{ __( 'Start', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
2020-09-01 13:21:31 +00:00
|
|
|
<Button isSecondary onClick={ deletePreviousData }>
|
2020-02-14 02:23:21 +00:00
|
|
|
{ __(
|
|
|
|
'Delete Previously Imported Data',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-06-11 12:47:53 +00:00
|
|
|
</Button>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-07 07:18:48 +00:00
|
|
|
return (
|
2019-05-10 09:34:03 +00:00
|
|
|
<Fragment>
|
2020-02-14 02:23:21 +00:00
|
|
|
<Button
|
|
|
|
isPrimary
|
|
|
|
onClick={ onStartImport }
|
|
|
|
disabled={ importDisabled }
|
|
|
|
>
|
2019-05-10 09:34:03 +00:00
|
|
|
{ __( 'Start', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
</Fragment>
|
2019-05-07 07:18:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-01 13:21:31 +00:00
|
|
|
if ( status === 'error' ) {
|
|
|
|
createNotice(
|
|
|
|
'error',
|
|
|
|
__(
|
|
|
|
'Something went wrong with the importation process.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-07 07:18:48 +00:00
|
|
|
// Has imported all possible data
|
|
|
|
return (
|
|
|
|
<Fragment>
|
2020-09-01 13:21:31 +00:00
|
|
|
<Button isSecondary onClick={ reimportData }>
|
2019-06-11 12:47:53 +00:00
|
|
|
{ __( 'Re-import Data', 'woocommerce-admin' ) }
|
2019-05-07 07:18:48 +00:00
|
|
|
</Button>
|
2020-09-01 13:21:31 +00:00
|
|
|
<Button isSecondary onClick={ deletePreviousData }>
|
2020-02-14 02:23:21 +00:00
|
|
|
{ __(
|
|
|
|
'Delete Previously Imported Data',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-05-07 07:18:48 +00:00
|
|
|
</Button>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-settings__actions woocommerce-settings-historical-data__actions">
|
|
|
|
{ getActions() }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-01 13:21:31 +00:00
|
|
|
export default compose( [
|
|
|
|
withSelect( ( select ) => {
|
|
|
|
const { getFormSettings } = select( IMPORT_STORE_NAME );
|
|
|
|
|
|
|
|
const {
|
|
|
|
period: selectedPeriod,
|
|
|
|
skipPrevious: skipChecked,
|
|
|
|
} = getFormSettings();
|
|
|
|
|
|
|
|
return {
|
|
|
|
selectedPeriod,
|
|
|
|
skipChecked,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( ( dispatch ) => {
|
|
|
|
const { updateImportation, setImportStarted } = dispatch(
|
|
|
|
IMPORT_STORE_NAME
|
|
|
|
);
|
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
return {
|
|
|
|
createNotice,
|
|
|
|
setImportStarted,
|
|
|
|
updateImportation,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
] )( HistoricalDataActions );
|