From 61f650a2a2bbd462a0da1fe7a44f6da050364470 Mon Sep 17 00:00:00 2001
From: Paul Sealock
Date: Tue, 14 Apr 2020 13:41:51 +1200
Subject: [PATCH] OBW: allow CBD only for US stores
(https://github.com/woocommerce/woocommerce-admin/pull/4117)
* OBW: allow CBD only for US stores
* fix if user goes back
* industry.js feedback
* store-details feedback
* better naming
---
.../profile-wizard/steps/industry.js | 48 ++++++++++++++++++-
.../profile-wizard/steps/store-details.js | 26 +++++++++-
2 files changed, 71 insertions(+), 3 deletions(-)
diff --git a/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/industry.js b/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/industry.js
index 59af9c826a5..ff1d11811bb 100644
--- a/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/industry.js
+++ b/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/industry.js
@@ -12,11 +12,13 @@ import { withDispatch } from '@wordpress/data';
* WooCommerce Dependencies
*/
import { getSetting } from '@woocommerce/wc-admin-settings';
+import { SETTINGS_STORE_NAME } from '@woocommerce/data';
/**
* Internal dependencies
*/
import { H, Card, TextControl } from '@woocommerce/components';
+import { getCurrencyRegion } from 'dashboard/utils';
import withSelect from 'wc-api/with-select';
import { recordEvent } from 'lib/tracks';
@@ -25,11 +27,38 @@ const onboarding = getSetting( 'onboarding', {} );
class Industry extends Component {
constructor( props ) {
const profileItems = get( props, 'profileItems', {} );
+ let selected = profileItems.industry || [];
+
+ /**
+ * @todo Remove block on `updateProfileItems` refactor to wp.data dataStores.
+ *
+ * The following block is a side effect of wc-api not being truly async
+ * and is a temporary fix until a refactor to wp.data can take place.
+ *
+ * Calls to `updateProfileItems` in the previous screen happen async
+ * and won't be updated in wc-api's state when this component is initialized.
+ * As such, we need to make sure cbd is not initialized as selected when a
+ * user has changed location to non-US based.
+ */
+ const { locationSettings } = props;
+ const region = getCurrencyRegion(
+ locationSettings.woocommerce_default_country
+ );
+
+ if ( region !== 'US' ) {
+ const cbdSlug = 'cbd-other-hemp-derived-products';
+ selected = selected.filter( ( industry ) => {
+ return cbdSlug !== industry && cbdSlug !== industry.slug;
+ } );
+ }
+ /**
+ * End block to be removed after refactor.
+ */
super();
this.state = {
error: null,
- selected: profileItems.industry || [],
+ selected,
textInputListContent: {},
};
this.onContinue = this.onContinue.bind( this );
@@ -130,6 +159,18 @@ class Industry extends Component {
render() {
const { industries } = onboarding;
const { error, selected, textInputListContent } = this.state;
+ const { locationSettings } = this.props;
+ const region = getCurrencyRegion(
+ locationSettings.woocommerce_default_country
+ );
+ const industryKeys = Object.keys( industries );
+
+ const filteredIndustryKeys =
+ region === 'US'
+ ? industryKeys
+ : industryKeys.filter(
+ ( slug ) => slug !== 'cbd-other-hemp-derived-products'
+ );
return (
@@ -144,7 +185,7 @@ class Industry extends Component {
- { Object.keys( industries ).map( ( slug ) => {
+ { filteredIndustryKeys.map( ( slug ) => {
const selectedIndustry = find( selected, { slug } );
return (
@@ -209,10 +250,13 @@ class Industry extends Component {
export default compose(
withSelect( ( select ) => {
const { getProfileItems, getProfileItemsError } = select( 'wc-api' );
+ const { getSettings } = select( SETTINGS_STORE_NAME );
+ const { general: locationSettings = {} } = getSettings( 'general' );
return {
isError: Boolean( getProfileItemsError() ),
profileItems: getProfileItems(),
+ locationSettings,
};
} ),
withDispatch( ( dispatch ) => {
diff --git a/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/store-details.js b/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/store-details.js
index e9fab183c66..4a5d4c76b9d 100644
--- a/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/store-details.js
+++ b/plugins/woocommerce-admin/client/dashboard/profile-wizard/steps/store-details.js
@@ -86,6 +86,7 @@ class StoreDetails extends Component {
updateProfileItems,
isProfileItemsError,
updateAndPersistSettingsForGroup,
+ profileItems,
} = this.props;
const currencySettings = this.deriveCurrencySettings(
@@ -117,7 +118,30 @@ class StoreDetails extends Component {
},
} );
- await updateProfileItems( { setup_client: values.isClient } );
+ 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.
+ */
+ if ( region !== 'US' ) {
+ 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 );
if ( ! isSettingsError && ! isProfileItemsError ) {
goToNextStep();