Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move tax rate setup to the Settings page #2749

Merged
merged 10 commits into from
Jan 6, 2025
34 changes: 0 additions & 34 deletions js/src/components/conditional-section.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ exports[`checkErrors Audience When the audience location option is an invalid va

exports[`checkErrors Audience When the audience location option is an invalid value or missing, should not pass 2`] = `"Please select a location option."`;

exports[`checkErrors For tax rate, if store country code or selected country codes include 'US' When the tax rate option is an invalid value or missing, should not pass 1`] = `"Please specify tax rate option."`;

exports[`checkErrors For tax rate, if store country code or selected country codes include 'US' When the tax rate option is an invalid value or missing, should not pass 2`] = `"Please specify tax rate option."`;

exports[`checkErrors For tax rate, if store country code or selected country codes include 'US' When the tax rate option is an invalid value or missing, should not pass 3`] = `"Please specify tax rate option."`;

exports[`checkErrors For tax rate, if store country code or selected country codes include 'US' When the tax rate option is an invalid value or missing, should not pass 4`] = `"Please specify tax rate option."`;

exports[`checkErrors Offer free shipping With flat shipping rate option When there are some non-free shipping rates, and offer free shipping is checked, and there is no minimum order amount for non-free shipping rates, should not pass 1`] = `"Please enter minimum order for free shipping."`;

exports[`checkErrors Shipping rates For flat type When there are any selected countries with shipping rates not set, should not pass 1`] = `"Please specify estimated shipping rates for all the countries, and the rate cannot be less than 0."`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@ import { __ } from '@wordpress/i18n';
const validlocationSet = new Set( [ 'all', 'selected' ] );
const validShippingRateSet = new Set( [ 'automatic', 'flat', 'manual' ] );
const validShippingTimeSet = new Set( [ 'flat', 'manual' ] );
const validTaxRateSet = new Set( [ 'destination', 'manual' ] );

const checkErrors = (
values,
shippingTimes,
finalCountryCodes,
storeCountryCode,
hideTaxRates = false
) => {
const checkErrors = ( values, shippingTimes, finalCountryCodes ) => {
const errors = {};

// Check audience.
Expand Down Expand Up @@ -102,20 +95,6 @@ const checkErrors = (
);
}

/**
* Check tax rate (required for U.S. only).
*/
if (
! hideTaxRates &&
( storeCountryCode === 'US' || finalCountryCodes.includes( 'US' ) ) &&
! validTaxRateSet.has( values.tax_rate )
) {
errors.tax_rate = __(
'Please specify tax rate option.',
'google-listings-and-ads'
);
}

return errors;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe( 'checkErrors', () => {
countries: [ 'US', 'JP' ],
shipping_rate: 'flat',
shipping_time: 'flat',
tax_rate: 'manual',
shipping_country_rates: toRates( [ 'US', 10 ], [ 'JP', 30, 88 ] ),
offer_free_shipping: true,
};
Expand Down Expand Up @@ -436,72 +435,4 @@ describe( 'checkErrors', () => {
} );
} );
} );

describe( `For tax rate, if store country code or selected country codes include 'US'`, () => {
let codes;

beforeEach( () => {
codes = [ 'US' ];
} );

it( `When the tax rate option is an invalid value or missing, should not pass`, () => {
// Not set yet
let errors = checkErrors( defaultFormValues, [], codes );

expect( errors ).toHaveProperty( 'tax_rate' );
expect( errors.tax_rate ).toMatchSnapshot();

errors = checkErrors( defaultFormValues, [], [], 'US' );

expect( errors ).toHaveProperty( 'tax_rate' );
expect( errors.tax_rate ).toMatchSnapshot();

// Invalid value
errors = checkErrors(
{ ...defaultFormValues, tax_rate: true },
[],
codes
);

expect( errors ).toHaveProperty( 'tax_rate' );
expect( errors.tax_rate ).toMatchSnapshot();

// Invalid value
errors = checkErrors(
{ ...defaultFormValues, tax_rate: 'invalid' },
[],
codes
);

expect( errors ).toHaveProperty( 'tax_rate' );
expect( errors.tax_rate ).toMatchSnapshot();
} );

it( 'When the tax rate option is a valid value, should pass', () => {
// Selected destination
const destinationTaxRate = {
...defaultFormValues,
tax_rate: 'destination',
};

let errors = checkErrors( destinationTaxRate, [], codes );

expect( errors ).not.toHaveProperty( 'tax_rate' );

errors = checkErrors( destinationTaxRate, [], [], 'US' );

expect( errors ).not.toHaveProperty( 'tax_rate' );

// Selected manual
const manualTaxRate = { ...defaultFormValues, tax_rate: 'manual' };

errors = checkErrors( manualTaxRate, [], codes );

expect( errors ).not.toHaveProperty( 'tax_rate' );

errors = checkErrors( destinationTaxRate, [], [], 'US' );

expect( errors ).not.toHaveProperty( 'tax_rate' );
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import { useAdaptiveFormContext } from '~/components/adaptive-form';
import StepContent from '~/components/stepper/step-content';
import StepContentActions from '~/components/stepper/step-content-actions';
import StepContentFooter from '~/components/stepper/step-content-footer';
import TaxRate from '~/pages/settings/setup-tax-rate/tax-rate';
import useDisplayTaxRate from '~/pages/settings/setup-tax-rate/useDisplayTaxRate';
import ChooseAudienceSection from '~/components/free-listings/choose-audience-section';
import ShippingRateSection from '~/components/shipping-rate-section';
import ShippingTimeSection from '~/components/free-listings/configure-product-listings/shipping-time-section';
import AppButton from '~/components/app-button';
import ConditionalSection from '~/components/conditional-section';
import OrderValueConditionSection from '~/components/order-value-condition-section';
import isNonFreeShippingRate from '~/utils/isNonFreeShippingRate';

Expand All @@ -25,23 +22,20 @@ import isNonFreeShippingRate from '~/utils/isNonFreeShippingRate';
*
* @param {Object} props React props.
* @param {string} [props.submitLabel="Complete setup"] Submit button label.
* @param {boolean} [props.hideTaxRates] Whether to hide tax rate section.
*/
const FormContent = ( {
submitLabel = __( 'Complete setup', 'google-listings-and-ads' ),
hideTaxRates,
} ) => {
const { values, isValidForm, handleSubmit, adapter } =
useAdaptiveFormContext();
const displayTaxRate = useDisplayTaxRate( adapter.audienceCountries );
const shouldDisplayTaxRate = ! hideTaxRates && displayTaxRate;

const shouldDisplayShippingTime = values.shipping_time === 'flat';
const shouldDisplayOrderValueCondition =
values.shipping_rate === 'flat' &&
values.shipping_country_rates.some( isNonFreeShippingRate );

const handleSubmitClick = ( event ) => {
if ( shouldDisplayTaxRate !== null && isValidForm ) {
if ( isValidForm ) {
return handleSubmit( event );
}

Expand All @@ -56,9 +50,6 @@ const FormContent = ( {
<OrderValueConditionSection />
) }
{ shouldDisplayShippingTime && <ShippingTimeSection /> }
<ConditionalSection show={ shouldDisplayTaxRate }>
<TaxRate />
</ConditionalSection>
<StepContentFooter>
<StepContentActions>
<AppButton
Expand Down
20 changes: 3 additions & 17 deletions js/src/components/free-listings/setup-free-listings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
/**
* Internal dependencies
*/
import useStoreCountry from '~/hooks/useStoreCountry';
import AppSpinner from '~/components/app-spinner';
import Hero from '~/components/free-listings/configure-product-listings/hero';
import AdaptiveForm from '~/components/adaptive-form';
Expand All @@ -32,7 +31,7 @@
*
* If we are adding a new settings field, it should be added into this array.
*/
const settingsFieldNames = [ 'shipping_rate', 'shipping_time', 'tax_rate' ];
const settingsFieldNames = [ 'shipping_rate', 'shipping_time' ];

/**
* Get settings object from Form values.
Expand Down Expand Up @@ -69,7 +68,6 @@
* @param {() => void} [props.onContinue] Callback called once continue button is clicked. Could be async. While it's being resolved the form would turn into a saving state.
* @param {string} [props.submitLabel] Submit button label, to be forwarded to `FormContent`.
* @param {JSX.Element} props.headerTitle Title in the header block of this setup.
* @param {boolean} [props.hideTaxRates=false] Whether to hide tax rate section, to be forwarded to `FormContent`.
*/
const SetupFreeListings = ( {
targetAudience,
Expand All @@ -84,10 +82,8 @@
onContinue = noop,
submitLabel,
headerTitle,
hideTaxRates = false,
} ) => {
const formRef = useRef();
const { code: storeCountryCode } = useStoreCountry();

if ( ! ( targetAudience && settings && shippingRates && shippingTimes ) ) {
return <AppSpinner />;
Expand All @@ -97,13 +93,7 @@
const countries = resolveFinalCountries( values );
const { shipping_country_times: shippingTimesData } = values;

return checkErrors(
values,
shippingTimesData,
countries,
storeCountryCode,
hideTaxRates
);
return checkErrors( values, shippingTimesData, countries );

Check warning on line 96 in js/src/components/free-listings/setup-free-listings/index.js

View check run for this annotation

Codecov / codecov/patch

js/src/components/free-listings/setup-free-listings/index.js#L96

Added line #L96 was not covered by tests
};

const handleChange = ( change, values ) => {
Expand Down Expand Up @@ -220,7 +210,6 @@
// These are the fields for settings.
shipping_rate: settings.shipping_rate,
shipping_time: settings.shipping_time,
tax_rate: settings.tax_rate,
// This is used in UI only, not used in API.
offer_free_shipping:
getOfferFreeShippingInitialValue( shippingRates ),
Expand All @@ -233,10 +222,7 @@
validate={ handleValidate }
onSubmit={ onContinue }
>
<FormContent
submitLabel={ submitLabel }
hideTaxRates={ hideTaxRates }
/>
<FormContent submitLabel={ submitLabel } />
</AdaptiveForm>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const adaptiveFormContextDefaultValues = {
shipping_country_times: [],
shipping_rate: true,
shipping_time: true,
tax_rate: null,
};

const adaptiveFormInputPropsDefaultValues = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ jest.mock( '~/components/adaptive-form', () => ( {
shipping_country_times: [],
shipping_rate: 'flat',
shipping_time: 'flat',
tax_rate: null,
},
};
} ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ const SavedSetupStepper = ( { savedStep } ) => {
targetAudience={ initTargetAudience }
settings={ initSettings }
shippingRates={ initShippingRates }
hideTaxRates={ true }
shippingTimes={ initShippingTimes }
resolveFinalCountries={ getFinalCountries }
onTargetAudienceChange={ handleFormChange.bind(
Expand Down
2 changes: 2 additions & 0 deletions js/src/pages/settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useGoogleAccount from '~/hooks/useGoogleAccount';
import useUpdateRestAPIAuthorizeStatusByUrlQuery from '~/hooks/useUpdateRestAPIAuthorizeStatusByUrlQuery';
import { subpaths, getReconnectAccountUrl } from '~/utils/urls';
import { ContactInformationPreview } from '~/components/contact-information';
import SetupTaxRate from './setup-tax-rate';
import LinkedAccounts from './linked-accounts';
import ReconnectWPComAccount from './reconnect-wpcom-account';
import ReconnectGoogleAccount from './reconnect-google-account';
Expand Down Expand Up @@ -65,6 +66,7 @@ const Settings = () => {
<MainTabNav />
<RebrandingTour />
<ContactInformationPreview />
<SetupTaxRate />
<LinkedAccounts />
</div>
);
Expand Down
1 change: 1 addition & 0 deletions js/src/pages/settings/setup-tax-rate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './setup-tax-rate';
Loading
Loading