2019-05-30 07:15:39 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-11-04 15:50:37 +00:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2019-12-02 17:39:22 +00:00
|
|
|
import { Button, CheckboxControl } from '@wordpress/components';
|
2019-05-30 07:15:39 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
2020-03-25 03:20:17 +00:00
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
2019-07-01 18:13:29 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
2019-05-30 07:15:39 +00:00
|
|
|
|
|
|
|
/**
|
2019-12-03 23:32:13 +00:00
|
|
|
* WooCommerce dependencies
|
2019-05-30 07:15:39 +00:00
|
|
|
*/
|
2019-08-05 01:41:47 +00:00
|
|
|
import { H, Card, Form } from '@woocommerce/components';
|
2019-10-29 18:34:04 +00:00
|
|
|
import { getCurrencyData } from '@woocommerce/currency';
|
2020-05-28 08:51:40 +00:00
|
|
|
import { ONBOARDING_STORE_NAME, SETTINGS_STORE_NAME } from '@woocommerce/data';
|
2019-12-03 23:32:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-12-10 19:01:21 +00:00
|
|
|
import { getCountryCode, getCurrencyRegion } from 'dashboard/utils';
|
2019-08-21 05:58:47 +00:00
|
|
|
import {
|
|
|
|
StoreAddress,
|
|
|
|
validateStoreAddress,
|
2020-04-27 14:56:15 +00:00
|
|
|
} from 'dashboard/components/settings/general/store-address';
|
2019-10-10 14:05:13 +00:00
|
|
|
import UsageModal from './usage-modal';
|
2020-04-02 21:54:38 +00:00
|
|
|
import { CurrencyContext } from 'lib/currency-context';
|
2019-05-30 07:15:39 +00:00
|
|
|
|
|
|
|
class StoreDetails extends Component {
|
2019-11-05 00:05:20 +00:00
|
|
|
constructor( props ) {
|
2019-05-30 07:15:39 +00:00
|
|
|
super( ...arguments );
|
2020-01-21 14:17:39 +00:00
|
|
|
const { profileItems, settings } = props;
|
2019-05-30 07:15:39 +00:00
|
|
|
|
2019-10-10 14:05:13 +00:00
|
|
|
this.state = {
|
|
|
|
showUsageModal: false,
|
|
|
|
};
|
|
|
|
|
2020-01-21 12:57:16 +00:00
|
|
|
// Check if a store address is set so that we don't default
|
|
|
|
// to WooCommerce's default country of the UK.
|
2019-11-15 13:32:54 +00:00
|
|
|
const countryState =
|
2020-02-14 02:23:21 +00:00
|
|
|
( settings.woocommerce_store_address &&
|
|
|
|
settings.woocommerce_default_country ) ||
|
|
|
|
'';
|
2019-11-15 13:32:54 +00:00
|
|
|
|
2019-08-05 01:41:47 +00:00
|
|
|
this.initialValues = {
|
2019-11-05 00:05:20 +00:00
|
|
|
addressLine1: settings.woocommerce_store_address || '',
|
|
|
|
addressLine2: settings.woocommerce_store_address_2 || '',
|
|
|
|
city: settings.woocommerce_store_city || '',
|
2019-11-15 13:32:54 +00:00
|
|
|
countryState,
|
2019-11-05 00:05:20 +00:00
|
|
|
postCode: settings.woocommerce_store_postcode || '',
|
|
|
|
isClient: profileItems.setup_client || false,
|
2019-05-30 07:15:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.onContinue = this.onContinue.bind( this );
|
2019-10-10 14:05:13 +00:00
|
|
|
this.onSubmit = this.onSubmit.bind( this );
|
|
|
|
}
|
|
|
|
|
2019-11-04 15:50:37 +00:00
|
|
|
componentWillUnmount() {
|
2020-02-14 02:23:21 +00:00
|
|
|
apiFetch( {
|
|
|
|
path: '/wc-admin/onboarding/tasks/create_store_pages',
|
|
|
|
method: 'POST',
|
|
|
|
} );
|
2019-11-04 15:50:37 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:34:04 +00:00
|
|
|
deriveCurrencySettings( countryState ) {
|
|
|
|
if ( ! countryState ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-12-10 19:01:21 +00:00
|
|
|
const region = getCurrencyRegion( countryState );
|
2019-10-29 18:34:04 +00:00
|
|
|
const currencyData = getCurrencyData();
|
|
|
|
return currencyData[ region ] || currencyData.US;
|
|
|
|
}
|
|
|
|
|
2020-03-03 09:44:26 +00:00
|
|
|
onSubmit() {
|
|
|
|
this.setState( { showUsageModal: true } );
|
2019-05-30 07:15:39 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 01:41:47 +00:00
|
|
|
async onContinue( values ) {
|
2019-08-01 17:29:35 +00:00
|
|
|
const {
|
|
|
|
createNotice,
|
|
|
|
goToNextStep,
|
|
|
|
isSettingsError,
|
|
|
|
updateProfileItems,
|
|
|
|
isProfileItemsError,
|
2020-03-25 03:20:17 +00:00
|
|
|
updateAndPersistSettingsForGroup,
|
2020-04-14 01:41:51 +00:00
|
|
|
profileItems,
|
2020-06-23 02:47:02 +00:00
|
|
|
settings,
|
2019-08-01 17:29:35 +00:00
|
|
|
} = this.props;
|
2019-05-30 07:15:39 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const currencySettings = this.deriveCurrencySettings(
|
|
|
|
values.countryState
|
|
|
|
);
|
2020-04-02 21:54:38 +00:00
|
|
|
const Currency = this.context;
|
|
|
|
Currency.setCurrency( currencySettings );
|
2019-10-29 18:34:04 +00:00
|
|
|
|
2019-07-01 18:13:29 +00:00
|
|
|
recordEvent( 'storeprofiler_store_details_continue', {
|
2019-08-26 05:49:04 +00:00
|
|
|
store_country: getCountryCode( values.countryState ),
|
2019-10-29 18:34:04 +00:00
|
|
|
derived_currency: currencySettings.code,
|
2019-08-05 01:41:47 +00:00
|
|
|
setup_client: values.isClient,
|
2019-07-01 18:13:29 +00:00
|
|
|
} );
|
|
|
|
|
2020-03-25 03:20:17 +00:00
|
|
|
await updateAndPersistSettingsForGroup( 'general', {
|
2019-05-30 07:15:39 +00:00
|
|
|
general: {
|
2020-06-23 02:47:02 +00:00
|
|
|
...settings,
|
2019-08-05 01:41:47 +00:00
|
|
|
woocommerce_store_address: values.addressLine1,
|
|
|
|
woocommerce_store_address_2: values.addressLine2,
|
|
|
|
woocommerce_default_country: values.countryState,
|
|
|
|
woocommerce_store_city: values.city,
|
|
|
|
woocommerce_store_postcode: values.postCode,
|
2019-10-29 18:34:04 +00:00
|
|
|
woocommerce_currency: currencySettings.code,
|
2019-12-03 23:32:13 +00:00
|
|
|
woocommerce_currency_pos: currencySettings.symbolPosition,
|
2020-02-14 02:23:21 +00:00
|
|
|
woocommerce_price_thousand_sep:
|
|
|
|
currencySettings.thousandSeparator,
|
|
|
|
woocommerce_price_decimal_sep:
|
|
|
|
currencySettings.decimalSeparator,
|
2019-10-29 18:34:04 +00:00
|
|
|
woocommerce_price_num_decimals: currencySettings.precision,
|
2019-05-30 07:15:39 +00:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2020-04-14 01:41:51 +00:00
|
|
|
const profileItemsToUpdate = { setup_client: values.isClient };
|
|
|
|
const region = getCurrencyRegion( values.countryState );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If a user has already selected cdb industry and returns to change to a
|
|
|
|
* non US store, remove cbd industry.
|
|
|
|
*
|
|
|
|
* NOTE: the following call to `updateProfileItems` does not respect the
|
|
|
|
* `await` and performs an update aysnchronously. This means the following
|
|
|
|
* screen may not be initialized with correct profile settings.
|
|
|
|
*
|
|
|
|
* This comment may be removed when a refactor to wp.data datatores is complete.
|
|
|
|
*/
|
2020-04-14 18:46:41 +00:00
|
|
|
if (
|
|
|
|
region !== 'US' &&
|
|
|
|
profileItems.industry &&
|
|
|
|
profileItems.industry.length
|
|
|
|
) {
|
2020-04-14 01:41:51 +00:00
|
|
|
const cbdSlug = 'cbd-other-hemp-derived-products';
|
|
|
|
const trimmedIndustries = profileItems.industry.filter(
|
|
|
|
( industry ) => {
|
|
|
|
return cbdSlug !== industry && cbdSlug !== industry.slug;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
profileItemsToUpdate.industry = trimmedIndustries;
|
|
|
|
}
|
|
|
|
|
|
|
|
await updateProfileItems( profileItemsToUpdate );
|
2019-08-01 17:29:35 +00:00
|
|
|
|
|
|
|
if ( ! isSettingsError && ! isProfileItemsError ) {
|
2019-05-30 07:15:39 +00:00
|
|
|
goToNextStep();
|
|
|
|
} else {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice(
|
|
|
|
'error',
|
2020-02-14 02:23:21 +00:00
|
|
|
__(
|
|
|
|
'There was a problem saving your store details.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
2019-07-23 03:26:46 +00:00
|
|
|
);
|
2019-05-30 07:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-10-10 14:05:13 +00:00
|
|
|
const { showUsageModal } = this.state;
|
2019-11-05 00:05:20 +00:00
|
|
|
|
2019-05-30 07:15:39 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<H className="woocommerce-profile-wizard__header-title">
|
2019-08-08 18:41:26 +00:00
|
|
|
{ __( 'Where is your store based?', 'woocommerce-admin' ) }
|
2019-05-30 07:15:39 +00:00
|
|
|
</H>
|
|
|
|
<H className="woocommerce-profile-wizard__header-subtitle">
|
2019-08-08 18:41:26 +00:00
|
|
|
{ __(
|
|
|
|
'This will help us configure your store and get you started quickly',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-05-30 07:15:39 +00:00
|
|
|
</H>
|
|
|
|
|
|
|
|
<Card>
|
2019-08-05 01:41:47 +00:00
|
|
|
<Form
|
|
|
|
initialValues={ this.initialValues }
|
2019-10-10 14:05:13 +00:00
|
|
|
onSubmitCallback={ this.onSubmit }
|
2019-08-21 05:58:47 +00:00
|
|
|
validate={ validateStoreAddress }
|
2019-08-05 01:41:47 +00:00
|
|
|
>
|
2020-02-14 02:23:21 +00:00
|
|
|
{ ( {
|
|
|
|
getInputProps,
|
|
|
|
handleSubmit,
|
|
|
|
values,
|
|
|
|
isValidForm,
|
|
|
|
setValue,
|
|
|
|
} ) => (
|
2019-08-05 01:41:47 +00:00
|
|
|
<Fragment>
|
2019-10-10 14:05:13 +00:00
|
|
|
{ showUsageModal && (
|
|
|
|
<UsageModal
|
2020-02-14 02:23:21 +00:00
|
|
|
onContinue={ () =>
|
|
|
|
this.onContinue( values )
|
|
|
|
}
|
|
|
|
onClose={ () =>
|
|
|
|
this.setState( {
|
|
|
|
showUsageModal: false,
|
|
|
|
} )
|
|
|
|
}
|
2019-10-10 14:05:13 +00:00
|
|
|
/>
|
|
|
|
) }
|
2020-02-14 02:23:21 +00:00
|
|
|
<StoreAddress
|
|
|
|
getInputProps={ getInputProps }
|
|
|
|
setValue={ setValue }
|
|
|
|
/>
|
2019-12-02 17:39:22 +00:00
|
|
|
|
|
|
|
<div className="woocommerce-profile-wizard__client">
|
|
|
|
<CheckboxControl
|
2020-02-14 02:23:21 +00:00
|
|
|
label={ __(
|
|
|
|
"I'm setting up a store for a client",
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-12-02 17:39:22 +00:00
|
|
|
{ ...getInputProps( 'isClient' ) }
|
|
|
|
/>
|
|
|
|
</div>
|
2019-08-05 01:41:47 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
<Button
|
|
|
|
isPrimary
|
|
|
|
onClick={ handleSubmit }
|
|
|
|
disabled={ ! isValidForm }
|
|
|
|
>
|
2019-08-05 01:41:47 +00:00
|
|
|
{ __( 'Continue', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
</Form>
|
2019-05-30 07:15:39 +00:00
|
|
|
</Card>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 21:54:38 +00:00
|
|
|
StoreDetails.contextType = CurrencyContext;
|
|
|
|
|
2019-05-30 07:15:39 +00:00
|
|
|
export default compose(
|
2020-04-02 21:54:38 +00:00
|
|
|
withSelect( ( select ) => {
|
|
|
|
const {
|
|
|
|
getSettings,
|
|
|
|
getSettingsError,
|
|
|
|
isGetSettingsRequesting,
|
|
|
|
} = select( SETTINGS_STORE_NAME );
|
2020-06-23 02:47:02 +00:00
|
|
|
const { getOnboardingError, getProfileItems } = select(
|
|
|
|
ONBOARDING_STORE_NAME
|
|
|
|
);
|
2020-05-28 08:51:40 +00:00
|
|
|
|
|
|
|
const profileItems = getProfileItems();
|
2020-06-23 02:47:02 +00:00
|
|
|
const isProfileItemsError = Boolean(
|
|
|
|
getOnboardingError( 'updateProfileItems' )
|
|
|
|
);
|
2020-03-25 03:20:17 +00:00
|
|
|
|
|
|
|
const { general: settings = {} } = getSettings( 'general' );
|
|
|
|
const isSettingsError = Boolean( getSettingsError( 'general' ) );
|
|
|
|
const isSettingsRequesting = isGetSettingsRequesting( 'general' );
|
|
|
|
|
|
|
|
return {
|
2020-05-28 08:51:40 +00:00
|
|
|
isProfileItemsError,
|
2019-10-10 14:05:13 +00:00
|
|
|
isSettingsError,
|
|
|
|
isSettingsRequesting,
|
2020-05-28 08:51:40 +00:00
|
|
|
profileItems,
|
2019-10-10 14:05:13 +00:00
|
|
|
settings,
|
|
|
|
};
|
2019-05-30 07:15:39 +00:00
|
|
|
} ),
|
2020-02-14 02:23:21 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2019-07-23 03:26:46 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
2020-05-28 08:51:40 +00:00
|
|
|
const { updateProfileItems } = dispatch( ONBOARDING_STORE_NAME );
|
2020-04-02 21:54:38 +00:00
|
|
|
const { updateAndPersistSettingsForGroup } = dispatch(
|
|
|
|
SETTINGS_STORE_NAME
|
|
|
|
);
|
2019-05-30 07:15:39 +00:00
|
|
|
|
|
|
|
return {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice,
|
2019-08-01 17:29:35 +00:00
|
|
|
updateProfileItems,
|
2020-03-25 03:20:17 +00:00
|
|
|
updateAndPersistSettingsForGroup,
|
2019-05-30 07:15:39 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( StoreDetails );
|