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';
|
|
|
|
import { getAdminLink } from '@woocommerce/wc-admin-settings';
|
|
|
|
import {
|
|
|
|
ONBOARDING_STORE_NAME,
|
|
|
|
OPTIONS_STORE_NAME,
|
|
|
|
SETTINGS_STORE_NAME,
|
|
|
|
} 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 { updateQueryString } from '@woocommerce/navigation';
|
|
|
|
import {
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
createElement,
|
|
|
|
} from '@wordpress/element';
|
2021-11-02 17:33:42 +00:00
|
|
|
import { WooOnboardingTask } from '@woocommerce/onboarding';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
redirectToTaxSettings,
|
|
|
|
SettingsSelector,
|
2021-11-05 20:32:02 +00:00
|
|
|
supportsAvalara,
|
2021-11-02 17:33:42 +00:00
|
|
|
} from './utils';
|
2021-11-05 20:32:02 +00:00
|
|
|
import { Card as AvalaraCard } from './avalara/card';
|
|
|
|
import { Card as WooCommerceTaxCard } from './woocommerce-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';
|
|
|
|
|
|
|
|
const TaskCard = ( { children } ) => {
|
|
|
|
return (
|
|
|
|
<Card className="woocommerce-task-card">
|
|
|
|
<CardBody>{ children }</CardBody>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|
2021-11-02 17:33:42 +00:00
|
|
|
|
2021-11-09 12:42:33 +00:00
|
|
|
const Tax = ( { 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' );
|
|
|
|
const { updateAndPersistSettingsForGroup } = useDispatch(
|
|
|
|
SETTINGS_STORE_NAME
|
|
|
|
);
|
2021-11-09 12:42:33 +00:00
|
|
|
const { generalSettings, isResolving, taxSettings } = useSelect(
|
|
|
|
( select ) => {
|
|
|
|
const { getSettings, hasFinishedResolution } = select(
|
|
|
|
SETTINGS_STORE_NAME
|
|
|
|
) as SettingsSelector;
|
2021-11-02 17:33:42 +00:00
|
|
|
|
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 );
|
|
|
|
if ( generalSettings.woocommerce_calc_taxes !== 'yes' ) {
|
|
|
|
updateAndPersistSettingsForGroup( 'tax', {
|
|
|
|
tax: {
|
|
|
|
...taxSettings,
|
|
|
|
wc_connect_taxes_enabled: 'no',
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
updateAndPersistSettingsForGroup( 'general', {
|
|
|
|
general: {
|
|
|
|
...generalSettings,
|
|
|
|
woocommerce_calc_taxes: 'yes',
|
|
|
|
},
|
|
|
|
} )
|
|
|
|
.then( () => redirectToTaxSettings() )
|
|
|
|
.catch( ( error ) => {
|
|
|
|
setIsPending( false );
|
|
|
|
createNoticesFromResponse( error );
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
redirectToTaxSettings();
|
|
|
|
}
|
2021-11-05 20:32:02 +00:00
|
|
|
}, [] );
|
2021-11-02 17:33:42 +00:00
|
|
|
|
2021-11-05 20:32:02 +00:00
|
|
|
const onAutomate = useCallback( () => {
|
2021-11-02 17:33:42 +00:00
|
|
|
setIsPending( true );
|
|
|
|
updateAndPersistSettingsForGroup( 'tax', {
|
|
|
|
tax: {
|
|
|
|
...taxSettings,
|
|
|
|
wc_connect_taxes_enabled: 'yes',
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
updateAndPersistSettingsForGroup( 'general', {
|
|
|
|
general: {
|
|
|
|
...generalSettings,
|
|
|
|
woocommerce_calc_taxes: 'yes',
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
createNotice(
|
|
|
|
'success',
|
|
|
|
__(
|
|
|
|
"You're awesome! One less item on your to-do list ✅",
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
onComplete();
|
2021-11-05 20:32:02 +00:00
|
|
|
}, [] );
|
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' );
|
|
|
|
} );
|
2021-11-05 20:32:02 +00:00
|
|
|
}, [] );
|
|
|
|
|
|
|
|
const getVisiblePartners = () => {
|
|
|
|
const countryCode = getCountryCode(
|
|
|
|
generalSettings?.woocommerce_default_country
|
|
|
|
);
|
|
|
|
const {
|
2021-11-09 12:42:33 +00:00
|
|
|
woocommerceTaxCountries = [],
|
2021-11-05 20:32:02 +00:00
|
|
|
taxJarActivated,
|
2021-11-09 12:42:33 +00:00
|
|
|
} = task.additionalData;
|
2021-11-05 20:32:02 +00:00
|
|
|
|
|
|
|
const partners = [
|
|
|
|
{
|
|
|
|
id: 'woocommerce-tax',
|
|
|
|
card: WooCommerceTaxCard,
|
|
|
|
component: WooCommerceTax,
|
|
|
|
isVisible:
|
|
|
|
! taxJarActivated && // WCS integration doesn't work with the official TaxJar plugin.
|
2021-11-09 12:42:33 +00:00
|
|
|
woocommerceTaxCountries.includes(
|
2021-11-05 20:32:02 +00:00
|
|
|
getCountryCode(
|
|
|
|
generalSettings?.woocommerce_default_country
|
|
|
|
)
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'avalara',
|
|
|
|
card: AvalaraCard,
|
|
|
|
component: null,
|
|
|
|
isVisible: supportsAvalara( countryCode ),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
return partners.filter( ( partner ) => partner.isVisible );
|
2021-11-02 17:33:42 +00:00
|
|
|
};
|
|
|
|
|
2021-11-05 20:32:02 +00:00
|
|
|
const partners = getVisiblePartners();
|
|
|
|
|
2021-11-02 17:33:42 +00:00
|
|
|
useEffect( () => {
|
|
|
|
const { auto } = query;
|
|
|
|
|
|
|
|
if ( auto === 'true' ) {
|
|
|
|
onAutomate();
|
2021-11-05 20:32:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ),
|
|
|
|
} );
|
2021-11-02 17:33:42 +00:00
|
|
|
}, [] );
|
|
|
|
|
2021-11-05 20:32:02 +00:00
|
|
|
const getCurrentPartner = () => {
|
|
|
|
if ( ! query.partner ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
partners.find( ( partner ) => partner.id === query.partner ) || null
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( partners.length > 1 || query.partner ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( partners.length === 1 && partners[ 0 ].component ) {
|
|
|
|
updateQueryString( {
|
|
|
|
partner: partners[ 0 ].id,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}, [ partners ] );
|
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 />;
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentPartner = getCurrentPartner();
|
|
|
|
|
|
|
|
if ( ! partners.length ) {
|
|
|
|
return (
|
|
|
|
<TaskCard>
|
|
|
|
<ManualConfiguration { ...childProps } />
|
|
|
|
</TaskCard>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currentPartner ) {
|
|
|
|
return (
|
|
|
|
<TaskCard>
|
|
|
|
{ createElement( currentPartner.component, childProps ) }
|
|
|
|
</TaskCard>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:33:42 +00:00
|
|
|
return (
|
2021-11-15 20:09:02 +00:00
|
|
|
<Partners { ...childProps }>
|
2021-11-05 20:32:02 +00:00
|
|
|
{ partners.map( ( partner ) =>
|
|
|
|
createElement( partner.card, {
|
|
|
|
key: partner.id,
|
|
|
|
...childProps,
|
|
|
|
} )
|
|
|
|
) }
|
|
|
|
</Partners>
|
2021-11-02 17:33:42 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
registerPlugin( 'wc-admin-onboarding-task-tax', {
|
|
|
|
scope: 'woocommerce-tasks',
|
|
|
|
render: () => (
|
|
|
|
<WooOnboardingTask id="tax">
|
2021-11-09 12:42:33 +00:00
|
|
|
{ ( { onComplete, query, task } ) => (
|
|
|
|
<Tax onComplete={ onComplete } query={ query } task={ task } />
|
2021-11-02 17:33:42 +00:00
|
|
|
) }
|
|
|
|
</WooOnboardingTask>
|
|
|
|
),
|
|
|
|
} );
|