2021-11-02 17:33:42 +00:00
/ * *
* External dependencies
* /
import { __ } from '@wordpress/i18n' ;
2021-11-05 20:32:02 +00:00
import { Card , CardBody , Spinner } from '@wordpress/components' ;
2021-11-02 17:33:42 +00:00
import { useDispatch , useSelect } from '@wordpress/data' ;
2022-01-06 12:53:30 +00:00
import { getAdminLink } from '@woocommerce/settings' ;
2021-11-02 17:33:42 +00:00
import {
OPTIONS_STORE_NAME ,
SETTINGS_STORE_NAME ,
2022-04-21 04:44:19 +00:00
TaskType ,
2021-11-02 17:33:42 +00:00
} from '@woocommerce/data' ;
2021-11-05 20:32:02 +00:00
import { queueRecordEvent , recordEvent } from '@woocommerce/tracks' ;
2021-11-02 17:33:42 +00:00
import { registerPlugin } from '@wordpress/plugins' ;
2021-11-05 20:32:02 +00:00
import {
useCallback ,
useEffect ,
useState ,
createElement ,
2024-08-29 11:48:51 +00:00
useMemo ,
2021-11-05 20:32:02 +00:00
} from '@wordpress/element' ;
2021-11-02 17:33:42 +00:00
import { WooOnboardingTask } from '@woocommerce/onboarding' ;
/ * *
* Internal dependencies
* /
2023-07-17 03:32:51 +00:00
import { redirectToTaxSettings } from './utils' ;
2021-11-05 20:32:02 +00:00
import { Card as WooCommerceTaxCard } from './woocommerce-tax/card' ;
2024-08-29 11:48:51 +00:00
import { Card as StripeTaxCard } from './stripe-tax/card' ;
2021-11-02 17:33:42 +00:00
import { createNoticesFromResponse } from '../../../lib/notices' ;
2021-11-05 20:32:02 +00:00
import { getCountryCode } from '~/dashboard/utils' ;
import { ManualConfiguration } from './manual-configuration' ;
import { Partners } from './components/partners' ;
import { WooCommerceTax } from './woocommerce-tax' ;
2022-04-21 04:44:19 +00:00
const TaskCard : React.FC = ( { children } ) = > {
2021-11-05 20:32:02 +00:00
return (
< Card className = "woocommerce-task-card" >
< CardBody > { children } < / CardBody >
< / Card >
) ;
} ;
2021-11-02 17:33:42 +00:00
2022-04-21 04:44:19 +00:00
export type TaxProps = {
onComplete : ( ) = > void ;
query : Record < string , string > ;
task : TaskType ;
} ;
2024-06-25 12:23:07 +00:00
export const Tax : React.FC < TaxProps > = ( { onComplete , query , task } ) = > {
2021-11-02 17:33:42 +00:00
const [ isPending , setIsPending ] = useState ( false ) ;
const { updateOptions } = useDispatch ( OPTIONS_STORE_NAME ) ;
const { createNotice } = useDispatch ( 'core/notices' ) ;
2022-06-21 08:37:34 +00:00
const { updateAndPersistSettingsForGroup } =
useDispatch ( SETTINGS_STORE_NAME ) ;
2021-11-09 12:42:33 +00:00
const { generalSettings , isResolving , taxSettings } = useSelect (
2022-04-25 05:49:11 +00:00
( select ) = > {
2022-08-08 05:25:46 +00:00
const { getSettings , hasFinishedResolution } =
select ( SETTINGS_STORE_NAME ) ;
2021-11-09 12:42:33 +00:00
return {
generalSettings : getSettings ( 'general' ) . general ,
isResolving : ! hasFinishedResolution ( 'getSettings' , [
'general' ,
] ) ,
taxSettings : getSettings ( 'tax' ) . tax || { } ,
} ;
}
) ;
2021-11-02 17:33:42 +00:00
2021-11-05 20:32:02 +00:00
const onManual = useCallback ( async ( ) = > {
2021-11-02 17:33:42 +00:00
setIsPending ( true ) ;
2022-08-08 05:25:46 +00:00
if ( generalSettings ? . woocommerce_calc_taxes !== 'yes' ) {
2021-11-02 17:33:42 +00:00
updateAndPersistSettingsForGroup ( 'tax' , {
tax : {
. . . taxSettings ,
wc_connect_taxes_enabled : 'no' ,
} ,
} ) ;
updateAndPersistSettingsForGroup ( 'general' , {
general : {
. . . generalSettings ,
woocommerce_calc_taxes : 'yes' ,
} ,
} )
. then ( ( ) = > redirectToTaxSettings ( ) )
2022-04-21 04:44:19 +00:00
. catch ( ( error : unknown ) = > {
2021-11-02 17:33:42 +00:00
setIsPending ( false ) ;
createNoticesFromResponse ( error ) ;
} ) ;
} else {
redirectToTaxSettings ( ) ;
}
2024-06-19 20:06:21 +00:00
} , [ generalSettings , taxSettings , updateAndPersistSettingsForGroup ] ) ;
2021-11-02 17:33:42 +00:00
2023-01-19 05:01:31 +00:00
const onAutomate = useCallback ( async ( ) = > {
2021-11-02 17:33:42 +00:00
setIsPending ( true ) ;
2023-01-19 05:01:31 +00:00
try {
await Promise . all ( [
updateAndPersistSettingsForGroup ( 'tax' , {
tax : {
. . . taxSettings ,
wc_connect_taxes_enabled : 'yes' ,
} ,
} ) ,
updateAndPersistSettingsForGroup ( 'general' , {
general : {
. . . generalSettings ,
woocommerce_calc_taxes : 'yes' ,
} ,
} ) ,
] ) ;
} catch ( error : unknown ) {
setIsPending ( false ) ;
createNotice (
'error' ,
__ (
'There was a problem setting up automated taxes. Please try again.' ,
'woocommerce'
)
) ;
return ;
}
2021-11-02 17:33:42 +00:00
createNotice (
'success' ,
__ (
2024-08-20 09:13:17 +00:00
'You’ re awesome! One less item on your to-do list ✅' ,
2022-03-30 09:00:04 +00:00
'woocommerce'
2021-11-02 17:33:42 +00:00
)
) ;
onComplete ( ) ;
2024-06-19 20:06:21 +00:00
} , [
createNotice ,
generalSettings ,
onComplete ,
taxSettings ,
updateAndPersistSettingsForGroup ,
] ) ;
2021-11-02 17:33:42 +00:00
2021-11-05 20:32:02 +00:00
const onDisable = useCallback ( ( ) = > {
2021-11-02 17:33:42 +00:00
setIsPending ( true ) ;
queueRecordEvent ( 'tasklist_tax_connect_store' , {
connect : false ,
no_tax : true ,
} ) ;
updateOptions ( {
woocommerce_no_sales_tax : true ,
woocommerce_calc_taxes : 'no' ,
} ) . then ( ( ) = > {
window . location . href = getAdminLink ( 'admin.php?page=wc-admin' ) ;
} ) ;
2024-06-19 20:06:21 +00:00
} , [ updateOptions ] ) ;
2021-11-05 20:32:02 +00:00
2024-08-29 11:48:51 +00:00
const partners = useMemo ( ( ) = > {
2023-05-09 03:28:57 +00:00
const countryCode =
getCountryCode ( generalSettings ? . woocommerce_default_country ) ||
'' ;
2021-11-05 20:32:02 +00:00
const {
2022-04-21 04:44:19 +00:00
additionalData : {
woocommerceTaxCountries = [ ] ,
2024-08-29 11:48:51 +00:00
stripeTaxCountries = [ ] ,
2022-04-21 04:44:19 +00:00
taxJarActivated ,
2024-06-25 12:23:07 +00:00
woocommerceTaxActivated ,
woocommerceShippingActivated ,
2022-04-21 04:44:19 +00:00
} = { } ,
} = task ;
2021-11-05 20:32:02 +00:00
2024-08-29 11:48:51 +00:00
const allPartners = [
2021-11-05 20:32:02 +00:00
{
id : 'woocommerce-tax' ,
card : WooCommerceTaxCard ,
component : WooCommerceTax ,
isVisible :
! taxJarActivated && // WCS integration doesn't work with the official TaxJar plugin.
2024-06-25 12:23:07 +00:00
! woocommerceTaxActivated &&
! woocommerceShippingActivated &&
2023-05-09 03:28:57 +00:00
woocommerceTaxCountries . includes ( countryCode ) ,
2021-11-05 20:32:02 +00:00
} ,
2024-08-29 11:48:51 +00:00
{
id : 'stripe-tax' ,
card : StripeTaxCard ,
2021-11-05 20:32:02 +00:00
2024-08-29 11:48:51 +00:00
isVisible : stripeTaxCountries.includes ( countryCode ) ,
} ,
] ;
2021-11-02 17:33:42 +00:00
2024-08-29 11:48:51 +00:00
return allPartners . filter ( ( partner ) = > partner . isVisible ) ;
// eslint-disable-next-line react-hooks/exhaustive-deps -- the partner list shouldn't be changing in the middle of interaction. for some reason the country is becoming null in a re-render and causing unexpected behaviour
} , [ ] ) ;
2021-11-05 20:32:02 +00:00
2024-08-29 11:48:51 +00:00
const { auto } = query ;
2021-11-02 17:33:42 +00:00
useEffect ( ( ) = > {
if ( auto === 'true' ) {
onAutomate ( ) ;
2021-11-05 20:32:02 +00:00
}
2024-08-29 11:48:51 +00:00
} , [ auto , onAutomate ] ) ;
2021-11-05 20:32:02 +00:00
2024-08-29 11:48:51 +00:00
useEffect ( ( ) = > {
2021-11-05 20:32:02 +00:00
if ( query . partner ) {
return ;
2021-11-02 17:33:42 +00:00
}
2021-11-05 20:32:02 +00:00
recordEvent ( 'tasklist_tax_view_options' , {
options : partners.map ( ( partner ) = > partner . id ) ,
} ) ;
2024-08-29 11:48:51 +00:00
} , [ partners , query . partner ] ) ;
2021-11-02 17:33:42 +00:00
2024-08-29 11:48:51 +00:00
const currentPartner = useMemo ( ( ) = > {
2021-11-05 20:32:02 +00:00
if ( ! query . partner ) {
return null ;
}
return (
partners . find ( ( partner ) = > partner . id === query . partner ) || null
) ;
2024-08-29 11:48:51 +00:00
} , [ partners , query . partner ] ) ;
2021-11-05 20:32:02 +00:00
2021-11-02 17:33:42 +00:00
const childProps = {
isPending ,
onAutomate ,
onManual ,
onDisable ,
2021-11-09 12:42:33 +00:00
task ,
2021-11-02 17:33:42 +00:00
} ;
2021-11-05 20:32:02 +00:00
if ( isResolving ) {
return < Spinner / > ;
}
if ( ! partners . length ) {
return (
< TaskCard >
< ManualConfiguration { ...childProps } / >
< / TaskCard >
) ;
}
if ( currentPartner ) {
return (
< TaskCard >
2022-04-21 04:44:19 +00:00
{ currentPartner . component &&
createElement ( currentPartner . component , childProps ) }
2021-11-05 20:32:02 +00:00
< / TaskCard >
) ;
}
2021-11-02 17:33:42 +00:00
return (
2021-11-15 20:09:02 +00:00
< Partners { ...childProps } >
2022-04-21 04:44:19 +00:00
{ partners . map (
( partner ) = >
partner . card &&
createElement ( partner . card , {
key : partner.id ,
. . . childProps ,
} )
2021-11-05 20:32:02 +00:00
) }
< / Partners >
2021-11-02 17:33:42 +00:00
) ;
} ;
registerPlugin ( 'wc-admin-onboarding-task-tax' , {
2022-04-21 04:44:19 +00:00
// @ts-expect-error @types/wordpress__plugins need to be updated
2021-11-02 17:33:42 +00:00
scope : 'woocommerce-tasks' ,
render : ( ) = > (
< WooOnboardingTask id = "tax" >
2022-04-21 04:44:19 +00:00
{ ( { onComplete , query , task } : TaxProps ) = > (
2021-11-09 12:42:33 +00:00
< Tax onComplete = { onComplete } query = { query } task = { task } / >
2021-11-02 17:33:42 +00:00
) }
< / WooOnboardingTask >
) ,
} ) ;