2019-05-30 07:15:39 +00:00
/ * *
* External dependencies
* /
import { _ _ } from '@wordpress/i18n' ;
2020-07-21 00:12:19 +00:00
import {
Button ,
Card ,
CardBody ,
CardFooter ,
CheckboxControl ,
2020-12-21 01:57:55 +00:00
FlexItem as MaybeFlexItem ,
2020-10-02 01:47:08 +00:00
Popover ,
2020-07-21 00:12:19 +00:00
} from '@wordpress/components' ;
2020-10-02 01:47:08 +00:00
import { Component } from '@wordpress/element' ;
2019-05-30 07:15:39 +00:00
import { compose } from '@wordpress/compose' ;
2020-03-25 03:20:17 +00:00
import { withDispatch , withSelect } from '@wordpress/data' ;
2020-09-01 08:47:49 +00:00
import { Form } from '@woocommerce/components' ;
2021-01-04 18:04:58 +00:00
import { getSetting } from '@woocommerce/wc-admin-settings' ;
2020-05-28 08:51:40 +00:00
import { ONBOARDING _STORE _NAME , SETTINGS _STORE _NAME } from '@woocommerce/data' ;
2020-08-20 04:59:52 +00:00
import { recordEvent } from '@woocommerce/tracks' ;
2021-01-07 23:57:09 +00:00
import { Text } from '@woocommerce/experimental' ;
2021-02-17 22:54:02 +00:00
import { Icon , info } from '@wordpress/icons' ;
2019-12-03 23:32:13 +00:00
/ * *
* Internal dependencies
* /
2020-10-02 01:47:08 +00:00
import { getCountryCode , getCurrencyRegion } from '../../../dashboard/utils' ;
2019-08-21 05:58:47 +00:00
import {
StoreAddress ,
validateStoreAddress ,
2020-10-02 01:47:08 +00:00
} from '../../../dashboard/components/settings/general/store-address' ;
import UsageModal from '../usage-modal' ;
import { CurrencyContext } from '../../../lib/currency-context' ;
import './style.scss' ;
2019-05-30 07:15:39 +00:00
2020-12-21 01:57:55 +00:00
// FlexItem is not available until WP version 5.5. This code is safe to remove
// once the minimum WP supported version becomes 5.5.
const FlextItemSubstitute = ( { children , align } ) => {
const style = {
display : 'flex' ,
'justify-content' : align ? 'center' : 'flex-start' ,
} ;
return < div style = { style } > { children } < / d i v > ;
} ;
const FlexItem = MaybeFlexItem || FlextItemSubstitute ;
2019-05-30 07:15:39 +00:00
class StoreDetails extends Component {
2019-11-05 00:05:20 +00:00
constructor ( props ) {
2020-07-21 00:12:19 +00:00
super ( props ) ;
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-09-01 00:16:53 +00:00
skipping : false ,
2020-10-02 01:47:08 +00:00
isStoreDetailsPopoverVisible : false ,
isSkipSetupPopoverVisible : false ,
2019-10-10 14:05:13 +00:00
} ;
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-10-29 18:34:04 +00:00
deriveCurrencySettings ( countryState ) {
if ( ! countryState ) {
return null ;
}
2021-01-04 18:04:58 +00:00
const Currency = this . context ;
const country = getCountryCode ( countryState ) ;
const { currencySymbols = { } , localeInfo = { } } = getSetting (
'onboarding' ,
{ }
) ;
return Currency . getDataForCountry (
country ,
localeInfo ,
currencySymbols
) ;
2019-10-29 18:34:04 +00:00
}
2020-03-03 09:44:26 +00:00
onSubmit ( ) {
2020-09-01 00:16:53 +00:00
this . setState ( {
showUsageModal : true ,
skipping : false ,
} ) ;
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 ) ,
2021-01-04 18:04:58 +00:00
derived _currency : currencySettings . currency _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
_ _ (
2021-02-10 23:57:51 +00:00
'There was a problem saving your store details' ,
2020-02-14 02:23:21 +00:00
'woocommerce-admin'
)
2019-07-23 03:26:46 +00:00
) ;
2019-05-30 07:15:39 +00:00
}
}
render ( ) {
2020-10-02 01:47:08 +00:00
const {
showUsageModal ,
skipping ,
isStoreDetailsPopoverVisible ,
isSkipSetupPopoverVisible ,
} = this . state ;
2020-12-01 22:56:17 +00:00
const { skipProfiler , isUpdatingProfileItems } = this . props ;
2020-07-21 00:12:19 +00:00
2020-07-28 02:32:58 +00:00
/* eslint-disable @wordpress/i18n-no-collapsible-whitespace */
2020-07-21 00:12:19 +00:00
const skipSetupText = _ _ (
'Manual setup is only recommended for\n experienced WooCommerce users or developers.' ,
'woocommerce-admin'
) ;
const configureCurrencyText = _ _ (
'Your store address will help us configure currency\n options and shipping rules automatically.\n This information will not be publicly visible and can\n easily be changed later.' ,
'woocommerce-admin'
) ;
2020-07-28 02:32:58 +00:00
/* eslint-enable @wordpress/i18n-no-collapsible-whitespace */
2019-11-05 00:05:20 +00:00
2019-05-30 07:15:39 +00:00
return (
2020-10-02 01:47:08 +00:00
< div className = "woocommerce-profile-wizard__store-details" >
2020-09-01 08:47:49 +00:00
< div className = "woocommerce-profile-wizard__step-header" >
< Text variant = "title.small" as = "h2" >
{ _ _ ( 'Welcome to WooCommerce' , 'woocommerce-admin' ) }
< / T e x t >
< Text variant = "body" >
{ _ _ (
"Tell us about your store and we'll get you set up in no time" ,
'woocommerce-admin'
) }
2020-10-02 01:47:08 +00:00
< Button
isTertiary
label = { _ _ (
'Learn more about store details' ,
'woocommerce-admin'
) }
onClick = { ( ) =>
this . setState ( {
isStoreDetailsPopoverVisible : true ,
} )
}
>
2021-02-17 22:54:02 +00:00
< Icon icon = { info } / >
2020-10-02 01:47:08 +00:00
< / B u t t o n >
2020-09-01 08:47:49 +00:00
< / T e x t >
2020-10-02 01:47:08 +00:00
{ isStoreDetailsPopoverVisible && (
< Popover
focusOnMount = "container"
position = "top center"
onClose = { ( ) =>
this . setState ( {
isStoreDetailsPopoverVisible : false ,
} )
}
>
{ configureCurrencyText }
< / P o p o v e r >
) }
2020-09-01 08:47:49 +00:00
< / d i v >
2019-05-30 07:15:39 +00:00
2020-07-21 00:12:19 +00:00
< Form
initialValues = { this . initialValues }
onSubmitCallback = { this . onSubmit }
validate = { validateStoreAddress }
>
{ ( {
getInputProps ,
handleSubmit ,
values ,
isValidForm ,
setValue ,
} ) => (
< Card >
{ showUsageModal && (
< UsageModal
2020-09-01 00:16:53 +00:00
onContinue = { ( ) => {
if ( skipping ) {
skipProfiler ( ) ;
} else {
this . onContinue ( values ) ;
}
} }
2020-07-21 00:12:19 +00:00
onClose = { ( ) =>
this . setState ( {
showUsageModal : false ,
2020-09-01 00:16:53 +00:00
skipping : false ,
2020-07-21 00:12:19 +00:00
} )
}
/ >
) }
< CardBody >
2020-02-14 02:23:21 +00:00
< StoreAddress
getInputProps = { getInputProps }
setValue = { setValue }
/ >
2020-07-21 00:12:19 +00:00
< / C a r d B o d y >
2019-12-02 17:39:22 +00:00
2020-07-21 00:12:19 +00:00
< CardFooter >
2020-12-21 01:57:55 +00:00
< FlexItem >
2020-07-21 00:12:19 +00:00
< div className = "woocommerce-profile-wizard__client" >
< CheckboxControl
label = { _ _ (
"I'm setting up a store for a client" ,
'woocommerce-admin'
) }
{ ... getInputProps ( 'isClient' ) }
/ >
< / d i v >
< / F l e x I t e m >
< / C a r d F o o t e r >
< CardFooter justify = "center" >
2020-12-21 19:34:22 +00:00
< Button
isPrimary
onClick = { handleSubmit }
2021-01-21 22:53:47 +00:00
isBusy = { isUpdatingProfileItems }
2020-12-21 19:34:22 +00:00
disabled = {
! isValidForm || isUpdatingProfileItems
}
>
{ _ _ ( 'Continue' , 'woocommerce-admin' ) }
< / B u t t o n >
2020-07-21 00:12:19 +00:00
< / C a r d F o o t e r >
< / C a r d >
) }
< / F o r m >
< div className = "woocommerce-profile-wizard__footer" >
< Button
isLink
className = "woocommerce-profile-wizard__footer-link"
onClick = { ( ) => {
2020-09-01 00:16:53 +00:00
this . setState ( {
showUsageModal : true ,
skipping : true ,
} ) ;
return false ;
2020-07-21 00:12:19 +00:00
} }
>
2020-09-25 11:48:14 +00:00
{ _ _ (
'Skip setup store details' ,
'woocommerce-admin'
) }
2020-07-21 00:12:19 +00:00
< / B u t t o n >
2020-10-02 01:47:08 +00:00
< Button
isTertiary
label = { skipSetupText }
onClick = { ( ) =>
this . setState ( { isSkipSetupPopoverVisible : true } )
}
>
2021-02-17 22:54:02 +00:00
< Icon icon = { info } / >
2020-10-02 01:47:08 +00:00
< / B u t t o n >
{ isSkipSetupPopoverVisible && (
< Popover
focusOnMount = "container"
position = "top center"
onClose = { ( ) =>
this . setState ( {
isSkipSetupPopoverVisible : false ,
} )
}
>
{ skipSetupText }
< / P o p o v e r >
) }
2020-07-21 00:12:19 +00:00
< / d i v >
2020-10-02 01:47:08 +00:00
< / d i v >
2019-05-30 07:15:39 +00:00
) ;
}
}
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 ) => {
2020-12-01 22:56:17 +00:00
const {
getSettings ,
getSettingsError ,
isUpdateSettingsRequesting ,
} = select ( SETTINGS _STORE _NAME ) ;
const {
getOnboardingError ,
getProfileItems ,
isOnboardingRequesting ,
} = 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' ) ) ;
2020-12-01 22:56:17 +00:00
const isUpdatingProfileItems =
isOnboardingRequesting ( 'updateProfileItems' ) ||
isUpdateSettingsRequesting ( 'general' ) ;
2020-03-25 03:20:17 +00:00
return {
2020-05-28 08:51:40 +00:00
isProfileItemsError ,
2019-10-10 14:05:13 +00:00
isSettingsError ,
2020-05-28 08:51:40 +00:00
profileItems ,
2020-12-01 22:56:17 +00:00
isUpdatingProfileItems ,
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 ) ;