2019-05-30 07:15:39 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Button, SelectControl, TextControl } from 'newspack-components';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2019-07-15 11:09:31 +00:00
|
|
|
import { pickBy } from 'lodash';
|
2019-05-30 07:15:39 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
2019-07-01 18:13:29 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
2019-05-30 07:15:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal depdencies
|
|
|
|
*/
|
|
|
|
import { H, Card } from '@woocommerce/components';
|
|
|
|
import withSelect from 'wc-api/with-select';
|
|
|
|
|
|
|
|
class StoreDetails extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
countryStateOptions: [],
|
2019-07-15 11:09:31 +00:00
|
|
|
errors: {},
|
|
|
|
fields: {
|
|
|
|
addressLine1: '',
|
|
|
|
addressLine2: '',
|
|
|
|
city: '',
|
|
|
|
countryState: '',
|
|
|
|
postCode: '',
|
|
|
|
},
|
2019-05-30 07:15:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.onContinue = this.onContinue.bind( this );
|
2019-07-15 11:09:31 +00:00
|
|
|
this.updateValue = this.updateValue.bind( this );
|
2019-05-30 07:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
const countryStateOptions = this.getCountryStateOptions();
|
|
|
|
this.setState( { countryStateOptions } );
|
|
|
|
}
|
|
|
|
|
2019-07-15 11:09:31 +00:00
|
|
|
validateField( name ) {
|
|
|
|
const { errors, fields } = this.state;
|
|
|
|
|
|
|
|
switch ( name ) {
|
|
|
|
case 'addressLine1':
|
|
|
|
errors.addressLine1 = fields.addressLine1.length
|
|
|
|
? null
|
|
|
|
: __( 'Please add an address', 'woocommerce-admin' );
|
|
|
|
break;
|
|
|
|
case 'countryState':
|
|
|
|
errors.countryState = fields.countryState.length
|
|
|
|
? null
|
|
|
|
: __( 'Please select a country and state', 'woocommerce-admin' );
|
|
|
|
break;
|
|
|
|
case 'city':
|
|
|
|
errors.city = fields.city.length ? null : __( 'Please add a city', 'woocommerce-admin' );
|
|
|
|
break;
|
|
|
|
case 'postCode':
|
|
|
|
errors.postCode = fields.postCode.length
|
|
|
|
? null
|
|
|
|
: __( 'Please add a post code', 'woocommerce-admin' );
|
|
|
|
break;
|
2019-05-30 07:15:39 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 11:09:31 +00:00
|
|
|
this.setState( { errors: pickBy( errors ) } );
|
|
|
|
}
|
|
|
|
|
|
|
|
updateValue( name, value ) {
|
|
|
|
const fields = { ...this.state.fields, [ name ]: value };
|
|
|
|
this.setState( { fields }, () => this.validateField( name ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
async validateForm() {
|
|
|
|
const { fields } = this.state;
|
|
|
|
Object.keys( fields ).forEach( fieldName => this.validateField( fieldName ) );
|
2019-05-30 07:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onContinue() {
|
2019-07-15 11:09:31 +00:00
|
|
|
await this.validateForm();
|
|
|
|
if ( Object.keys( this.state.errors ).length ) {
|
2019-05-30 07:15:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-10 16:58:51 +00:00
|
|
|
const { addNotice, goToNextStep, isError, updateSettings } = this.props;
|
2019-07-15 11:09:31 +00:00
|
|
|
const { addressLine1, addressLine2, city, countryState, postCode } = this.state.fields;
|
2019-05-30 07:15:39 +00:00
|
|
|
|
2019-07-01 18:13:29 +00:00
|
|
|
recordEvent( 'storeprofiler_store_details_continue', {
|
|
|
|
store_country: countryState.split( ':' )[ 0 ],
|
|
|
|
} );
|
|
|
|
|
2019-05-30 07:15:39 +00:00
|
|
|
await updateSettings( {
|
|
|
|
general: {
|
|
|
|
woocommerce_store_address: addressLine1,
|
|
|
|
woocommerce_store_address_2: addressLine2,
|
|
|
|
woocommerce_default_country: countryState,
|
|
|
|
woocommerce_store_city: city,
|
|
|
|
woocommerce_store_postcode: postCode,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( ! isError ) {
|
|
|
|
goToNextStep();
|
|
|
|
} else {
|
2019-07-10 16:58:51 +00:00
|
|
|
addNotice( {
|
|
|
|
status: 'error',
|
|
|
|
message: __( 'There was a problem saving your store details.', 'woocommerce-admin' ),
|
|
|
|
} );
|
2019-05-30 07:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCountryStateOptions() {
|
|
|
|
const countries = ( wcSettings.dataEndpoints && wcSettings.dataEndpoints.countries ) || [];
|
|
|
|
|
|
|
|
const countryStateOptions = countries.reduce( ( acc, country ) => {
|
|
|
|
if ( ! country.states.length ) {
|
|
|
|
acc.push( {
|
|
|
|
value: country.code,
|
|
|
|
label: decodeEntities( country.name ),
|
|
|
|
} );
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
const countryStates = country.states.map( state => {
|
|
|
|
return {
|
|
|
|
value: country.code + ':' + state.code,
|
|
|
|
label: decodeEntities( country.name ) + ' -- ' + decodeEntities( state.name ),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
acc.push( ...countryStates );
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
countryStateOptions.unshift( { value: '', label: '' } );
|
|
|
|
|
|
|
|
return countryStateOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-07-15 11:09:31 +00:00
|
|
|
const { countryStateOptions, errors, fields } = this.state;
|
|
|
|
const { addressLine1, addressLine2, city, countryState, postCode } = fields;
|
2019-05-30 07:15:39 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<H className="woocommerce-profile-wizard__header-title">
|
|
|
|
{ __( 'Store Details', 'woocommerce-admin' ) }
|
|
|
|
</H>
|
|
|
|
<H className="woocommerce-profile-wizard__header-subtitle">
|
|
|
|
{ __( 'Tell us about your store', 'woocommerce-admin' ) }
|
|
|
|
</H>
|
|
|
|
|
|
|
|
<Card>
|
|
|
|
<TextControl
|
|
|
|
label={ __( 'Address line 1', 'woocommerce-admin' ) }
|
2019-07-15 11:09:31 +00:00
|
|
|
onChange={ value => this.updateValue( 'addressLine1', value ) }
|
2019-05-30 07:15:39 +00:00
|
|
|
required
|
|
|
|
value={ addressLine1 }
|
2019-07-15 11:09:31 +00:00
|
|
|
help={ errors.addressLine1 }
|
|
|
|
className={ errors.addressLine1 ? 'has-error' : null }
|
2019-05-30 07:15:39 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<TextControl
|
2019-07-09 02:55:58 +00:00
|
|
|
label={ __( 'Address line 2 (optional)', 'woocommerce-admin' ) }
|
2019-07-15 11:09:31 +00:00
|
|
|
onChange={ value => this.updateValue( 'addressLine2', value ) }
|
2019-05-30 07:15:39 +00:00
|
|
|
required
|
|
|
|
value={ addressLine2 }
|
2019-07-15 11:09:31 +00:00
|
|
|
help={ errors.addressLine2 }
|
|
|
|
className={ errors.addressLine2 ? 'has-error' : null }
|
2019-05-30 07:15:39 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<SelectControl
|
|
|
|
label={ __( 'Country / State', 'woocommerce-admin' ) }
|
2019-07-15 11:09:31 +00:00
|
|
|
onChange={ value => this.updateValue( 'countryState', value ) }
|
2019-05-30 07:15:39 +00:00
|
|
|
options={ countryStateOptions }
|
|
|
|
value={ countryState }
|
|
|
|
required
|
2019-07-15 11:09:31 +00:00
|
|
|
help={ errors.countryState }
|
|
|
|
className={ errors.countryState ? 'has-error' : null }
|
2019-05-30 07:15:39 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<TextControl
|
|
|
|
label={ __( 'City', 'woocommerce-admin' ) }
|
2019-07-15 11:09:31 +00:00
|
|
|
onChange={ value => this.updateValue( 'city', value ) }
|
2019-05-30 07:15:39 +00:00
|
|
|
required
|
|
|
|
value={ city }
|
2019-07-15 11:09:31 +00:00
|
|
|
help={ errors.city }
|
|
|
|
className={ errors.city ? 'has-error' : null }
|
2019-05-30 07:15:39 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<TextControl
|
|
|
|
label={ __( 'Post code', 'woocommerce-admin' ) }
|
2019-07-15 11:09:31 +00:00
|
|
|
onChange={ value => this.updateValue( 'postCode', value ) }
|
2019-05-30 07:15:39 +00:00
|
|
|
required
|
|
|
|
value={ postCode }
|
2019-07-15 11:09:31 +00:00
|
|
|
help={ errors.postCode }
|
|
|
|
className={ errors.postCode ? 'has-error' : null }
|
2019-05-30 07:15:39 +00:00
|
|
|
/>
|
|
|
|
|
2019-07-15 11:09:31 +00:00
|
|
|
<Button isPrimary onClick={ this.onContinue }>
|
2019-05-30 07:15:39 +00:00
|
|
|
{ __( 'Continue', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
</Card>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( select => {
|
|
|
|
const { getSettings, getSettingsError, isGetSettingsRequesting } = select( 'wc-api' );
|
|
|
|
|
|
|
|
const settings = getSettings( 'general' );
|
|
|
|
const isError = Boolean( getSettingsError( 'general' ) );
|
|
|
|
const isRequesting = isGetSettingsRequesting( 'general' );
|
|
|
|
|
|
|
|
return { getSettings, isError, isRequesting, settings };
|
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
2019-07-10 16:58:51 +00:00
|
|
|
const { addNotice } = dispatch( 'wc-api' );
|
2019-05-30 07:15:39 +00:00
|
|
|
const { updateSettings } = dispatch( 'wc-api' );
|
|
|
|
|
|
|
|
return {
|
2019-07-10 16:58:51 +00:00
|
|
|
addNotice,
|
2019-05-30 07:15:39 +00:00
|
|
|
updateSettings,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( StoreDetails );
|