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

95 lines
2.6 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import moment from 'moment';
import { SelectControl } from '@wordpress/components';
import { DatePicker } from '@woocommerce/components';
import { dateValidationMessages } from '@woocommerce/date';
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 } from '@wordpress/data';
function HistoricalDataPeriodSelector( {
dateFormat,
disabled,
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
setImportPeriod,
value,
} ) {
const onSelectChange = ( val ) => {
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
setImportPeriod( val );
};
const onDatePickerChange = ( val ) => {
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 dateModified = true;
if ( val.date && val.date.isValid ) {
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
setImportPeriod( val.date.format( dateFormat ), dateModified );
} else {
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
setImportPeriod( val.text, dateModified );
}
};
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 getDatePickerError = ( momentDate ) => {
if (
! momentDate.isValid() ||
value.date.length !== dateFormat.length
) {
return dateValidationMessages.invalid;
}
if ( momentDate.isAfter( new Date(), 'day' ) ) {
return dateValidationMessages.future;
}
return null;
};
const getDatePicker = () => {
const momentDate = moment( value.date, dateFormat );
return (
<div className="woocommerce-settings-historical-data__column">
<div className="woocommerce-settings-historical-data__column-label">
{ __( 'Beginning on', 'woocommerce-admin' ) }
</div>
<DatePicker
date={ momentDate.isValid() ? momentDate.toDate() : null }
dateFormat={ dateFormat }
disabled={ disabled }
error={ getDatePickerError( momentDate ) }
isInvalidDate={ ( date ) =>
moment( date ).isAfter( new Date(), 'day' )
}
onUpdate={ onDatePickerChange }
text={ value.date }
/>
</div>
);
};
return (
<div className="woocommerce-settings-historical-data__columns">
<div className="woocommerce-settings-historical-data__column">
<SelectControl
label={ __(
'Import Historical Data',
'woocommerce-admin'
) }
value={ value.label }
disabled={ disabled }
onChange={ onSelectChange }
options={ [
{ label: 'All', value: 'all' },
{ label: 'Last 365 days', value: '365' },
{ label: 'Last 90 days', value: '90' },
{ label: 'Last 30 days', value: '30' },
{ label: 'Last 7 days', value: '7' },
{ label: 'Last 24 hours', value: '1' },
{ label: 'Custom', value: 'custom' },
] }
/>
</div>
{ value.label === 'custom' && getDatePicker() }
</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 withDispatch( ( dispatch ) => {
const { setImportPeriod } = dispatch( IMPORT_STORE_NAME );
return { setImportPeriod };
} )( HistoricalDataPeriodSelector );