2019-01-31 01:04:11 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Button } from '@wordpress/components';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { remove } from 'lodash';
|
|
|
|
import { withDispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { SectionHeader, useFilters } from '@woocommerce/components';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-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 ) );
|
|
|
|
this.setState( { settings }, this.saveChanges );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-13 11:44:58 +00:00
|
|
|
componentDidUpdate() {
|
2019-07-10 16:58:51 +00:00
|
|
|
const { addNotice, 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-10 16:58:51 +00:00
|
|
|
addNotice( {
|
|
|
|
status: 'success',
|
|
|
|
message: __( '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-10 16:58:51 +00:00
|
|
|
addNotice( {
|
|
|
|
status: 'error',
|
|
|
|
message: __(
|
|
|
|
'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 ) {
|
|
|
|
analyticsSettings.forEach( setting => {
|
|
|
|
const updatedValue = state.settings[ setting.name ];
|
|
|
|
wcSettings.wcAdminSettings[ setting.name ] = updatedValue;
|
|
|
|
setting.initialValue = updatedValue;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:04:11 +00:00
|
|
|
saveChanges = () => {
|
2019-05-22 21:43:04 +00:00
|
|
|
this.persistChanges( this.state );
|
2019-05-24 07:22:21 +00:00
|
|
|
this.props.updateSettings( { wc_admin: this.state.settings } );
|
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-07-10 16:58:51 +00:00
|
|
|
const { addNotice } = 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-07-10 16:58:51 +00:00
|
|
|
<HistoricalData addNotice={ addNotice } />
|
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-10 16:58:51 +00:00
|
|
|
const { addNotice } = dispatch( 'wc-admin' );
|
2019-01-31 01:04:11 +00:00
|
|
|
const { updateSettings } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
2019-07-10 16:58:51 +00:00
|
|
|
addNotice,
|
2019-01-31 01:04:11 +00:00
|
|
|
updateSettings,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( useFilters( SETTINGS_FILTER )( Settings ) );
|