Fix displaying ountry are as readable name (#2910)
* Fix displaying country area * Fix displaying country area * Remove local code
This commit is contained in:
parent
e69eb9cc5f
commit
4e0528146b
5 changed files with 38 additions and 8 deletions
|
@ -77,7 +77,9 @@ const AddressEdit: React.FC<AddressEditProps> = props => {
|
||||||
} = props;
|
} = props;
|
||||||
const classes = useStyles(props);
|
const classes = useStyles(props);
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const { areas, isFieldAllowed } = useAddressValidation(data.country);
|
const { areas, isFieldAllowed, getDisplayValue } = useAddressValidation(
|
||||||
|
data.country,
|
||||||
|
);
|
||||||
|
|
||||||
const formErrors = getFormErrors<
|
const formErrors = getFormErrors<
|
||||||
keyof AddressTypeInput,
|
keyof AddressTypeInput,
|
||||||
|
@ -269,7 +271,7 @@ const AddressEdit: React.FC<AddressEditProps> = props => {
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
autocomplete="new-password"
|
autocomplete="new-password"
|
||||||
data-test-id="address-edit-country-area-field"
|
data-test-id="address-edit-country-area-field"
|
||||||
displayValue={data.countryArea}
|
displayValue={getDisplayValue(data.countryArea)}
|
||||||
error={!!formErrors.countryArea}
|
error={!!formErrors.countryArea}
|
||||||
helperText={getErrorMessage(formErrors.countryArea, intl)}
|
helperText={getErrorMessage(formErrors.countryArea, intl)}
|
||||||
label={intl.formatMessage({
|
label={intl.formatMessage({
|
||||||
|
|
|
@ -51,8 +51,8 @@ describe("useAddressValidation", () => {
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
expect(current.areas).toEqual([
|
expect(current.areas).toEqual([
|
||||||
{ label: "Alabama", value: "Alabama" },
|
{ label: "Alabama", value: "Alabama", raw: "AL" },
|
||||||
{ label: "Ancona", value: "Ancona" },
|
{ label: "Ancona", value: "Ancona", raw: "AN" },
|
||||||
]);
|
]);
|
||||||
expect(current.loading).toBeFalsy();
|
expect(current.loading).toBeFalsy();
|
||||||
expect(useAddressValidationRulesQuery).toBeCalledWith({
|
expect(useAddressValidationRulesQuery).toBeCalledWith({
|
||||||
|
|
|
@ -5,10 +5,17 @@ import {
|
||||||
} from "@saleor/graphql";
|
} from "@saleor/graphql";
|
||||||
import { ChoiceValue } from "@saleor/sdk/dist/apollo/types";
|
import { ChoiceValue } from "@saleor/sdk/dist/apollo/types";
|
||||||
|
|
||||||
const prepareChoices = (values: ChoiceValue[]) =>
|
interface AreaChoices {
|
||||||
|
label: string;
|
||||||
|
value: string;
|
||||||
|
raw: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const prepareChoices = (values: ChoiceValue[]): AreaChoices[] =>
|
||||||
values.map(v => ({
|
values.map(v => ({
|
||||||
label: v.verbose,
|
label: v.verbose,
|
||||||
value: v.verbose,
|
value: v.verbose,
|
||||||
|
raw: v.raw,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const selectRules = (data: AddressValidationRulesQuery) =>
|
const selectRules = (data: AddressValidationRulesQuery) =>
|
||||||
|
@ -45,14 +52,33 @@ const useAllowedFields = (data: AddressValidationRulesQuery) => {
|
||||||
return { isAllowed };
|
return { isAllowed };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const useDisplayValues = (areas: AreaChoices[]) => {
|
||||||
|
const isProvinceCode = (code: string) =>
|
||||||
|
code.length === 2 && code.toLocaleUpperCase() === code;
|
||||||
|
|
||||||
|
const getDisplayValue = (value: string) => {
|
||||||
|
if (isProvinceCode(value)) {
|
||||||
|
const area = areas.find(area => area.raw === value);
|
||||||
|
|
||||||
|
return area.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
||||||
|
return { getDisplayValue };
|
||||||
|
};
|
||||||
|
|
||||||
export const useAddressValidation = (country?: string) => {
|
export const useAddressValidation = (country?: string) => {
|
||||||
const { data, loading } = useValidationRules(country);
|
const { data, loading } = useValidationRules(country);
|
||||||
const areas = useAreas(data);
|
const areas = useAreas(data);
|
||||||
const { isAllowed } = useAllowedFields(data);
|
const { isAllowed } = useAllowedFields(data);
|
||||||
|
const { getDisplayValue } = useDisplayValues(areas);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
areas,
|
areas,
|
||||||
isFieldAllowed: isAllowed,
|
isFieldAllowed: isAllowed,
|
||||||
|
getDisplayValue,
|
||||||
loading,
|
loading,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -64,7 +64,9 @@ const CompanyAddressForm: React.FC<CompanyAddressFormProps> = props => {
|
||||||
onChange,
|
onChange,
|
||||||
onCountryChange,
|
onCountryChange,
|
||||||
} = props;
|
} = props;
|
||||||
const { areas, isFieldAllowed } = useAddressValidation(data.country);
|
const { areas, isFieldAllowed, getDisplayValue } = useAddressValidation(
|
||||||
|
data.country,
|
||||||
|
);
|
||||||
const classes = useStyles(props);
|
const classes = useStyles(props);
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
|
|
||||||
|
@ -200,7 +202,7 @@ const CompanyAddressForm: React.FC<CompanyAddressFormProps> = props => {
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
autocomplete="new-password"
|
autocomplete="new-password"
|
||||||
data-test-id="address-edit-country-area-field"
|
data-test-id="address-edit-country-area-field"
|
||||||
displayValue={data.countryArea}
|
displayValue={getDisplayValue(data.countryArea)}
|
||||||
error={!!formErrors.countryArea}
|
error={!!formErrors.countryArea}
|
||||||
helperText={getErrorMessage(formErrors.countryArea, intl)}
|
helperText={getErrorMessage(formErrors.countryArea, intl)}
|
||||||
label={intl.formatMessage({
|
label={intl.formatMessage({
|
||||||
|
|
|
@ -87,7 +87,7 @@ const SiteSettingsPage: React.FC<SiteSettingsPageProps> = props => {
|
||||||
const navigate = useNavigator();
|
const navigate = useNavigator();
|
||||||
|
|
||||||
const [displayCountry, setDisplayCountry] = useStateFromProps(
|
const [displayCountry, setDisplayCountry] = useStateFromProps(
|
||||||
shop?.companyAddress?.country.code || "",
|
shop?.companyAddress?.country.country || "",
|
||||||
);
|
);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
|
Loading…
Reference in a new issue