From adcc80e51ca0707abbdd00f4406165856e82f170 Mon Sep 17 00:00:00 2001 From: Tomas Vykoukal Date: Tue, 24 Oct 2023 15:51:22 +0200 Subject: [PATCH] fix set default delivery address country --- .../ContactInformation/ContactInformationAddress.tsx | 12 ++++++++++-- .../ContactInformationDeliveryAddress.tsx | 12 +++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/storefront/components/Pages/Order/ContactInformation/ContactInformationAddress.tsx b/storefront/components/Pages/Order/ContactInformation/ContactInformationAddress.tsx index b2077f004d..b990de0257 100644 --- a/storefront/components/Pages/Order/ContactInformation/ContactInformationAddress.tsx +++ b/storefront/components/Pages/Order/ContactInformation/ContactInformationAddress.tsx @@ -5,6 +5,7 @@ import { FormLineError } from 'components/Forms/Lib/FormLineError'; import { Select } from 'components/Forms/Select/Select'; import { TextInputControlled } from 'components/Forms/TextInput/TextInputControlled'; import { useContactInformationFormMeta } from 'components/Pages/Order/ContactInformation/contactInformationFormMeta'; +import { useCurrentCustomerData } from 'connectors/customer/CurrentCustomer'; import { useCountriesQueryApi } from 'graphql/generated'; import { mapCountriesToSelectOptions } from 'helpers/mappers/country'; import useTranslation from 'next-translate/useTranslation'; @@ -20,6 +21,7 @@ export const ContactInformationAddress: FC = () => { const { setValue } = formProviderMethods; const formMeta = useContactInformationFormMeta(formProviderMethods); const [{ data: countriesData }] = useCountriesQueryApi(); + const user = useCurrentCustomerData(); const countriesAsSelectOptions = useMemo( () => mapCountriesToSelectOptions(countriesData?.countries), [countriesData?.countries], @@ -30,8 +32,14 @@ export const ContactInformationAddress: FC = () => { }); useEffect(() => { - if (countriesAsSelectOptions.length > 0 && countryValue.value === '') { - setValue(formMeta.fields.country.name, countriesAsSelectOptions[0], { shouldValidate: true }); + if (countriesAsSelectOptions.length && !countryValue.value) { + const selectedCountryOption = countriesAsSelectOptions.find( + (option) => option.value === user?.country.code, + ); + + setValue(formMeta.fields.country.name, selectedCountryOption || countriesAsSelectOptions[0], { + shouldValidate: true, + }); } }, [countriesAsSelectOptions, countryValue, formMeta.fields.country.name]); diff --git a/storefront/components/Pages/Order/ContactInformation/ContactInformationDeliveryAddress.tsx b/storefront/components/Pages/Order/ContactInformation/ContactInformationDeliveryAddress.tsx index 193665a7d5..0adad78040 100644 --- a/storefront/components/Pages/Order/ContactInformation/ContactInformationDeliveryAddress.tsx +++ b/storefront/components/Pages/Order/ContactInformation/ContactInformationDeliveryAddress.tsx @@ -84,7 +84,11 @@ export const ContactInformationDeliveryAddress: FC = () => { setValue(formMeta.fields.deliveryPostcode.name, deliveryAddress.postcode, { shouldValidate: true }); } } - } else if (deliveryAddressUuid === '') { + } else { + const selectedCountryOption = countriesAsSelectOptions.find( + (option) => option.value === user?.country.code, + ); + setValue(formMeta.fields.deliveryFirstName.name, ''); setValue(formMeta.fields.deliveryLastName.name, ''); setValue(formMeta.fields.deliveryCompanyName.name, ''); @@ -92,11 +96,13 @@ export const ContactInformationDeliveryAddress: FC = () => { setValue(formMeta.fields.deliveryStreet.name, ''); setValue(formMeta.fields.deliveryCity.name, ''); setValue(formMeta.fields.deliveryPostcode.name, ''); - setValue(formMeta.fields.deliveryCountry.name, countriesAsSelectOptions[0], { shouldValidate: true }); + setValue(formMeta.fields.deliveryCountry.name, selectedCountryOption || countriesAsSelectOptions[0], { + shouldValidate: true, + }); } }, [deliveryAddressUuid, countriesAsSelectOptions]); - if (countriesAsSelectOptions.length === 0) { + if (!countriesAsSelectOptions.length) { return null; }