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 Header from 'header';
|
|
|
|
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 = {
|
|
|
|
settings: settings,
|
2019-02-13 11:44:58 +00:00
|
|
|
saving: false,
|
2019-01-31 01:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.handleInputChange = this.handleInputChange.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidCatch( error ) {
|
|
|
|
this.setState( {
|
|
|
|
hasError: true,
|
|
|
|
} );
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.warn( error );
|
|
|
|
/* eslint-enable no-console */
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
const { addNotice, isError, isRequesting } = this.props;
|
|
|
|
const { saving } = this.state;
|
|
|
|
|
|
|
|
if ( saving && ! isRequesting ) {
|
|
|
|
if ( ! isError ) {
|
|
|
|
addNotice( {
|
|
|
|
status: 'success',
|
2019-03-13 17:14:02 +00:00
|
|
|
message: __( 'Your settings have been successfully saved.', 'woocommerce-admin' ),
|
2019-02-13 11:44:58 +00:00
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
addNotice( {
|
|
|
|
status: 'error',
|
2019-03-13 17:14:02 +00:00
|
|
|
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 */
|
|
|
|
this.setState( { saving: false } );
|
|
|
|
/* eslint-enable react/no-did-update-set-state */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:04:11 +00:00
|
|
|
saveChanges = () => {
|
|
|
|
this.props.updateSettings( this.state.settings );
|
2019-02-13 11:44:58 +00:00
|
|
|
this.setState( { saving: true } );
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState( { settings } );
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { hasError } = this.state;
|
|
|
|
if ( hasError ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<Header
|
|
|
|
sections={ [
|
2019-03-13 17:14:02 +00:00
|
|
|
[ '/analytics/revenue', __( 'Analytics', 'woocommerce-admin' ) ],
|
|
|
|
__( 'Settings', 'woocommerce-admin' ),
|
2019-01-31 01:04:11 +00:00
|
|
|
] }
|
|
|
|
/>
|
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-03-13 17:14:02 +00:00
|
|
|
{ __( 'Save Changes', 'woocommerce-admin' ) }
|
2019-01-31 01:04:11 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
2019-05-07 07:18:48 +00:00
|
|
|
<HistoricalData />
|
2019-01-31 01:04:11 +00:00
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
2019-02-13 11:44:58 +00:00
|
|
|
withSelect( select => {
|
|
|
|
const { getSettings, getSettingsError, isGetSettingsRequesting } = select( 'wc-api' );
|
|
|
|
|
|
|
|
const settings = getSettings();
|
|
|
|
const isError = Boolean( getSettingsError() );
|
|
|
|
const isRequesting = isGetSettingsRequesting();
|
|
|
|
|
|
|
|
return { getSettings, isError, isRequesting, settings };
|
|
|
|
} ),
|
2019-01-31 01:04:11 +00:00
|
|
|
withDispatch( dispatch => {
|
2019-02-13 11:44:58 +00:00
|
|
|
const { addNotice } = dispatch( 'wc-admin' );
|
2019-01-31 01:04:11 +00:00
|
|
|
const { updateSettings } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
2019-02-13 11:44:58 +00:00
|
|
|
addNotice,
|
2019-01-31 01:04:11 +00:00
|
|
|
updateSettings,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( useFilters( SETTINGS_FILTER )( Settings ) );
|