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 { 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';
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 ) => {
|
2021-11-09 12:42:33 +00:00
|
|
|
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',
|
|
|
|
},
|
|
|
|
} )
|
2022-04-25 06:16:39 +00:00
|
|
|
// @ts-expect-error updateAndPersistSettingsForGroup returns a Promise, but it is not typed in source.
|
2021-11-02 17:33:42 +00:00
|
|
|
.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();
|
|
|
|
}
|
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 ✅",
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2021-11-02 17:33:42 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
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 {
|
2022-04-21 04:44:19 +00:00
|
|
|
additionalData: {
|
|
|
|
woocommerceTaxCountries = [],
|
|
|
|
taxJarActivated,
|
|
|
|
} = {},
|
|
|
|
} = task;
|
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>
|
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>
|
|
|
|
),
|
|
|
|
} );
|