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.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-09-01 13:21:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-09-01 13:21:31 +00:00
|
|
|
);
|
|
|
|
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.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-09-01 13:21:31 +00:00
|
|
|
);
|
|
|
|
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
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Stop Import', 'woocommerce' ) }
|
2019-05-07 07:18:48 +00:00
|
|
|
</Button>
|
|
|
|
<div className="woocommerce-setting__help woocommerce-settings-historical-data__action-help">
|
|
|
|
{ __(
|
|
|
|
'Imported data will not be lost if the import is stopped.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2019-05-07 07:18:48 +00:00
|
|
|
) }
|
|
|
|
<br />
|
|
|
|
{ __(
|
|
|
|
'Navigating away from this page will not affect the import.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2019-05-07 07:18:48 +00:00
|
|
|
) }
|
|
|
|
</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 }
|
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Start', 'woocommerce' ) }
|
2019-06-11 12:47:53 +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',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-02-14 02:23:21 +00:00
|
|
|
) }
|
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 }
|
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Start', 'woocommerce' ) }
|
2019-05-10 09:34:03 +00:00
|
|
|
</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.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-09-01 13:21:31 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 }>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Re-import Data', 'woocommerce' ) }
|
2019-05-07 07:18:48 +00:00
|
|
|
</Button>
|
2020-09-01 13:21:31 +00:00
|
|
|
<Button isSecondary onClick={ deletePreviousData }>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Delete Previously Imported Data', 'woocommerce' ) }
|
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 );
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
const { period: selectedPeriod, skipPrevious: skipChecked } =
|
|
|
|
getFormSettings();
|
2020-09-01 13:21:31 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
selectedPeriod,
|
|
|
|
skipChecked,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( ( dispatch ) => {
|
2022-06-21 08:37:34 +00:00
|
|
|
const { updateImportation, setImportStarted } =
|
|
|
|
dispatch( IMPORT_STORE_NAME );
|
2020-09-01 13:21:31 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
return {
|
|
|
|
createNotice,
|
|
|
|
setImportStarted,
|
|
|
|
updateImportation,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
] )( HistoricalDataActions );
|