2022-08-02 03:10:34 +00:00
/ * *
* External dependencies
* /
import { FunctionComponent } from 'react' ;
import { TourKit , TourKitTypes } from '@woocommerce/components' ;
import { __ } from '@wordpress/i18n' ;
import { OPTIONS_STORE_NAME } from '@woocommerce/data' ;
2022-09-26 11:52:23 +00:00
import { useState } from '@wordpress/element' ;
import { useSelect } from '@wordpress/data' ;
2022-08-18 02:10:22 +00:00
import { recordEvent } from '@woocommerce/tracks' ;
2022-08-02 03:10:34 +00:00
/ * *
* Internal dependencies
* /
2022-08-12 01:49:18 +00:00
import { EmbeddedBodyProps } from '../embedded-body-layout/embedded-body-props' ;
2022-08-02 03:10:34 +00:00
2022-09-26 11:52:23 +00:00
const STORE_ADDRESS_SETTINGS_OPTION = 'woocommerce_store_address' ;
const STORE_CITY_SETTINGS_OPTION = 'woocommerce_store_city' ;
const STORE_POSTCODE_SETTINGS_OPTION = 'woocommerce_store_postcode' ;
2022-08-02 03:10:34 +00:00
const useShowStoreLocationTour = ( ) = > {
2022-09-26 11:52:23 +00:00
const { hasFilledStoreAddress , isLoading } = useSelect ( ( select ) = > {
const { hasFinishedResolution , getOption } =
select ( OPTIONS_STORE_NAME ) ;
2022-08-02 03:10:34 +00:00
2022-09-26 11:52:23 +00:00
return {
isLoading :
! hasFinishedResolution ( 'getOption' , [
STORE_ADDRESS_SETTINGS_OPTION ,
] ) ||
! hasFinishedResolution ( 'getOption' , [
STORE_CITY_SETTINGS_OPTION ,
] ) ||
! hasFinishedResolution ( 'getOption' , [
STORE_POSTCODE_SETTINGS_OPTION ,
2022-08-02 03:10:34 +00:00
] ) ,
2022-09-26 11:52:23 +00:00
hasFilledStoreAddress :
getOption ( STORE_ADDRESS_SETTINGS_OPTION ) !== '' &&
getOption ( STORE_CITY_SETTINGS_OPTION ) !== '' &&
getOption ( STORE_POSTCODE_SETTINGS_OPTION ) !== '' ,
} ;
} ) ;
2022-08-02 03:10:34 +00:00
return {
isLoading ,
2022-09-26 11:52:23 +00:00
show : ! isLoading && ! hasFilledStoreAddress ,
2022-08-02 03:10:34 +00:00
} ;
} ;
2022-08-18 02:10:22 +00:00
const isFieldFilled = ( fieldSelector : string ) = > {
const field = document . querySelector < HTMLInputElement > ( fieldSelector ) ;
return ! ! field && field . value . length > 0 ;
} ;
2022-08-02 03:10:34 +00:00
const StoreAddressTourOverlay = ( ) = > {
const { isLoading , show } = useShowStoreLocationTour ( ) ;
2022-09-26 11:52:23 +00:00
const [ isDismissed , setIsDismissed ] = useState ( false ) ;
2022-08-02 03:10:34 +00:00
const config : TourKitTypes.WooConfig = {
steps : [
{
referenceElements : {
desktop : '#store_address-description + table.form-table' ,
} ,
meta : {
name : 'store-location-tour-step-1' ,
heading : 'Add your store location' ,
descriptions : {
desktop : __ (
2022-09-26 11:52:23 +00:00
'Add your store location details to help us configure shipping, taxes, currency and more in a fully automated way. Once done, click on the "Save" button at the end of the form.' ,
2022-08-02 03:10:34 +00:00
'woocommerce'
) ,
} ,
primaryButton : {
text : __ ( 'Got it' , 'woocommerce' ) ,
} ,
} ,
} ,
] ,
placement : 'bottom-start' ,
options : {
effects : {
liveResize : { mutation : true , resize : true } ,
spotlight : {
styles : {
inset : '0px auto auto -8px' , // default inset causes all increase in padding to show up on the right side
paddingInline : '8px' , // Add some padding because the spotlight is right on the edge of the text and that's ugly
} ,
interactivity : {
enabled : true ,
} ,
} ,
} ,
} ,
2022-08-18 02:10:22 +00:00
closeHandler : ( _steps , _currentStepIndex , source ) = > {
const fields_filled = {
address_1 : isFieldFilled ( 'input#woocommerce_store_address' ) ,
address_2 : isFieldFilled ( 'input#woocommerce_store_address_2' ) ,
city : isFieldFilled ( 'input#woocommerce_store_city' ) ,
postcode : isFieldFilled ( 'input#woocommerce_store_postcode' ) ,
} ;
recordEvent ( 'settings_store_address_tour_dismiss' , {
source , // 'close-btn' | 'done-btn'
fields_filled ,
} ) ;
2022-09-26 11:52:23 +00:00
setIsDismissed ( true ) ;
2022-08-02 03:10:34 +00:00
} ,
} ;
2022-09-26 11:52:23 +00:00
if ( isDismissed || isLoading || ! show ) {
2022-08-02 03:10:34 +00:00
return null ;
}
return < TourKit config = { config } > < / TourKit > ;
} ;
export const StoreAddressTour : FunctionComponent <
EmbeddedBodyProps & { tutorial : boolean }
> = ( { page , tab , tutorial } ) = > {
if ( page !== 'wc-settings' ) {
return null ;
}
// tab should be general but general is also the default if not set
if ( tab !== 'general' && tab !== undefined ) {
return null ;
}
// only show tour if tutorial query param is set to true, which is the case when referred from onboarding task list
if ( ! tutorial ) {
return null ;
}
return < StoreAddressTourOverlay / > ;
} ;