2019-01-31 01:04:11 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Button } from '@wordpress/components';
|
2020-03-25 03:20:17 +00:00
|
|
|
import { Fragment, useEffect, useRef } from '@wordpress/element';
|
2019-01-31 01:04:11 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { withDispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2019-08-19 06:18:40 +00:00
|
|
|
import { SectionHeader, useFilters, ScrollTo } from '@woocommerce/components';
|
2020-03-25 03:20:17 +00:00
|
|
|
import { useSettings } from '@woocommerce/data';
|
2019-01-31 01:04:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './index.scss';
|
2020-03-25 03:20:17 +00:00
|
|
|
import { config } from './config';
|
2019-01-31 01:04:11 +00:00
|
|
|
import Setting from './setting';
|
2019-05-07 07:18:48 +00:00
|
|
|
import HistoricalData from './historical-data';
|
2019-07-11 18:16:20 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
2019-01-31 01:04:11 +00:00
|
|
|
|
|
|
|
const SETTINGS_FILTER = 'woocommerce_admin_analytics_settings';
|
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
const Settings = ( { createNotice, query } ) => {
|
|
|
|
const {
|
|
|
|
settingsError,
|
|
|
|
isRequesting,
|
|
|
|
isDirty,
|
|
|
|
persistSettings,
|
|
|
|
updateAndPersistSettings,
|
|
|
|
updateSettings,
|
|
|
|
wcAdminSettings,
|
|
|
|
} = useSettings( 'wc_admin', [ 'wcAdminSettings' ] );
|
|
|
|
const hasSaved = useRef( false );
|
|
|
|
|
2020-04-16 23:58:36 +00:00
|
|
|
useEffect( () => {
|
|
|
|
function warnIfUnsavedChanges( event ) {
|
|
|
|
if ( isDirty ) {
|
|
|
|
event.returnValue = __(
|
|
|
|
'You have unsaved changes. If you proceed, they will be lost.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
return event.returnValue;
|
2020-03-25 03:20:17 +00:00
|
|
|
}
|
2020-04-16 23:58:36 +00:00
|
|
|
}
|
|
|
|
window.addEventListener( 'beforeunload', warnIfUnsavedChanges );
|
|
|
|
return () =>
|
|
|
|
window.removeEventListener( 'beforeunload', warnIfUnsavedChanges );
|
|
|
|
}, [ isDirty ] );
|
2020-03-25 03:20:17 +00:00
|
|
|
|
2020-04-16 23:58:36 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( isRequesting ) {
|
|
|
|
hasSaved.current = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( ! isRequesting && hasSaved.current ) {
|
|
|
|
if ( ! settingsError ) {
|
|
|
|
createNotice(
|
|
|
|
'success',
|
|
|
|
__(
|
|
|
|
'Your settings have been successfully saved.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
createNotice(
|
|
|
|
'error',
|
|
|
|
__(
|
2020-07-28 02:32:58 +00:00
|
|
|
'There was an error saving your settings. Please try again.',
|
2020-04-16 23:58:36 +00:00
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
2020-03-25 03:20:17 +00:00
|
|
|
}
|
2020-04-16 23:58:36 +00:00
|
|
|
hasSaved.current = false;
|
|
|
|
}
|
|
|
|
}, [ isRequesting, settingsError, createNotice ] );
|
2019-07-02 20:50:59 +00:00
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
const resetDefaults = () => {
|
2019-01-31 01:04:11 +00:00
|
|
|
if (
|
2020-02-14 02:23:21 +00:00
|
|
|
// eslint-disable-next-line no-alert
|
2019-01-31 01:04:11 +00:00
|
|
|
window.confirm(
|
2020-02-14 02:23:21 +00:00
|
|
|
__(
|
|
|
|
'Are you sure you want to reset all settings to default values?',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
2019-01-31 01:04:11 +00:00
|
|
|
)
|
|
|
|
) {
|
2020-04-16 23:58:36 +00:00
|
|
|
const resetSettings = Object.keys( config ).reduce(
|
|
|
|
( result, setting ) => {
|
|
|
|
result[ setting ] = config[ setting ].defaultValue;
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
2019-02-13 11:44:58 +00:00
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
updateAndPersistSettings( 'wcAdminSettings', resetSettings );
|
2019-07-11 19:30:42 +00:00
|
|
|
recordEvent( 'analytics_settings_reset_defaults' );
|
|
|
|
}
|
2020-03-25 03:20:17 +00:00
|
|
|
};
|
2019-07-02 20:50:59 +00:00
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
const saveChanges = () => {
|
|
|
|
persistSettings();
|
|
|
|
recordEvent( 'analytics_settings_save', wcAdminSettings );
|
2019-11-28 20:54:15 +00:00
|
|
|
|
|
|
|
// On save, reset persisted query properties of Nav Menu links to default
|
|
|
|
query.period = undefined;
|
|
|
|
query.compare = undefined;
|
|
|
|
query.before = undefined;
|
|
|
|
query.after = undefined;
|
|
|
|
query.interval = undefined;
|
|
|
|
query.type = undefined;
|
|
|
|
window.wpNavMenuUrlUpdate( query );
|
2019-01-31 01:04:11 +00:00
|
|
|
};
|
|
|
|
|
2020-04-16 23:58:36 +00:00
|
|
|
const handleInputChange = ( e ) => {
|
2019-01-31 01:04:11 +00:00
|
|
|
const { checked, name, type, value } = e.target;
|
2020-03-25 03:20:17 +00:00
|
|
|
const nextSettings = { ...wcAdminSettings };
|
2019-01-31 01:04:11 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
if ( type === 'checkbox' ) {
|
2019-01-31 01:04:11 +00:00
|
|
|
if ( checked ) {
|
2020-03-25 03:20:17 +00:00
|
|
|
nextSettings[ name ] = [ ...nextSettings[ name ], value ];
|
2019-01-31 01:04:11 +00:00
|
|
|
} else {
|
2020-04-16 23:58:36 +00:00
|
|
|
nextSettings[ name ] = nextSettings[ name ].filter(
|
|
|
|
( v ) => v !== value
|
|
|
|
);
|
2019-01-31 01:04:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-03-25 03:20:17 +00:00
|
|
|
nextSettings[ name ] = value;
|
2019-01-31 01:04:11 +00:00
|
|
|
}
|
2020-03-25 03:20:17 +00:00
|
|
|
updateSettings( 'wcAdminSettings', nextSettings );
|
|
|
|
};
|
2019-01-31 01:04:11 +00:00
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2020-04-16 23:58:36 +00:00
|
|
|
<SectionHeader
|
|
|
|
title={ __( 'Analytics Settings', 'woocommerce-admin' ) }
|
|
|
|
/>
|
2020-03-25 03:20:17 +00:00
|
|
|
<div className="woocommerce-settings__wrapper">
|
2020-04-16 23:58:36 +00:00
|
|
|
{ Object.keys( config ).map( ( setting ) => (
|
2020-03-25 03:20:17 +00:00
|
|
|
<Setting
|
|
|
|
handleChange={ handleInputChange }
|
|
|
|
value={ wcAdminSettings[ setting ] }
|
|
|
|
key={ setting }
|
|
|
|
name={ setting }
|
|
|
|
{ ...config[ setting ] }
|
|
|
|
/>
|
|
|
|
) ) }
|
|
|
|
<div className="woocommerce-settings__actions">
|
2020-06-11 19:22:20 +00:00
|
|
|
<Button isSecondary onClick={ resetDefaults }>
|
2020-03-25 03:20:17 +00:00
|
|
|
{ __( 'Reset Defaults', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
2020-04-16 23:58:36 +00:00
|
|
|
<Button
|
|
|
|
isPrimary
|
|
|
|
isBusy={ isRequesting }
|
|
|
|
onClick={ saveChanges }
|
|
|
|
>
|
2020-03-25 03:20:17 +00:00
|
|
|
{ __( 'Save Settings', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
2019-01-31 01:04:11 +00:00
|
|
|
</div>
|
2020-03-25 03:20:17 +00:00
|
|
|
</div>
|
|
|
|
{ query.import === 'true' ? (
|
|
|
|
<ScrollTo offset="-56">
|
2019-08-23 18:34:44 +00:00
|
|
|
<HistoricalData createNotice={ createNotice } />
|
2020-03-25 03:20:17 +00:00
|
|
|
</ScrollTo>
|
|
|
|
) : (
|
|
|
|
<HistoricalData createNotice={ createNotice } />
|
|
|
|
) }
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
};
|
2019-01-31 01:04:11 +00:00
|
|
|
|
|
|
|
export default compose(
|
2020-04-16 23:58:36 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2019-07-23 03:26:46 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
2019-01-31 01:04:11 +00:00
|
|
|
|
|
|
|
return {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice,
|
2019-01-31 01:04:11 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( useFilters( SETTINGS_FILTER )( Settings ) );
|