Skip to content

Commit

Permalink
fix set default delivery address country (#2902)
Browse files Browse the repository at this point in the history
  • Loading branch information
tvikito authored Nov 6, 2023
2 parents e463926 + adcc80e commit ec908e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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],
Expand All @@ -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]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,25 @@ 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, '');
setValue(formMeta.fields.deliveryTelephone.name, '');
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;
}

Expand Down

0 comments on commit ec908e8

Please sign in to comment.