/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { Component } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import { Button, Card, CardBody, CardFooter, TabPanel, __experimentalText as Text, FlexItem, CheckboxControl, } from '@wordpress/components'; import { withDispatch, withSelect } from '@wordpress/data'; import { SelectControl, Form, TextControl } from '@woocommerce/components'; import { ONBOARDING_STORE_NAME, PLUGINS_STORE_NAME, SETTINGS_STORE_NAME, } from '@woocommerce/data'; import { recordEvent } from '@woocommerce/tracks'; /** * Internal dependencies */ import { CurrencyContext } from '~/lib/currency-context'; import { createNoticesFromResponse } from '~/lib/notices'; import { platformOptions } from '../../data/platform-options'; import { employeeOptions } from '../../data/employee-options'; import { sellingVenueOptions } from '../../data/selling-venue-options'; import { getRevenueOptions } from '../../data/revenue-options'; import { getProductCountOptions } from '../../data/product-options'; import { SelectiveExtensionsBundle } from './selective-extensions-bundle'; import './style.scss'; const BUSINESS_DETAILS_TAB_NAME = 'business-details'; const FREE_FEATURES_TAB_NAME = 'free-features'; export const filterBusinessExtensions = ( extensionInstallationOptions ) => { return ( Object.keys( extensionInstallationOptions ) .filter( ( key ) => extensionInstallationOptions[ key ] && key !== 'install_extensions' ) .map( ( key ) => { // Remove anything after : // Please refer to selective-extensions-bundle/index.js // installableExtensions variable // this is to allow duplicate slugs (Tax & Shipping for example) return key.split( ':' )[ 0 ]; } ) // remove duplicate .filter( ( item, index, arr ) => arr.indexOf( item ) === index ) ); }; export const prepareExtensionTrackingData = ( extensionInstallationOptions ) => { const installedExtensions = {}; for ( const [ fieldKey, value ] of Object.entries( extensionInstallationOptions ) ) { const key = fieldKey === 'woocommerce-payments' ? 'install_wcpay' : `install_${ fieldKey.replace( /-/g, '_' ).split( ':', 1 ) }`; if ( fieldKey !== 'install_extensions' && ! installedExtensions[ key ] ) { installedExtensions[ key ] = value; } } return installedExtensions; }; class BusinessDetails extends Component { constructor() { super(); this.state = { isPopoverVisible: false, isValid: false, currentTab: 'business-details', savedValues: null, }; this.onContinue = this.onContinue.bind( this ); this.validate = this.validate.bind( this ); } async onContinue( extensionInstallationOptions ) { const { createNotice, goToNextStep, installAndActivatePlugins, } = this.props; const businessExtensions = filterBusinessExtensions( extensionInstallationOptions ); const installedExtensions = prepareExtensionTrackingData( extensionInstallationOptions ); recordEvent( 'storeprofiler_store_business_features_continue', { all_extensions_installed: Object.values( extensionInstallationOptions ).every( ( val ) => val ), ...installedExtensions, } ); const promises = [ this.persistProfileItems( { business_extensions: businessExtensions, } ), ]; if ( businessExtensions.length ) { promises.push( installAndActivatePlugins( businessExtensions ) .then( ( response ) => { createNoticesFromResponse( response ); } ) .catch( ( error ) => { createNoticesFromResponse( error ); throw new Error(); } ) ); } Promise.all( promises ) .then( () => { goToNextStep(); } ) .catch( () => { createNotice( 'error', __( 'There was a problem updating your business details', 'woocommerce-admin' ) ); } ); } async persistProfileItems( additions = {} ) { const { updateProfileItems, createNotice } = this.props; const { number_employees: numberEmployees, other_platform: otherPlatform, other_platform_name: otherPlatformName, product_count: productCount, revenue, selling_venues: sellingVenues, setup_client: isSetupClient, } = this.state.savedValues; const updates = { number_employees: numberEmployees, other_platform: otherPlatform, other_platform_name: otherPlatform === 'other' ? otherPlatformName : '', product_count: productCount, revenue, selling_venues: sellingVenues, setup_client: isSetupClient, ...additions, }; // Remove possible empty values like `revenue` and `other_platform`. const finalUpdates = Object.entries( updates ).reduce( ( acc, [ key, val ] ) => { if ( val !== '' ) { acc[ key ] = val; } return acc; }, {} ); return updateProfileItems( finalUpdates ).catch( () => { createNotice( 'error', __( 'There was a problem updating your business details', 'woocommerce-admin' ) ); } ); } validate( values ) { const errors = {}; if ( ! values.product_count.length ) { errors.product_count = __( 'This field is required', 'woocommerce-admin' ); } if ( ! values.selling_venues.length ) { errors.selling_venues = __( 'This field is required', 'woocommerce-admin' ); } if ( ! values.other_platform.length && [ 'other', 'brick-mortar-other' ].includes( values.selling_venues ) ) { errors.other_platform = __( 'This field is required', 'woocommerce-admin' ); } if ( ! values.other_platform_name && values.other_platform === 'other' && [ 'other', 'brick-mortar-other' ].includes( values.selling_venues ) ) { errors.other_platform_name = __( 'This field is required', 'woocommerce-admin' ); } if ( ! values.number_employees.length && [ 'other', 'brick-mortar', 'brick-mortar-other', 'other-woocommerce', ].includes( values.selling_venues ) ) { errors.number_employees = __( 'This field is required', 'woocommerce-admin' ); } if ( ! values.revenue.length && [ 'other', 'brick-mortar', 'brick-mortar-other', 'other-woocommerce', ].includes( values.selling_venues ) ) { errors.revenue = __( 'This field is required', 'woocommerce-admin' ); } if ( Object.keys( errors ).length === 0 ) { this.setState( { isValid: true } ); } return errors; } trackBusinessDetailsStep( { number_employees: numberEmployees, other_platform: otherPlatform, other_platform_name: otherPlatformName, product_count: productCount, selling_venues: sellingVenues, revenue, setup_client: isSetupClient, } ) { const { getCurrencyConfig } = this.context; recordEvent( 'storeprofiler_store_business_details_continue_variant', { number_employees: numberEmployees, already_selling: sellingVenues, currency: getCurrencyConfig().code, product_number: productCount, revenue, used_platform: otherPlatform, used_platform_name: otherPlatformName, setup_client: isSetupClient, } ); } renderBusinessDetailsStep() { const { goToNextStep, isInstallingActivating, hasInstallActivateError, } = this.props; const { formatAmount, getCurrencyConfig } = this.context; const productCountOptions = getProductCountOptions( getCurrencyConfig() ); return (
); } renderFreeFeaturesStep() { const { isInstallingActivating, settings, profileItems } = this.props; const country = settings.woocommerce_default_country ? settings.woocommerce_default_country : null; return ( <>