2019-01-31 01:04:11 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Button } from '@wordpress/components';
|
2019-08-19 06:18:40 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2019-01-31 01:04:11 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2019-07-11 19:30:42 +00:00
|
|
|
import { partial, remove, transform } from 'lodash';
|
2019-01-31 01:04:11 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2019-08-19 06:18:40 +00:00
|
|
|
import { SectionHeader, useFilters, ScrollTo } from '@woocommerce/components';
|
2019-01-31 01:04:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './index.scss';
|
|
|
|
import { analyticsSettings } from './config';
|
|
|
|
import Setting from './setting';
|
2019-05-07 07:18:48 +00:00
|
|
|
import HistoricalData from './historical-data';
|
2019-02-13 11:44:58 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
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';
|
|
|
|
|
|
|
|
class Settings extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
|
|
|
|
const settings = {};
|
|
|
|
analyticsSettings.forEach( setting => ( settings[ setting.name ] = setting.initialValue ) );
|
|
|
|
|
|
|
|
this.state = {
|
2019-05-22 21:43:04 +00:00
|
|
|
settings,
|
2019-02-13 11:44:58 +00:00
|
|
|
saving: false,
|
2019-07-02 20:50:59 +00:00
|
|
|
isDirty: false,
|
2019-01-31 01:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.handleInputChange = this.handleInputChange.bind( this );
|
2019-07-02 20:50:59 +00:00
|
|
|
this.warnIfUnsavedChanges = this.warnIfUnsavedChanges.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener( 'beforeunload', this.warnIfUnsavedChanges );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener( 'beforeunload', this.warnIfUnsavedChanges );
|
2019-01-31 01:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidCatch( error ) {
|
|
|
|
this.setState( {
|
|
|
|
hasError: true,
|
|
|
|
} );
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.warn( error );
|
|
|
|
/* eslint-enable no-console */
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:50:59 +00:00
|
|
|
warnIfUnsavedChanges( event ) {
|
|
|
|
const { isDirty } = this.state;
|
|
|
|
|
|
|
|
if ( isDirty ) {
|
|
|
|
event.returnValue = __(
|
|
|
|
'You have unsaved changes. If you proceed, they will be lost.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
return event.returnValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:04:11 +00:00
|
|
|
resetDefaults = () => {
|
|
|
|
if (
|
|
|
|
window.confirm(
|
2019-03-13 17:14:02 +00:00
|
|
|
__( 'Are you sure you want to reset all settings to default values?', 'woocommerce-admin' )
|
2019-01-31 01:04:11 +00:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
const settings = {};
|
|
|
|
analyticsSettings.forEach( setting => ( settings[ setting.name ] = setting.defaultValue ) );
|
2019-07-11 19:30:42 +00:00
|
|
|
this.setState( { settings }, partial( this.saveChanges, 'reset' ) );
|
2019-01-31 01:04:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-13 11:44:58 +00:00
|
|
|
componentDidUpdate() {
|
2019-07-23 03:26:46 +00:00
|
|
|
const { createNotice, isError, isRequesting } = this.props;
|
2019-07-02 20:50:59 +00:00
|
|
|
const { saving, isDirty } = this.state;
|
|
|
|
let newIsDirtyState = isDirty;
|
2019-02-13 11:44:58 +00:00
|
|
|
|
|
|
|
if ( saving && ! isRequesting ) {
|
|
|
|
if ( ! isError ) {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice(
|
|
|
|
'success',
|
|
|
|
__( 'Your settings have been successfully saved.', 'woocommerce-admin' )
|
|
|
|
);
|
2019-07-02 20:50:59 +00:00
|
|
|
newIsDirtyState = false;
|
2019-02-13 11:44:58 +00:00
|
|
|
} else {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice(
|
|
|
|
'error',
|
|
|
|
__( 'There was an error saving your settings. Please try again.', 'woocommerce-admin' )
|
|
|
|
);
|
2019-02-13 11:44:58 +00:00
|
|
|
}
|
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
2019-07-02 20:50:59 +00:00
|
|
|
this.setState( { saving: false, isDirty: newIsDirtyState } );
|
2019-02-13 11:44:58 +00:00
|
|
|
/* eslint-enable react/no-did-update-set-state */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:43:04 +00:00
|
|
|
/**
|
|
|
|
* Ensure changes are reflected to parameters on the window as well as
|
|
|
|
* the config for construction of this component when re-navigating to
|
|
|
|
* the settings page.
|
|
|
|
*
|
|
|
|
* @param {object} state - State
|
|
|
|
*/
|
|
|
|
persistChanges( state ) {
|
2019-09-23 21:47:08 +00:00
|
|
|
// @todo Should remove global state from the file. This creates
|
|
|
|
// potential hard to debug side-effects.
|
|
|
|
wcSettings.wcAdminSettings = wcSettings.wcAdminSettings || {};
|
2019-05-22 21:43:04 +00:00
|
|
|
analyticsSettings.forEach( setting => {
|
|
|
|
const updatedValue = state.settings[ setting.name ];
|
|
|
|
wcSettings.wcAdminSettings[ setting.name ] = updatedValue;
|
|
|
|
setting.initialValue = updatedValue;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-07-11 19:30:42 +00:00
|
|
|
saveChanges = source => {
|
2019-07-11 18:16:20 +00:00
|
|
|
const { settings } = this.state;
|
2019-05-22 21:43:04 +00:00
|
|
|
this.persistChanges( this.state );
|
2019-07-11 18:16:20 +00:00
|
|
|
this.props.updateSettings( { wc_admin: settings } );
|
|
|
|
|
2019-07-11 19:30:42 +00:00
|
|
|
if ( 'reset' === source ) {
|
|
|
|
recordEvent( 'analytics_settings_reset_defaults' );
|
|
|
|
} else {
|
|
|
|
const eventProps = transform(
|
|
|
|
analyticsSettings,
|
|
|
|
( props, setting ) => {
|
|
|
|
props[ setting.name ] = settings[ setting.name ];
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
recordEvent( 'analytics_settings_save', eventProps );
|
|
|
|
}
|
2019-07-02 20:50:59 +00:00
|
|
|
|
|
|
|
// TODO: remove this optimistic set of isDirty to false once #2541 is resolved.
|
|
|
|
this.setState( { saving: true, isDirty: false } );
|
2019-01-31 01:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
handleInputChange( e ) {
|
|
|
|
const { checked, name, type, value } = e.target;
|
|
|
|
const { settings } = this.state;
|
|
|
|
|
|
|
|
if ( 'checkbox' === type ) {
|
|
|
|
if ( checked ) {
|
|
|
|
settings[ name ].push( value );
|
|
|
|
} else {
|
|
|
|
remove( settings[ name ], v => v === value );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
settings[ name ] = value;
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:50:59 +00:00
|
|
|
this.setState( { settings, isDirty: true } );
|
2019-01-31 01:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-08-19 06:18:40 +00:00
|
|
|
const { createNotice, query } = this.props;
|
2019-01-31 01:04:11 +00:00
|
|
|
const { hasError } = this.state;
|
|
|
|
if ( hasError ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
2019-03-13 17:14:02 +00:00
|
|
|
<SectionHeader title={ __( 'Analytics Settings', 'woocommerce-admin' ) } />
|
2019-01-31 01:04:11 +00:00
|
|
|
<div className="woocommerce-settings__wrapper">
|
|
|
|
{ analyticsSettings.map( setting => (
|
|
|
|
<Setting
|
|
|
|
handleChange={ this.handleInputChange }
|
|
|
|
value={ this.state.settings[ setting.name ] }
|
2019-03-06 05:32:05 +00:00
|
|
|
key={ setting.name }
|
|
|
|
{ ...setting }
|
2019-01-31 01:04:11 +00:00
|
|
|
/>
|
|
|
|
) ) }
|
|
|
|
<div className="woocommerce-settings__actions">
|
|
|
|
<Button isDefault onClick={ this.resetDefaults }>
|
2019-03-13 17:14:02 +00:00
|
|
|
{ __( 'Reset Defaults', 'woocommerce-admin' ) }
|
2019-01-31 01:04:11 +00:00
|
|
|
</Button>
|
|
|
|
<Button isPrimary onClick={ this.saveChanges }>
|
2019-07-02 20:50:59 +00:00
|
|
|
{ __( 'Save Settings', 'woocommerce-admin' ) }
|
2019-01-31 01:04:11 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-08-23 18:34:44 +00:00
|
|
|
{ query.import === 'true' ? (
|
|
|
|
<ScrollTo offset="-56">
|
|
|
|
<HistoricalData createNotice={ createNotice } />
|
|
|
|
</ScrollTo>
|
|
|
|
) : (
|
|
|
|
<HistoricalData createNotice={ createNotice } />
|
|
|
|
) }
|
2019-01-31 01:04:11 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
2019-02-13 11:44:58 +00:00
|
|
|
withSelect( select => {
|
|
|
|
const { getSettings, getSettingsError, isGetSettingsRequesting } = select( 'wc-api' );
|
|
|
|
|
2019-05-24 07:22:21 +00:00
|
|
|
const settings = getSettings( 'wc_admin' );
|
|
|
|
const isError = Boolean( getSettingsError( 'wc_admin' ) );
|
|
|
|
const isRequesting = isGetSettingsRequesting( 'wc_admin' );
|
2019-02-13 11:44:58 +00:00
|
|
|
|
|
|
|
return { getSettings, isError, isRequesting, settings };
|
|
|
|
} ),
|
2019-01-31 01:04:11 +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
|
|
|
const { updateSettings } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice,
|
2019-01-31 01:04:11 +00:00
|
|
|
updateSettings,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( useFilters( SETTINGS_FILTER )( Settings ) );
|