/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import { Button } from '@wordpress/components';
import { Fragment } from '@wordpress/element';
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';
function HistoricalDataActions( {
clearStatusAndTotalsCache,
createNotice,
dateFormat,
importDate,
onImportStarted,
selectedPeriod,
stopImport,
skipChecked,
status,
setImportStarted,
updateImportation,
} ) {
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 onStartImport = () => {
const path = addQueryArgs(
'/wc-analytics/reports/import',
formatParams( dateFormat, selectedPeriod, skipChecked )
);
const errorMessage = __(
'There was a problem rebuilding your report data.',
'woocommerce'
);
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'
);
makeQuery( path, errorMessage );
};
const deletePreviousData = () => {
const path = '/wc-analytics/reports/import/delete';
const errorMessage = __(
'There was a problem deleting your previous data.',
'woocommerce'
);
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();
};
const getActions = () => {
const importDisabled = status !== 'ready';
// An import is currently in progress
if (
[ 'initializing', 'customers', 'orders', 'finalizing' ].includes(
status
)
) {
return (
{ __(
'Navigating away from this page will not affect the import.',
'woocommerce'
) }