2019-05-28 14:05:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-05-30 06:31:07 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-05-28 14:05:55 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2020-09-01 08:47:49 +00:00
|
|
|
import {
|
|
|
|
Button,
|
2020-12-21 19:34:22 +00:00
|
|
|
Card,
|
|
|
|
CardBody,
|
|
|
|
CardFooter,
|
2020-09-01 08:47:49 +00:00
|
|
|
CheckboxControl,
|
2022-09-27 08:26:22 +00:00
|
|
|
Spinner,
|
2020-09-01 08:47:49 +00:00
|
|
|
} from '@wordpress/components';
|
2019-05-30 06:31:07 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2020-03-19 12:13:14 +00:00
|
|
|
import { filter, find, findIndex, get } from 'lodash';
|
2020-09-03 21:45:40 +00:00
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
2020-05-28 08:51:40 +00:00
|
|
|
import { ONBOARDING_STORE_NAME, SETTINGS_STORE_NAME } from '@woocommerce/data';
|
2020-12-21 19:34:22 +00:00
|
|
|
import { TextControl } from '@woocommerce/components';
|
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';
|
2019-09-23 21:47:08 +00:00
|
|
|
|
2019-05-30 06:31:07 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import { getCurrencyRegion } from '../../dashboard/utils';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2019-05-28 14:05:55 +00:00
|
|
|
|
2022-01-06 12:53:30 +00:00
|
|
|
const onboarding = getAdminSetting( 'onboarding', {} );
|
2019-09-23 21:47:08 +00:00
|
|
|
|
2019-05-28 14:05:55 +00:00
|
|
|
class Industry extends Component {
|
2019-11-05 00:05:20 +00:00
|
|
|
constructor( props ) {
|
|
|
|
const profileItems = get( props, 'profileItems', {} );
|
2022-12-28 08:23:16 +00:00
|
|
|
let selected = Array.isArray( profileItems.industry )
|
|
|
|
? [ ...profileItems.industry ]
|
|
|
|
: [];
|
2020-04-14 01:41:51 +00:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2019-11-05 00:05:20 +00:00
|
|
|
|
2019-05-28 14:05:55 +00:00
|
|
|
super();
|
|
|
|
this.state = {
|
2019-07-15 11:09:31 +00:00
|
|
|
error: null,
|
2020-04-14 01:41:51 +00:00
|
|
|
selected,
|
2020-03-02 22:22:32 +00:00
|
|
|
textInputListContent: {},
|
2019-05-28 14:05:55 +00:00
|
|
|
};
|
2019-05-30 06:31:07 +00:00
|
|
|
this.onContinue = this.onContinue.bind( this );
|
2020-03-02 22:22:32 +00:00
|
|
|
this.onIndustryChange = this.onIndustryChange.bind( this );
|
|
|
|
this.onDetailChange = this.onDetailChange.bind( this );
|
2022-02-17 19:15:11 +00:00
|
|
|
const selectedSlugs = this.getSelectedSlugs();
|
|
|
|
props.trackStepValueChanges(
|
|
|
|
props.step.key,
|
|
|
|
selectedSlugs,
|
|
|
|
selectedSlugs,
|
|
|
|
this.onContinue
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getSelectedSlugs() {
|
|
|
|
return this.state.selected.map( ( industry ) => industry.slug );
|
|
|
|
}
|
|
|
|
|
2023-04-17 20:47:24 +00:00
|
|
|
componentDidMount() {
|
|
|
|
recordEvent( 'onboarding_site_heuristics', {
|
|
|
|
page_count: onboarding.pageCount,
|
|
|
|
post_count: onboarding.postCount,
|
|
|
|
is_block_theme: onboarding.isBlockTheme,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-02-17 19:15:11 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
this.props.updateCurrentStepValues(
|
|
|
|
this.props.step.key,
|
|
|
|
this.getSelectedSlugs()
|
|
|
|
);
|
2019-05-30 06:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onContinue() {
|
2019-07-15 11:09:31 +00:00
|
|
|
await this.validateField();
|
|
|
|
if ( this.state.error ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-17 19:15:11 +00:00
|
|
|
const { createNotice, isError, updateProfileItems } = this.props;
|
2020-03-02 22:22:32 +00:00
|
|
|
const selectedIndustriesList = this.state.selected.map(
|
|
|
|
( industry ) => industry.slug
|
|
|
|
);
|
2020-03-19 12:13:14 +00:00
|
|
|
|
|
|
|
// Here the selected industries are converted to a string that is a comma separated list
|
|
|
|
const industriesWithDetail = this.state.selected
|
|
|
|
.map( ( industry ) => industry.detail )
|
|
|
|
.filter( ( n ) => n )
|
|
|
|
.join( ',' );
|
2019-05-30 06:31:07 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
recordEvent( 'storeprofiler_store_industry_continue', {
|
2020-03-02 22:22:32 +00:00
|
|
|
store_industry: selectedIndustriesList,
|
|
|
|
industries_with_detail: industriesWithDetail,
|
2020-02-14 02:23:21 +00:00
|
|
|
} );
|
2019-05-30 06:31:07 +00:00
|
|
|
await updateProfileItems( { industry: this.state.selected } );
|
|
|
|
|
2022-02-17 19:15:11 +00:00
|
|
|
if ( isError ) {
|
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 updating your industries',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-02-14 02:23:21 +00:00
|
|
|
)
|
2019-07-23 03:26:46 +00:00
|
|
|
);
|
2022-02-17 19:15:11 +00:00
|
|
|
|
|
|
|
return Promise.reject();
|
2019-05-30 06:31:07 +00:00
|
|
|
}
|
2022-02-17 19:15:11 +00:00
|
|
|
|
|
|
|
return true;
|
2019-05-28 14:05:55 +00:00
|
|
|
}
|
2019-05-30 06:31:07 +00:00
|
|
|
|
2019-07-15 11:09:31 +00:00
|
|
|
async validateField() {
|
|
|
|
const error = this.state.selected.length
|
|
|
|
? null
|
2022-03-30 09:00:04 +00:00
|
|
|
: __( 'Please select at least one industry', 'woocommerce' );
|
2019-07-15 11:09:31 +00:00
|
|
|
this.setState( { error } );
|
|
|
|
}
|
|
|
|
|
2020-03-02 22:22:32 +00:00
|
|
|
onIndustryChange( slug ) {
|
2019-07-15 11:09:31 +00:00
|
|
|
this.setState(
|
2020-02-14 02:23:21 +00:00
|
|
|
( state ) => {
|
2020-03-02 22:22:32 +00:00
|
|
|
const newSelected = state.selected;
|
|
|
|
const selectedIndustry = find( newSelected, { slug } );
|
|
|
|
if ( selectedIndustry ) {
|
|
|
|
const newTextInputListContent = state.textInputListContent;
|
|
|
|
newTextInputListContent[ slug ] = selectedIndustry.detail;
|
2019-07-15 11:09:31 +00:00
|
|
|
return {
|
|
|
|
selected:
|
2020-02-14 02:23:21 +00:00
|
|
|
filter( state.selected, ( value ) => {
|
2020-03-02 22:22:32 +00:00
|
|
|
return value.slug !== slug;
|
2019-07-15 11:09:31 +00:00
|
|
|
} ) || [],
|
2020-03-02 22:22:32 +00:00
|
|
|
textInputListContent: newTextInputListContent,
|
2019-07-15 11:09:31 +00:00
|
|
|
};
|
|
|
|
}
|
2020-03-02 22:22:32 +00:00
|
|
|
newSelected.push( {
|
|
|
|
slug,
|
|
|
|
detail: state.textInputListContent[ slug ],
|
|
|
|
} );
|
2019-05-30 06:31:07 +00:00
|
|
|
return {
|
2019-07-15 11:09:31 +00:00
|
|
|
selected: newSelected,
|
2019-05-30 06:31:07 +00:00
|
|
|
};
|
2019-07-15 11:09:31 +00:00
|
|
|
},
|
|
|
|
() => this.validateField()
|
|
|
|
);
|
2019-05-30 06:31:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 22:22:32 +00:00
|
|
|
onDetailChange( value, slug ) {
|
|
|
|
this.setState( ( state ) => {
|
|
|
|
const newSelected = state.selected;
|
|
|
|
const newTextInputListContent = state.textInputListContent;
|
|
|
|
const industryIndex = findIndex( newSelected, { slug } );
|
|
|
|
newSelected[ industryIndex ].detail = value;
|
|
|
|
newTextInputListContent[ slug ] = value;
|
|
|
|
return {
|
|
|
|
selected: newSelected,
|
|
|
|
textInputListContent: newTextInputListContent,
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2020-12-21 19:34:22 +00:00
|
|
|
renderIndustryLabel( slug, industry, selectedIndustry ) {
|
|
|
|
const { textInputListContent } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ industry.label }
|
|
|
|
{ industry.use_description && selectedIndustry && (
|
|
|
|
<TextControl
|
|
|
|
key={ `text-control-${ slug }` }
|
|
|
|
label={ industry.description_label }
|
|
|
|
value={
|
|
|
|
selectedIndustry.detail ||
|
|
|
|
textInputListContent[ slug ] ||
|
|
|
|
''
|
|
|
|
}
|
|
|
|
onChange={ ( value ) =>
|
|
|
|
this.onDetailChange( value, slug )
|
|
|
|
}
|
|
|
|
className="woocommerce-profile-wizard__text"
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-28 14:05:55 +00:00
|
|
|
render() {
|
2019-09-23 21:47:08 +00:00
|
|
|
const { industries } = onboarding;
|
2020-12-21 19:34:22 +00:00
|
|
|
const { error, selected } = this.state;
|
2020-12-01 22:56:17 +00:00
|
|
|
const { locationSettings, isProfileItemsRequesting } = this.props;
|
2020-04-14 01:41:51 +00:00
|
|
|
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'
|
|
|
|
);
|
2019-11-05 00:05:20 +00:00
|
|
|
|
2019-05-28 14:05:55 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2020-09-01 08:47:49 +00:00
|
|
|
<div className="woocommerce-profile-wizard__step-header">
|
2021-06-28 01:14:59 +00:00
|
|
|
<Text
|
|
|
|
variant="title.small"
|
|
|
|
as="h2"
|
|
|
|
size="20"
|
|
|
|
lineHeight="28px"
|
|
|
|
>
|
2020-09-01 08:47:49 +00:00
|
|
|
{ __(
|
|
|
|
'In which industry does the store operate?',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-09-01 08:47:49 +00:00
|
|
|
) }
|
|
|
|
</Text>
|
2021-06-28 01:14:59 +00:00
|
|
|
<Text variant="body" as="p">
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Choose any that apply', 'woocommerce' ) }
|
2020-09-01 08:47:49 +00:00
|
|
|
</Text>
|
|
|
|
</div>
|
2019-11-04 00:23:26 +00:00
|
|
|
<Card>
|
2020-12-21 19:34:22 +00:00
|
|
|
<CardBody size={ null }>
|
|
|
|
<div className="woocommerce-profile-wizard__checkbox-group">
|
|
|
|
{ filteredIndustryKeys.map( ( slug ) => {
|
|
|
|
const selectedIndustry = find( selected, {
|
|
|
|
slug,
|
|
|
|
} );
|
2020-03-02 22:22:32 +00:00
|
|
|
|
2020-12-21 19:34:22 +00:00
|
|
|
return (
|
2020-03-02 22:22:32 +00:00
|
|
|
<CheckboxControl
|
|
|
|
key={ `checkbox-control-${ slug }` }
|
2020-12-21 19:34:22 +00:00
|
|
|
label={ this.renderIndustryLabel(
|
|
|
|
slug,
|
|
|
|
industries[ slug ],
|
|
|
|
selectedIndustry
|
|
|
|
) }
|
2020-03-02 22:22:32 +00:00
|
|
|
onChange={ () =>
|
|
|
|
this.onIndustryChange( slug )
|
|
|
|
}
|
|
|
|
checked={ selectedIndustry || false }
|
|
|
|
className="woocommerce-profile-wizard__checkbox"
|
|
|
|
/>
|
2020-12-21 19:34:22 +00:00
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
{ error && (
|
|
|
|
<span className="woocommerce-profile-wizard__error">
|
|
|
|
{ error }
|
|
|
|
</span>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</CardBody>
|
|
|
|
<CardFooter isBorderless justify="center">
|
2020-07-15 11:24:00 +00:00
|
|
|
<Button
|
|
|
|
isPrimary
|
2022-02-17 19:15:11 +00:00
|
|
|
onClick={ () => {
|
|
|
|
this.onContinue().then(
|
|
|
|
this.props.goToNextStep
|
|
|
|
);
|
|
|
|
} }
|
2021-01-21 22:53:47 +00:00
|
|
|
isBusy={ isProfileItemsRequesting }
|
2020-12-01 22:56:17 +00:00
|
|
|
disabled={
|
|
|
|
! selected.length || isProfileItemsRequesting
|
|
|
|
}
|
2022-10-03 08:32:23 +00:00
|
|
|
aria-disabled={
|
|
|
|
! selected.length || isProfileItemsRequesting
|
|
|
|
}
|
2020-07-15 11:24:00 +00:00
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Continue', 'woocommerce' ) }
|
2020-07-15 11:24:00 +00:00
|
|
|
</Button>
|
2020-12-21 19:34:22 +00:00
|
|
|
</CardFooter>
|
2019-05-30 06:31:07 +00:00
|
|
|
</Card>
|
2019-05-28 14:05:55 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 07:21:14 +00:00
|
|
|
const Loader = ( props ) => {
|
|
|
|
if ( props.isLoading ) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="woocommerce-admin__industry__spinner"
|
|
|
|
style={ { textAlign: 'center' } }
|
|
|
|
>
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Industry { ...props } />;
|
|
|
|
};
|
|
|
|
|
2019-05-30 06:31:07 +00:00
|
|
|
export default compose(
|
2020-02-14 02:23:21 +00:00
|
|
|
withSelect( ( select ) => {
|
2022-09-27 08:26:22 +00:00
|
|
|
const {
|
|
|
|
getProfileItems,
|
|
|
|
getOnboardingError,
|
|
|
|
isOnboardingRequesting,
|
|
|
|
hasFinishedResolution: hasOnboardingFinishedResolution,
|
|
|
|
} = select( ONBOARDING_STORE_NAME );
|
|
|
|
const {
|
|
|
|
getSettings,
|
|
|
|
hasFinishedResolution: hasSettingsFinishedResolution,
|
|
|
|
} = select( SETTINGS_STORE_NAME );
|
2020-04-14 01:41:51 +00:00
|
|
|
const { general: locationSettings = {} } = getSettings( 'general' );
|
2019-05-30 06:31:07 +00:00
|
|
|
|
2019-11-05 00:05:20 +00:00
|
|
|
return {
|
2020-05-28 08:51:40 +00:00
|
|
|
isError: Boolean( getOnboardingError( 'updateProfileItems' ) ),
|
2019-11-05 00:05:20 +00:00
|
|
|
profileItems: getProfileItems(),
|
2020-04-14 01:41:51 +00:00
|
|
|
locationSettings,
|
2022-06-21 08:37:34 +00:00
|
|
|
isProfileItemsRequesting:
|
|
|
|
isOnboardingRequesting( 'updateProfileItems' ),
|
2022-09-27 08:26:22 +00:00
|
|
|
isLoading:
|
|
|
|
! hasOnboardingFinishedResolution( 'getProfileItems', [] ) ||
|
|
|
|
! hasSettingsFinishedResolution( 'getSettings', [ 'general' ] ),
|
2019-11-05 00:05:20 +00:00
|
|
|
};
|
2019-05-30 06:31:07 +00:00
|
|
|
} ),
|
2020-02-14 02:23:21 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2020-05-28 08:51:40 +00:00
|
|
|
const { updateProfileItems } = dispatch( ONBOARDING_STORE_NAME );
|
2019-07-23 03:26:46 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
2019-05-30 06:31:07 +00:00
|
|
|
|
|
|
|
return {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice,
|
2019-05-30 06:31:07 +00:00
|
|
|
updateProfileItems,
|
|
|
|
};
|
|
|
|
} )
|
2022-09-27 08:26:22 +00:00
|
|
|
)( Loader );
|