2021-05-14 08:15:15 +00:00
|
|
|
import { TextField } from "@material-ui/core";
|
2020-02-11 14:41:56 +00:00
|
|
|
import FormSpacer from "@saleor/components/FormSpacer";
|
|
|
|
import Grid from "@saleor/components/Grid";
|
|
|
|
import SingleAutocompleteSelectField, {
|
|
|
|
SingleAutocompleteChoiceType
|
|
|
|
} from "@saleor/components/SingleAutocompleteSelectField";
|
|
|
|
import { AddressTypeInput } from "@saleor/customers/types";
|
2020-07-07 10:14:12 +00:00
|
|
|
import { AccountErrorFragment } from "@saleor/fragments/types/AccountErrorFragment";
|
|
|
|
import { ShopErrorFragment } from "@saleor/fragments/types/ShopErrorFragment";
|
|
|
|
import { WarehouseErrorFragment } from "@saleor/fragments/types/WarehouseErrorFragment";
|
2020-02-11 14:41:56 +00:00
|
|
|
import { ChangeEvent } from "@saleor/hooks/useForm";
|
2021-03-30 07:40:18 +00:00
|
|
|
import { makeStyles } from "@saleor/theme";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { getFormErrors } from "@saleor/utils/errors";
|
2020-02-11 14:41:56 +00:00
|
|
|
import getAccountErrorMessage from "@saleor/utils/errors/account";
|
2020-05-14 09:30:32 +00:00
|
|
|
import getShopErrorMessage from "@saleor/utils/errors/shop";
|
2020-03-27 12:21:34 +00:00
|
|
|
import getWarehouseErrorMessage from "@saleor/utils/errors/warehouse";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
|
|
|
import { IntlShape, useIntl } from "react-intl";
|
2020-02-11 14:41:56 +00:00
|
|
|
|
|
|
|
export interface CompanyAddressFormProps {
|
|
|
|
countries: SingleAutocompleteChoiceType[];
|
|
|
|
data: AddressTypeInput;
|
|
|
|
displayCountry: string;
|
2020-03-27 12:21:34 +00:00
|
|
|
errors: Array<
|
|
|
|
AccountErrorFragment | ShopErrorFragment | WarehouseErrorFragment
|
|
|
|
>;
|
2020-02-11 14:41:56 +00:00
|
|
|
disabled: boolean;
|
|
|
|
onChange: (event: ChangeEvent) => void;
|
|
|
|
onCountryChange: (event: ChangeEvent) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const useStyles = makeStyles(
|
|
|
|
{
|
|
|
|
root: {}
|
|
|
|
},
|
|
|
|
{ name: "CompanyAddressForm" }
|
|
|
|
);
|
|
|
|
|
|
|
|
function getErrorMessage(
|
2020-03-27 12:21:34 +00:00
|
|
|
err: AccountErrorFragment | ShopErrorFragment | WarehouseErrorFragment,
|
2020-02-11 14:41:56 +00:00
|
|
|
intl: IntlShape
|
|
|
|
): string {
|
2020-03-27 12:21:34 +00:00
|
|
|
switch (err?.__typename) {
|
|
|
|
case "AccountError":
|
|
|
|
return getAccountErrorMessage(err, intl);
|
|
|
|
case "WarehouseError":
|
|
|
|
return getWarehouseErrorMessage(err, intl);
|
|
|
|
default:
|
|
|
|
return getShopErrorMessage(err, intl);
|
2020-02-11 14:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const CompanyAddressForm: React.FC<CompanyAddressFormProps> = props => {
|
|
|
|
const {
|
|
|
|
countries,
|
|
|
|
data,
|
|
|
|
disabled,
|
|
|
|
displayCountry,
|
|
|
|
errors,
|
|
|
|
onChange,
|
|
|
|
onCountryChange
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const classes = useStyles(props);
|
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
const formFields = [
|
|
|
|
"companyName",
|
|
|
|
"streetAddress1",
|
|
|
|
"streetAddress2",
|
|
|
|
"city",
|
|
|
|
"postalCode",
|
|
|
|
"country",
|
2020-04-29 14:53:52 +00:00
|
|
|
"countryArea",
|
2020-02-11 14:41:56 +00:00
|
|
|
"companyArea",
|
|
|
|
"phone"
|
|
|
|
];
|
|
|
|
const formErrors = getFormErrors(formFields, errors);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classes.root}>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
|
|
|
error={!!formErrors.companyName}
|
|
|
|
helperText={getErrorMessage(formErrors.companyName, intl)}
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Company"
|
|
|
|
})}
|
|
|
|
name={"companyName" as keyof AddressTypeInput}
|
|
|
|
onChange={onChange}
|
|
|
|
value={data.companyName}
|
|
|
|
fullWidth
|
2021-07-12 07:59:56 +00:00
|
|
|
InputProps={{
|
|
|
|
autoComplete: "organization"
|
|
|
|
}}
|
2020-02-11 14:41:56 +00:00
|
|
|
/>
|
|
|
|
<FormSpacer />
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
|
|
|
error={!!formErrors.streetAddress1}
|
|
|
|
helperText={getErrorMessage(formErrors.streetAddress1, intl)}
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Address line 1"
|
|
|
|
})}
|
|
|
|
name={"streetAddress1" as keyof AddressTypeInput}
|
|
|
|
onChange={onChange}
|
|
|
|
value={data.streetAddress1}
|
|
|
|
fullWidth
|
2021-07-12 07:59:56 +00:00
|
|
|
InputProps={{
|
|
|
|
autoComplete: "address-line1"
|
|
|
|
}}
|
2020-02-11 14:41:56 +00:00
|
|
|
/>
|
|
|
|
<FormSpacer />
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
|
|
|
error={!!formErrors.streetAddress2}
|
|
|
|
helperText={getErrorMessage(formErrors.streetAddress2, intl)}
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Address line 2"
|
|
|
|
})}
|
|
|
|
name={"streetAddress2" as keyof AddressTypeInput}
|
|
|
|
onChange={onChange}
|
|
|
|
value={data.streetAddress2}
|
|
|
|
fullWidth
|
2021-07-12 07:59:56 +00:00
|
|
|
InputProps={{
|
|
|
|
autoComplete: "address-line2"
|
|
|
|
}}
|
2020-02-11 14:41:56 +00:00
|
|
|
/>
|
|
|
|
<FormSpacer />
|
|
|
|
<Grid>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
|
|
|
error={!!formErrors.city}
|
|
|
|
helperText={getErrorMessage(formErrors.city, intl)}
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "City"
|
|
|
|
})}
|
|
|
|
name={"city" as keyof AddressTypeInput}
|
|
|
|
onChange={onChange}
|
|
|
|
value={data.city}
|
|
|
|
fullWidth
|
2021-07-12 07:59:56 +00:00
|
|
|
InputProps={{
|
|
|
|
autoComplete: "address-level2"
|
|
|
|
}}
|
2020-02-11 14:41:56 +00:00
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
|
|
|
error={!!formErrors.postalCode}
|
|
|
|
helperText={getErrorMessage(formErrors.postalCode, intl)}
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "ZIP / Postal code"
|
|
|
|
})}
|
|
|
|
name={"postalCode" as keyof AddressTypeInput}
|
|
|
|
onChange={onChange}
|
|
|
|
value={data.postalCode}
|
|
|
|
fullWidth
|
2021-07-12 07:59:56 +00:00
|
|
|
InputProps={{
|
|
|
|
autoComplete: "postal-code"
|
|
|
|
}}
|
2020-02-11 14:41:56 +00:00
|
|
|
/>
|
|
|
|
</Grid>
|
|
|
|
<FormSpacer />
|
|
|
|
<Grid>
|
|
|
|
<SingleAutocompleteSelectField
|
2021-07-06 10:32:09 +00:00
|
|
|
data-test-id="address-edit-country-select-field"
|
2020-02-11 14:41:56 +00:00
|
|
|
disabled={disabled}
|
|
|
|
displayValue={displayCountry}
|
|
|
|
error={!!formErrors.country}
|
|
|
|
helperText={getErrorMessage(formErrors.country, intl)}
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Country"
|
|
|
|
})}
|
|
|
|
name={"country" as keyof AddressTypeInput}
|
|
|
|
onChange={onCountryChange}
|
|
|
|
value={data.country}
|
|
|
|
choices={countries}
|
|
|
|
InputProps={{
|
|
|
|
inputProps: {
|
2020-07-02 10:59:09 +00:00
|
|
|
autoComplete: "none"
|
2020-02-11 14:41:56 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
|
|
|
error={!!formErrors.countryArea}
|
|
|
|
helperText={getErrorMessage(formErrors.countryArea, intl)}
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Country area"
|
|
|
|
})}
|
|
|
|
name={"countryArea" as keyof AddressTypeInput}
|
|
|
|
onChange={onChange}
|
|
|
|
value={data.countryArea}
|
|
|
|
fullWidth
|
2021-07-12 07:59:56 +00:00
|
|
|
InputProps={{
|
|
|
|
autoComplete: "address-level1"
|
|
|
|
}}
|
2020-02-11 14:41:56 +00:00
|
|
|
/>
|
|
|
|
</Grid>
|
|
|
|
<FormSpacer />
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
|
|
|
error={!!formErrors.phone}
|
|
|
|
fullWidth
|
|
|
|
helperText={getErrorMessage(formErrors.phone, intl)}
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Phone"
|
|
|
|
})}
|
|
|
|
name={"phone" as keyof AddressTypeInput}
|
|
|
|
value={data.phone}
|
|
|
|
onChange={onChange}
|
2021-07-12 07:59:56 +00:00
|
|
|
InputProps={{
|
|
|
|
autoComplete: "tel"
|
|
|
|
}}
|
2020-02-11 14:41:56 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
CompanyAddressForm.displayName = "CompanyAddressForm";
|
|
|
|
export default CompanyAddressForm;
|