@@ -9,7 +9,7 @@ import * as Form from '@radix-ui/react-form';
99import countries from 'i18n-iso-countries' ;
1010import { useEffect , useState } from 'react' ;
1111import { ButtonVariant } from '@fxa/payments/ui' ;
12- import { validatePostalCode } from '@fxa/payments/ui/actions' ;
12+ import { updateCartAction , validatePostalCode } from '@fxa/payments/ui/actions' ;
1313import { SubmitButton } from '../SubmitButton' ;
1414
1515interface CollapsedProps {
@@ -37,7 +37,9 @@ const Collapsed = ({
3737 variant = { ButtonVariant . Secondary }
3838 data-testid = "tax-location-edit-button"
3939 >
40- < Localized id = "select-tax-location-edit-button" > Edit</ Localized >
40+ < Localized id = "select-tax-location-edit-button" >
41+ < p > Edit</ p >
42+ </ Localized >
4143 </ SubmitButton >
4244 </ Form . Submit >
4345 </ span >
@@ -55,15 +57,19 @@ const Collapsed = ({
5557} ;
5658
5759interface ExpandedProps {
60+ cmsCountryCodes : string [ ] ;
5861 locale : string ;
62+ productName : string ;
5963 unsupportedLocations : string ;
6064 countryCode : string | undefined ;
6165 postalCode : string | undefined ;
6266 saveAction : ( countryCode : string , postalCode : string ) => void ;
6367}
6468
6569const Expanded = ( {
70+ cmsCountryCodes,
6671 locale,
72+ productName,
6773 unsupportedLocations,
6874 countryCode,
6975 postalCode,
@@ -82,6 +88,7 @@ const Expanded = ({
8288 [ key : string ] : boolean ;
8389 } > ( {
8490 missingCountryCode : false ,
91+ productNotAvailable : false ,
8592 unsupportedCountry : false ,
8693 invalidPostalCode : false ,
8794 locationNotUpdated : false ,
@@ -113,6 +120,7 @@ const Expanded = ({
113120 setServerErrors ( ( prev ) => ( {
114121 ...prev ,
115122 missingCountryCode : false ,
123+ productNotAvailable : false ,
116124 unsupportedCountries : false ,
117125 } ) ) ;
118126
@@ -122,10 +130,35 @@ const Expanded = ({
122130
123131 setSelectedCountryCode ( countryCode ) ;
124132
125- if ( unsupportedLocations . includes ( countryCode ) ) {
126- setServerErrors ( ( prev ) => ( { ...prev , unsupportedCountry : true } ) ) ;
133+ // If the selected location is not supported per TOS, it is not necessary to
134+ // also inform the customer that the product is not available in their location.
135+ if (
136+ unsupportedLocations . includes ( countryCode ) &&
137+ ! cmsCountryCodes . includes ( countryCode )
138+ ) {
139+ setServerErrors ( ( prev ) => ( {
140+ ...prev ,
141+ productNotAvailable : false ,
142+ unsupportedCountry : true ,
143+ } ) ) ;
144+ } else if ( unsupportedLocations . includes ( countryCode ) ) {
145+ setServerErrors ( ( prev ) => ( {
146+ ...prev ,
147+ productNotAvailable : false ,
148+ unsupportedCountry : true ,
149+ } ) ) ;
150+ } else if ( ! cmsCountryCodes . includes ( countryCode ) ) {
151+ setServerErrors ( ( prev ) => ( {
152+ ...prev ,
153+ productNotAvailable : true ,
154+ unsupportedCountry : false ,
155+ } ) ) ;
127156 } else {
128- setServerErrors ( ( prev ) => ( { ...prev , unsupportedCountry : false } ) ) ;
157+ setServerErrors ( ( prev ) => ( {
158+ ...prev ,
159+ productNotAvailable : false ,
160+ unsupportedCountry : false ,
161+ } ) ) ;
129162 }
130163 } ;
131164
@@ -206,6 +239,15 @@ const Expanded = ({
206239 </ p >
207240 </ Localized >
208241 </ Form . Message >
242+ { serverErrors . productNotAvailable && (
243+ < Form . Message >
244+ < Localized id = "select-tax-location-product-not-available" >
245+ < p className = "mt-1 text-alert-red" role = "alert" >
246+ { productName } is not available in this location.
247+ </ p >
248+ </ Localized >
249+ </ Form . Message >
250+ ) }
209251 { serverErrors . unsupportedCountry && (
210252 < Form . Message >
211253 < Localized id = "next-location-unsupported" >
@@ -282,22 +324,32 @@ const Expanded = ({
282324 data-testid = "tax-location-save-button"
283325 variant = { ButtonVariant . Secondary }
284326 >
285- < Localized id = "select-tax-location-save-button" > Save</ Localized >
327+ < Localized id = "select-tax-location-save-button" >
328+ < p > Save</ p >
329+ </ Localized >
286330 </ SubmitButton >
287331 </ Form . Submit >
288332 </ Form . Root >
289333 ) ;
290334} ;
291335
292336interface SelectTaxLocationProps {
337+ cartId : string ;
338+ cartVersion : number ;
339+ cmsCountries : string [ ] ;
293340 locale : string ;
341+ productName : string ;
294342 unsupportedLocations : string ;
295343 countryCode : string | undefined ;
296344 postalCode : string | undefined ;
297345}
298346
299347export function SelectTaxLocation ( {
348+ cartId,
349+ cartVersion,
350+ cmsCountries,
300351 locale,
352+ productName,
301353 unsupportedLocations,
302354 countryCode,
303355 postalCode,
@@ -306,6 +358,7 @@ export function SelectTaxLocation({
306358 ! countryCode || ! postalCode
307359 ) ;
308360 const [ alertStatus , setAlertStatus ] = useState < boolean > ( false ) ;
361+ const cmsCountryCodes = cmsCountries . map ( ( country ) => country . slice ( 0 , 2 ) ) ;
309362
310363 return (
311364 < div
@@ -318,15 +371,19 @@ export function SelectTaxLocation({
318371
319372 { expanded ? (
320373 < Expanded
374+ cmsCountryCodes = { cmsCountryCodes }
321375 locale = { locale }
376+ productName = { productName }
322377 unsupportedLocations = { unsupportedLocations }
323378 countryCode = { countryCode }
324379 postalCode = { postalCode }
325- saveAction = { ( countryCode : string , postalCode : string ) => {
380+ saveAction = { async ( countryCode : string , postalCode : string ) => {
326381 setExpanded ( false ) ;
327382
328383 // Call function to save to Cart
329- // await saveTaxLocationAction(countryCode, postalCode);
384+ await updateCartAction ( cartId , cartVersion , {
385+ taxAddress : { countryCode, postalCode } ,
386+ } ) ;
330387 setAlertStatus ( true ) ;
331388 } }
332389 />
0 commit comments