woocommerce/plugins/woocommerce-admin/client/analytics/settings/historical-data/actions.js

219 lines
5.0 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
Migrated "import" store to "wp.data" (https://github.com/woocommerce/woocommerce-admin/pull/4982) * Migrated the "import" store to "wp.data" This commit migrates the "import" store to "wp.data" # Conflicts: # client/analytics/settings/historical-data/layout.js # packages/data/src/index.js # Conflicts: # packages/data/src/index.js # Conflicts: # packages/data/src/index.js * Added error handling This commit adds error handling to the apiFetch * Fixred reducer.js * Added reducer.js tests * Removed "endpoint" references * Tests fixed * Removed old import store files and wc-api-spec import references This commit removes old import store files and "wc-api-spec" import references # Conflicts: # client/wc-api/wc-api-spec.js # Conflicts: # client/wc-api/wc-api-spec.js * Added interval to invalidate resolution of getImportStatus * Fixed timestamp * Store dependency removed * Fixed reimport functionality # Conflicts: # client/analytics/settings/historical-data/index.js * Refactored layout.js * Refactored clearCache method * inProgress check refactored * Using activeImport from store # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed console warning # Conflicts: # client/analytics/settings/historical-data/index.js * Migration done! # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed hasImportFinished * Test fixed * Removed "setImportFinished" from actions.js and index.js * Added style to disabled button * Fixed incorrect "Finalizing" status * Fixed tests * Fixed notes update on importation start * Removed local constants * Removed css for disabled button * Typo corrected * Fixed param for getImportError method Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
2020-09-01 13:21:31 +00:00
import { addQueryArgs } from '@wordpress/url';
import { Button } from '@wordpress/components';
import { Fragment } from '@wordpress/element';
Migrated "import" store to "wp.data" (https://github.com/woocommerce/woocommerce-admin/pull/4982) * Migrated the "import" store to "wp.data" This commit migrates the "import" store to "wp.data" # Conflicts: # client/analytics/settings/historical-data/layout.js # packages/data/src/index.js # Conflicts: # packages/data/src/index.js # Conflicts: # packages/data/src/index.js * Added error handling This commit adds error handling to the apiFetch * Fixred reducer.js * Added reducer.js tests * Removed "endpoint" references * Tests fixed * Removed old import store files and wc-api-spec import references This commit removes old import store files and "wc-api-spec" import references # Conflicts: # client/wc-api/wc-api-spec.js # Conflicts: # client/wc-api/wc-api-spec.js * Added interval to invalidate resolution of getImportStatus * Fixed timestamp * Store dependency removed * Fixed reimport functionality # Conflicts: # client/analytics/settings/historical-data/index.js * Refactored layout.js * Refactored clearCache method * inProgress check refactored * Using activeImport from store # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed console warning # Conflicts: # client/analytics/settings/historical-data/index.js * Migration done! # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed hasImportFinished * Test fixed * Removed "setImportFinished" from actions.js and index.js * Added style to disabled button * Fixed incorrect "Finalizing" status * Fixed tests * Fixed notes update on importation start * Removed local constants * Removed css for disabled button * Typo corrected * Fixed param for getImportError method Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
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';
function HistoricalDataActions( {
Migrated "import" store to "wp.data" (https://github.com/woocommerce/woocommerce-admin/pull/4982) * Migrated the "import" store to "wp.data" This commit migrates the "import" store to "wp.data" # Conflicts: # client/analytics/settings/historical-data/layout.js # packages/data/src/index.js # Conflicts: # packages/data/src/index.js # Conflicts: # packages/data/src/index.js * Added error handling This commit adds error handling to the apiFetch * Fixred reducer.js * Added reducer.js tests * Removed "endpoint" references * Tests fixed * Removed old import store files and wc-api-spec import references This commit removes old import store files and "wc-api-spec" import references # Conflicts: # client/wc-api/wc-api-spec.js # Conflicts: # client/wc-api/wc-api-spec.js * Added interval to invalidate resolution of getImportStatus * Fixed timestamp * Store dependency removed * Fixed reimport functionality # Conflicts: # client/analytics/settings/historical-data/index.js * Refactored layout.js * Refactored clearCache method * inProgress check refactored * Using activeImport from store # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed console warning # Conflicts: # client/analytics/settings/historical-data/index.js * Migration done! # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed hasImportFinished * Test fixed * Removed "setImportFinished" from actions.js and index.js * Added style to disabled button * Fixed incorrect "Finalizing" status * Fixed tests * Fixed notes update on importation start * Removed local constants * Removed css for disabled button * Typo corrected * Fixed param for getImportError method Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
2020-09-01 13:21:31 +00:00
clearStatusAndTotalsCache,
createNotice,
dateFormat,
importDate,
Migrated "import" store to "wp.data" (https://github.com/woocommerce/woocommerce-admin/pull/4982) * Migrated the "import" store to "wp.data" This commit migrates the "import" store to "wp.data" # Conflicts: # client/analytics/settings/historical-data/layout.js # packages/data/src/index.js # Conflicts: # packages/data/src/index.js # Conflicts: # packages/data/src/index.js * Added error handling This commit adds error handling to the apiFetch * Fixred reducer.js * Added reducer.js tests * Removed "endpoint" references * Tests fixed * Removed old import store files and wc-api-spec import references This commit removes old import store files and "wc-api-spec" import references # Conflicts: # client/wc-api/wc-api-spec.js # Conflicts: # client/wc-api/wc-api-spec.js * Added interval to invalidate resolution of getImportStatus * Fixed timestamp * Store dependency removed * Fixed reimport functionality # Conflicts: # client/analytics/settings/historical-data/index.js * Refactored layout.js * Refactored clearCache method * inProgress check refactored * Using activeImport from store # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed console warning # Conflicts: # client/analytics/settings/historical-data/index.js * Migration done! # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed hasImportFinished * Test fixed * Removed "setImportFinished" from actions.js and index.js * Added style to disabled button * Fixed incorrect "Finalizing" status * Fixed tests * Fixed notes update on importation start * Removed local constants * Removed css for disabled button * Typo corrected * Fixed param for getImportError method Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
2020-09-01 13:21:31 +00:00
onImportStarted,
selectedPeriod,
stopImport,
skipChecked,
status,
Migrated "import" store to "wp.data" (https://github.com/woocommerce/woocommerce-admin/pull/4982) * Migrated the "import" store to "wp.data" This commit migrates the "import" store to "wp.data" # Conflicts: # client/analytics/settings/historical-data/layout.js # packages/data/src/index.js # Conflicts: # packages/data/src/index.js # Conflicts: # packages/data/src/index.js * Added error handling This commit adds error handling to the apiFetch * Fixred reducer.js * Added reducer.js tests * Removed "endpoint" references * Tests fixed * Removed old import store files and wc-api-spec import references This commit removes old import store files and "wc-api-spec" import references # Conflicts: # client/wc-api/wc-api-spec.js # Conflicts: # client/wc-api/wc-api-spec.js * Added interval to invalidate resolution of getImportStatus * Fixed timestamp * Store dependency removed * Fixed reimport functionality # Conflicts: # client/analytics/settings/historical-data/index.js * Refactored layout.js * Refactored clearCache method * inProgress check refactored * Using activeImport from store # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed console warning # Conflicts: # client/analytics/settings/historical-data/index.js * Migration done! # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed hasImportFinished * Test fixed * Removed "setImportFinished" from actions.js and index.js * Added style to disabled button * Fixed incorrect "Finalizing" status * Fixed tests * Fixed notes update on importation start * Removed local constants * Removed css for disabled button * Typo corrected * Fixed param for getImportError method Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
2020-09-01 13:21:31 +00:00
setImportStarted,
updateImportation,
} ) {
Migrated "import" store to "wp.data" (https://github.com/woocommerce/woocommerce-admin/pull/4982) * Migrated the "import" store to "wp.data" This commit migrates the "import" store to "wp.data" # Conflicts: # client/analytics/settings/historical-data/layout.js # packages/data/src/index.js # Conflicts: # packages/data/src/index.js # Conflicts: # packages/data/src/index.js * Added error handling This commit adds error handling to the apiFetch * Fixred reducer.js * Added reducer.js tests * Removed "endpoint" references * Tests fixed * Removed old import store files and wc-api-spec import references This commit removes old import store files and "wc-api-spec" import references # Conflicts: # client/wc-api/wc-api-spec.js # Conflicts: # client/wc-api/wc-api-spec.js * Added interval to invalidate resolution of getImportStatus * Fixed timestamp * Store dependency removed * Fixed reimport functionality # Conflicts: # client/analytics/settings/historical-data/index.js * Refactored layout.js * Refactored clearCache method * inProgress check refactored * Using activeImport from store # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed console warning # Conflicts: # client/analytics/settings/historical-data/index.js * Migration done! # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed hasImportFinished * Test fixed * Removed "setImportFinished" from actions.js and index.js * Added style to disabled button * Fixed incorrect "Finalizing" status * Fixed tests * Fixed notes update on importation start * Removed local constants * Removed css for disabled button * Typo corrected * Fixed param for getImportError method Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
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();
};
const getActions = () => {
const importDisabled = status !== 'ready';
// An import is currently in progress
if (
[ 'initializing', 'customers', 'orders', 'finalizing' ].includes(
status
)
) {
return (
<Fragment>
<Button
className="woocommerce-settings-historical-data__action-button"
isPrimary
onClick={ onStopImport }
>
{ __( '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>
);
}
if ( [ 'ready', 'nothing' ].includes( status ) ) {
if ( importDate ) {
return (
<Fragment>
<Button
isPrimary
onClick={ onStartImport }
disabled={ importDisabled }
>
{ __( 'Start', 'woocommerce-admin' ) }
</Button>
Migrated "import" store to "wp.data" (https://github.com/woocommerce/woocommerce-admin/pull/4982) * Migrated the "import" store to "wp.data" This commit migrates the "import" store to "wp.data" # Conflicts: # client/analytics/settings/historical-data/layout.js # packages/data/src/index.js # Conflicts: # packages/data/src/index.js # Conflicts: # packages/data/src/index.js * Added error handling This commit adds error handling to the apiFetch * Fixred reducer.js * Added reducer.js tests * Removed "endpoint" references * Tests fixed * Removed old import store files and wc-api-spec import references This commit removes old import store files and "wc-api-spec" import references # Conflicts: # client/wc-api/wc-api-spec.js # Conflicts: # client/wc-api/wc-api-spec.js * Added interval to invalidate resolution of getImportStatus * Fixed timestamp * Store dependency removed * Fixed reimport functionality # Conflicts: # client/analytics/settings/historical-data/index.js * Refactored layout.js * Refactored clearCache method * inProgress check refactored * Using activeImport from store # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed console warning # Conflicts: # client/analytics/settings/historical-data/index.js * Migration done! # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed hasImportFinished * Test fixed * Removed "setImportFinished" from actions.js and index.js * Added style to disabled button * Fixed incorrect "Finalizing" status * Fixed tests * Fixed notes update on importation start * Removed local constants * Removed css for disabled button * Typo corrected * Fixed param for getImportError method Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
2020-09-01 13:21:31 +00:00
<Button isSecondary onClick={ deletePreviousData }>
{ __(
'Delete Previously Imported Data',
'woocommerce-admin'
) }
</Button>
</Fragment>
);
}
return (
<Fragment>
<Button
isPrimary
onClick={ onStartImport }
disabled={ importDisabled }
>
{ __( 'Start', 'woocommerce-admin' ) }
</Button>
</Fragment>
);
}
Migrated "import" store to "wp.data" (https://github.com/woocommerce/woocommerce-admin/pull/4982) * Migrated the "import" store to "wp.data" This commit migrates the "import" store to "wp.data" # Conflicts: # client/analytics/settings/historical-data/layout.js # packages/data/src/index.js # Conflicts: # packages/data/src/index.js # Conflicts: # packages/data/src/index.js * Added error handling This commit adds error handling to the apiFetch * Fixred reducer.js * Added reducer.js tests * Removed "endpoint" references * Tests fixed * Removed old import store files and wc-api-spec import references This commit removes old import store files and "wc-api-spec" import references # Conflicts: # client/wc-api/wc-api-spec.js # Conflicts: # client/wc-api/wc-api-spec.js * Added interval to invalidate resolution of getImportStatus * Fixed timestamp * Store dependency removed * Fixed reimport functionality # Conflicts: # client/analytics/settings/historical-data/index.js * Refactored layout.js * Refactored clearCache method * inProgress check refactored * Using activeImport from store # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed console warning # Conflicts: # client/analytics/settings/historical-data/index.js * Migration done! # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed hasImportFinished * Test fixed * Removed "setImportFinished" from actions.js and index.js * Added style to disabled button * Fixed incorrect "Finalizing" status * Fixed tests * Fixed notes update on importation start * Removed local constants * Removed css for disabled button * Typo corrected * Fixed param for getImportError method Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
2020-09-01 13:21:31 +00:00
if ( status === 'error' ) {
createNotice(
'error',
__(
'Something went wrong with the importation process.',
'woocommerce-admin'
)
);
}
// Has imported all possible data
return (
<Fragment>
Migrated "import" store to "wp.data" (https://github.com/woocommerce/woocommerce-admin/pull/4982) * Migrated the "import" store to "wp.data" This commit migrates the "import" store to "wp.data" # Conflicts: # client/analytics/settings/historical-data/layout.js # packages/data/src/index.js # Conflicts: # packages/data/src/index.js # Conflicts: # packages/data/src/index.js * Added error handling This commit adds error handling to the apiFetch * Fixred reducer.js * Added reducer.js tests * Removed "endpoint" references * Tests fixed * Removed old import store files and wc-api-spec import references This commit removes old import store files and "wc-api-spec" import references # Conflicts: # client/wc-api/wc-api-spec.js # Conflicts: # client/wc-api/wc-api-spec.js * Added interval to invalidate resolution of getImportStatus * Fixed timestamp * Store dependency removed * Fixed reimport functionality # Conflicts: # client/analytics/settings/historical-data/index.js * Refactored layout.js * Refactored clearCache method * inProgress check refactored * Using activeImport from store # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed console warning # Conflicts: # client/analytics/settings/historical-data/index.js * Migration done! # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed hasImportFinished * Test fixed * Removed "setImportFinished" from actions.js and index.js * Added style to disabled button * Fixed incorrect "Finalizing" status * Fixed tests * Fixed notes update on importation start * Removed local constants * Removed css for disabled button * Typo corrected * Fixed param for getImportError method Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
2020-09-01 13:21:31 +00:00
<Button isSecondary onClick={ reimportData }>
{ __( 'Re-import Data', 'woocommerce-admin' ) }
</Button>
Migrated "import" store to "wp.data" (https://github.com/woocommerce/woocommerce-admin/pull/4982) * Migrated the "import" store to "wp.data" This commit migrates the "import" store to "wp.data" # Conflicts: # client/analytics/settings/historical-data/layout.js # packages/data/src/index.js # Conflicts: # packages/data/src/index.js # Conflicts: # packages/data/src/index.js * Added error handling This commit adds error handling to the apiFetch * Fixred reducer.js * Added reducer.js tests * Removed "endpoint" references * Tests fixed * Removed old import store files and wc-api-spec import references This commit removes old import store files and "wc-api-spec" import references # Conflicts: # client/wc-api/wc-api-spec.js # Conflicts: # client/wc-api/wc-api-spec.js * Added interval to invalidate resolution of getImportStatus * Fixed timestamp * Store dependency removed * Fixed reimport functionality # Conflicts: # client/analytics/settings/historical-data/index.js * Refactored layout.js * Refactored clearCache method * inProgress check refactored * Using activeImport from store # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed console warning # Conflicts: # client/analytics/settings/historical-data/index.js * Migration done! # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed hasImportFinished * Test fixed * Removed "setImportFinished" from actions.js and index.js * Added style to disabled button * Fixed incorrect "Finalizing" status * Fixed tests * Fixed notes update on importation start * Removed local constants * Removed css for disabled button * Typo corrected * Fixed param for getImportError method Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
2020-09-01 13:21:31 +00:00
<Button isSecondary onClick={ deletePreviousData }>
{ __(
'Delete Previously Imported Data',
'woocommerce-admin'
) }
</Button>
</Fragment>
);
};
return (
<div className="woocommerce-settings__actions woocommerce-settings-historical-data__actions">
{ getActions() }
</div>
);
}
Migrated "import" store to "wp.data" (https://github.com/woocommerce/woocommerce-admin/pull/4982) * Migrated the "import" store to "wp.data" This commit migrates the "import" store to "wp.data" # Conflicts: # client/analytics/settings/historical-data/layout.js # packages/data/src/index.js # Conflicts: # packages/data/src/index.js # Conflicts: # packages/data/src/index.js * Added error handling This commit adds error handling to the apiFetch * Fixred reducer.js * Added reducer.js tests * Removed "endpoint" references * Tests fixed * Removed old import store files and wc-api-spec import references This commit removes old import store files and "wc-api-spec" import references # Conflicts: # client/wc-api/wc-api-spec.js # Conflicts: # client/wc-api/wc-api-spec.js * Added interval to invalidate resolution of getImportStatus * Fixed timestamp * Store dependency removed * Fixed reimport functionality # Conflicts: # client/analytics/settings/historical-data/index.js * Refactored layout.js * Refactored clearCache method * inProgress check refactored * Using activeImport from store # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed console warning # Conflicts: # client/analytics/settings/historical-data/index.js * Migration done! # Conflicts: # client/analytics/settings/historical-data/index.js * Fixed hasImportFinished * Test fixed * Removed "setImportFinished" from actions.js and index.js * Added style to disabled button * Fixed incorrect "Finalizing" status * Fixed tests * Fixed notes update on importation start * Removed local constants * Removed css for disabled button * Typo corrected * Fixed param for getImportError method Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
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 );