| import { CALYPSO_CONTACT } from '@automattic/urls'; |
| import { Field } from '@automattic/wpcom-checkout'; |
| import { CheckboxControl } from '@wordpress/components'; |
| import { useDispatch, useSelect } from '@wordpress/data'; |
| import { useTranslate } from 'i18n-calypso'; |
| import { useState, useEffect, useRef, useCallback } from 'react'; |
| import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation'; |
| import useVatDetails from 'calypso/me/purchases/vat-info/use-vat-details'; |
| import { useTaxName } from 'calypso/my-sites/checkout/src/hooks/use-country-list'; |
| import { useDispatch as useReduxDispatch } from 'calypso/state'; |
| import { recordTracksEvent } from 'calypso/state/analytics/actions'; |
| import useCountryList, { isVatSupported } from '../../hooks/use-country-list'; |
| import { CHECKOUT_STORE } from '../../lib/wpcom-store'; |
|
|
| import './style.scss'; |
|
|
| export function VatForm( { |
| section, |
| isDisabled, |
| countryCode, |
| }: { |
| section: string; |
| isDisabled?: boolean; |
| |
| /** |
| * This is the country code from the parent form, not necessarily from the |
| * VAT details. They may differ. See below for more details. |
| */ |
| countryCode: string | undefined; |
| } ) { |
| const countries = useCountryList(); |
| const translate = useTranslate(); |
| const vatDetailsInForm = useSelect( ( select ) => select( CHECKOUT_STORE ).getVatDetails(), [] ); |
| const wpcomStoreActions = useDispatch( CHECKOUT_STORE ); |
| const taxName = useTaxName( countryCode ?? 'GB' ); |
| const setVatDetailsInForm = wpcomStoreActions?.setVatDetails; |
| const { vatDetails: vatDetailsFromServer, isLoading: isLoadingVatDetails } = useVatDetails(); |
| const [ isFormActive, setIsFormActive ] = useState< boolean >( false ); |
|
|
| const isVatSupportedFor = useCallback( |
| ( code: string ) => |
| countries |
| .filter( isVatSupported ) |
| .some( ( country ) => country.code === code || country.tax_country_codes.includes( code ) ), |
| [ countries ] |
| ); |
|
|
| |
| |
| const previousCountryCode = useRef< string >( '' ); |
| useEffect( () => { |
| if ( |
| ! vatDetailsInForm || |
| ! setVatDetailsInForm || |
| ! countryCode || |
| isLoadingVatDetails || |
| countryCode === previousCountryCode.current |
| ) { |
| return; |
| } |
|
|
| if ( ! isVatSupportedFor( previousCountryCode.current ) && isVatSupportedFor( countryCode ) ) { |
| previousCountryCode.current = countryCode; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| setVatDetailsInForm( { |
| ...vatDetailsFromServer, |
| |
| |
| |
| country: vatDetailsFromServer.country === 'XI' ? vatDetailsFromServer.country : countryCode, |
| } ); |
| |
| |
| setIsFormActive( Boolean( vatDetailsFromServer.id ) ); |
| return; |
| } |
|
|
| previousCountryCode.current = countryCode; |
|
|
| if ( isVatSupportedFor( countryCode ) ) { |
| |
| |
| |
| setVatDetailsInForm( { |
| ...vatDetailsInForm, |
| country: countryCode, |
| } ); |
| return; |
| } |
|
|
| |
| |
| setVatDetailsInForm( {} ); |
| setIsFormActive( false ); |
| }, [ |
| isVatSupportedFor, |
| countryCode, |
| vatDetailsInForm, |
| setVatDetailsInForm, |
| vatDetailsFromServer, |
| isLoadingVatDetails, |
| ] ); |
|
|
| const reduxDispatch = useReduxDispatch(); |
|
|
| if ( |
| ! setVatDetailsInForm || |
| ! countryCode || |
| isLoadingVatDetails || |
| ! isVatSupportedFor( countryCode ) |
| ) { |
| return null; |
| } |
|
|
| const toggleVatForm = ( isChecked: boolean ) => { |
| if ( ! isChecked ) { |
| |
| |
| setVatDetailsInForm( {} ); |
| } else { |
| |
| setVatDetailsInForm( { |
| ...vatDetailsFromServer, |
| |
| |
| country: countryCode, |
| } ); |
| } |
| setIsFormActive( isChecked ); |
| }; |
|
|
| |
| |
| const toggleNorthernIreland = ( isChecked: boolean ) => { |
| if ( ! isChecked ) { |
| setVatDetailsInForm( { |
| ...vatDetailsInForm, |
| country: countryCode, |
| } ); |
| } else { |
| setVatDetailsInForm( { |
| ...vatDetailsInForm, |
| country: 'XI', |
| } ); |
| } |
| }; |
|
|
| const clickSupport = () => { |
| reduxDispatch( recordTracksEvent( 'calypso_vat_details_support_click' ) ); |
| }; |
|
|
| const genericTaxName = |
| |
| translate( 'tax (VAT/GST/CT)' ); |
| const fallbackTaxName = genericTaxName; |
| |
| const addVatText = translate( 'Add %s details', { |
| textOnly: true, |
| args: [ taxName ?? fallbackTaxName ], |
| } ); |
|
|
| if ( ! isFormActive ) { |
| return ( |
| <div className="vat-form__row"> |
| <CheckboxControl |
| className="vat-form__expand-button" |
| checked={ isFormActive } |
| onChange={ toggleVatForm } |
| label={ addVatText } |
| disabled={ isDisabled } |
| /> |
| </div> |
| ); |
| } |
|
|
| return ( |
| <> |
| <div className="vat-form__row"> |
| <CheckboxControl |
| className="vat-form__expand-button" |
| checked={ isFormActive } |
| onChange={ toggleVatForm } |
| label={ addVatText } |
| disabled={ isDisabled || Boolean( vatDetailsFromServer.id ) } |
| /> |
| { countryCode === 'GB' && ( |
| <CheckboxControl |
| className="vat-form__expand-button" |
| checked={ vatDetailsInForm.country === 'XI' } |
| onChange={ toggleNorthernIreland } |
| label={ |
| /* translators: %s is the name of taxes in the country (eg: "VAT" or "GST"). */ |
| translate( 'Is %s for Northern Ireland?', { |
| textOnly: true, |
| args: [ taxName ?? translate( 'VAT', { textOnly: true } ) ], |
| } ) |
| } |
| disabled={ isDisabled } |
| /> |
| ) } |
| </div> |
| <div className="vat-form__row"> |
| <Field |
| id={ section + '-vat-organization' } |
| type="text" |
| label={ |
| /* translators: %s is the name of taxes in the country (eg: "VAT" or "GST"). */ |
| translate( 'Organization for %s', { |
| textOnly: true, |
| args: [ taxName ?? translate( 'VAT', { textOnly: true } ) ], |
| } ) |
| } |
| value={ vatDetailsInForm.name ?? '' } |
| disabled={ isDisabled } |
| onChange={ ( newValue: string ) => { |
| setVatDetailsInForm( { |
| ...vatDetailsInForm, |
| name: newValue, |
| } ); |
| } } |
| /> |
| <Field |
| id={ section + '-vat-id' } |
| type="text" |
| label={ |
| /* translators: %s is the name of taxes in the country (eg: "VAT" or "GST"). */ |
| translate( '%s ID', { |
| textOnly: true, |
| args: [ taxName ?? translate( 'VAT', { textOnly: true } ) ], |
| } ) |
| } |
| value={ vatDetailsInForm.id ?? '' } |
| disabled={ isDisabled || Boolean( vatDetailsFromServer.id ) } |
| onChange={ ( newValue: string ) => { |
| setVatDetailsInForm( { |
| ...vatDetailsInForm, |
| id: newValue, |
| } ); |
| } } |
| prefix={ vatDetailsInForm?.country ? vatDetailsInForm.country : '' } |
| /> |
| </div> |
| <div className="vat-form__row vat-form__row--full-width"> |
| <Field |
| id={ section + '-vat-address' } |
| type="text" |
| label={ |
| /* translators: %s is the name of taxes in the country (eg: "VAT" or "GST"). */ |
| translate( 'Address for %s', { |
| textOnly: true, |
| args: [ taxName ?? translate( 'VAT', { textOnly: true } ) ], |
| } ) |
| } |
| value={ vatDetailsInForm.address ?? '' } |
| autoComplete={ `section-${ section } street-address` } |
| disabled={ isDisabled } |
| onChange={ ( newValue: string ) => { |
| setVatDetailsInForm( { |
| ...vatDetailsInForm, |
| address: newValue, |
| } ); |
| } } |
| /> |
| </div> |
| { vatDetailsFromServer.id && ( |
| <div> |
| <FormSettingExplanation> |
| { translate( |
| /* translators: %s is the name of taxes in the country (eg: "VAT" or "GST"). */ |
| 'To change your %(taxName)s ID, {{contactSupportLink}}please contact support{{/contactSupportLink}}.', |
| { |
| args: { taxName: taxName ?? translate( 'VAT', { textOnly: true } ) }, |
| components: { |
| contactSupportLink: ( |
| <a |
| target="_blank" |
| href={ CALYPSO_CONTACT } |
| rel="noreferrer" |
| onClick={ clickSupport } |
| /> |
| ), |
| }, |
| } |
| ) } |
| </FormSettingExplanation> |
| </div> |
| ) } |
| </> |
| ); |
| } |
|
|