2019-08-09 10:17:04 +00:00
|
|
|
import Card from "@material-ui/core/Card";
|
|
|
|
import CardContent from "@material-ui/core/CardContent";
|
2019-10-30 14:34:24 +00:00
|
|
|
import { makeStyles } from "@material-ui/core/styles";
|
2019-08-09 10:17:04 +00:00
|
|
|
import TextField from "@material-ui/core/TextField";
|
|
|
|
import React from "react";
|
2020-03-17 18:49:01 +00:00
|
|
|
import { useIntl, IntlShape } from "react-intl";
|
2019-08-09 10:17:04 +00:00
|
|
|
|
|
|
|
import CardTitle from "@saleor/components/CardTitle";
|
|
|
|
import FormSpacer from "@saleor/components/FormSpacer";
|
|
|
|
import Grid from "@saleor/components/Grid";
|
|
|
|
import SingleAutocompleteSelectField, {
|
|
|
|
SingleAutocompleteChoiceType
|
|
|
|
} from "@saleor/components/SingleAutocompleteSelectField";
|
|
|
|
import { ChangeEvent } from "@saleor/hooks/useForm";
|
2020-03-17 18:49:01 +00:00
|
|
|
import { getFormErrors } from "@saleor/utils/errors";
|
|
|
|
import { ShopErrorFragment } from "@saleor/siteSettings/types/ShopErrorFragment";
|
|
|
|
import getShopErrorMessage from "@saleor/utils/errors/shop";
|
|
|
|
import { AccountErrorFragment } from "@saleor/customers/types/AccountErrorFragment";
|
|
|
|
import getAccountErrorMessage from "@saleor/utils/errors/account";
|
2020-01-30 13:17:29 +00:00
|
|
|
import { AddressTypeInput } from "@saleor/customers/types";
|
2019-08-09 10:17:04 +00:00
|
|
|
|
2020-01-30 13:17:29 +00:00
|
|
|
interface CompanyAddressInputProps {
|
2019-08-09 10:17:04 +00:00
|
|
|
countries: SingleAutocompleteChoiceType[];
|
2020-01-30 13:17:29 +00:00
|
|
|
data: AddressTypeInput;
|
2019-08-09 10:17:04 +00:00
|
|
|
displayCountry: string;
|
2020-03-17 18:49:01 +00:00
|
|
|
errors: Array<AccountErrorFragment | ShopErrorFragment>;
|
2019-08-09 10:17:04 +00:00
|
|
|
disabled: boolean;
|
2020-01-30 13:17:29 +00:00
|
|
|
header: string;
|
2019-08-09 10:17:04 +00:00
|
|
|
onChange: (event: ChangeEvent) => void;
|
|
|
|
onCountryChange: (event: ChangeEvent) => void;
|
|
|
|
}
|
|
|
|
|
2019-12-03 15:28:40 +00:00
|
|
|
const useStyles = makeStyles(
|
|
|
|
{
|
|
|
|
root: {
|
|
|
|
overflow: "visible"
|
|
|
|
}
|
|
|
|
},
|
2020-01-30 13:17:29 +00:00
|
|
|
{ name: "CompanyAddressInput" }
|
2019-12-03 15:28:40 +00:00
|
|
|
);
|
2019-08-09 10:17:04 +00:00
|
|
|
|
2020-03-17 18:49:01 +00:00
|
|
|
function getErrorMessage(
|
|
|
|
err: AccountErrorFragment | ShopErrorFragment,
|
|
|
|
intl: IntlShape
|
|
|
|
): string {
|
|
|
|
if (err?.__typename === "AccountError") {
|
|
|
|
return getAccountErrorMessage(err, intl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getShopErrorMessage(err, intl);
|
|
|
|
}
|
|
|
|
|
2020-01-30 13:17:29 +00:00
|
|
|
const CompanyAddressInput: React.FC<CompanyAddressInputProps> = props => {
|
2019-10-30 14:34:24 +00:00
|
|
|
const {
|
2019-08-09 10:17:04 +00:00
|
|
|
countries,
|
|
|
|
data,
|
|
|
|
disabled,
|
|
|
|
displayCountry,
|
|
|
|
errors,
|
2020-01-30 13:17:29 +00:00
|
|
|
header,
|
2019-08-09 10:17:04 +00:00
|
|
|
onChange,
|
|
|
|
onCountryChange
|
2019-10-30 14:34:24 +00:00
|
|
|
} = props;
|
2019-08-26 21:39:21 +00:00
|
|
|
|
2020-03-17 18:49:01 +00:00
|
|
|
const classes = useStyles(props);
|
2019-10-30 14:34:24 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
|
2020-03-17 18:49:01 +00:00
|
|
|
const formFields = [
|
|
|
|
"companyName",
|
|
|
|
"streetAddress1",
|
|
|
|
"streetAddress2",
|
|
|
|
"city",
|
|
|
|
"postalCode",
|
|
|
|
"country",
|
|
|
|
"companyArea",
|
|
|
|
"phone"
|
|
|
|
];
|
|
|
|
const formErrors = getFormErrors(formFields, errors);
|
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
return (
|
|
|
|
<Card className={classes.root}>
|
2020-01-30 13:17:29 +00:00
|
|
|
<CardTitle title={header} />
|
2019-10-30 14:34:24 +00:00
|
|
|
<CardContent>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
2020-03-17 18:49:01 +00:00
|
|
|
error={!!formErrors.companyName}
|
|
|
|
helperText={getErrorMessage(formErrors.companyName, intl)}
|
2019-10-30 14:34:24 +00:00
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Company"
|
|
|
|
})}
|
2020-01-30 13:17:29 +00:00
|
|
|
name={"companyName" as keyof AddressTypeInput}
|
2019-10-30 14:34:24 +00:00
|
|
|
onChange={onChange}
|
|
|
|
value={data.companyName}
|
|
|
|
fullWidth
|
|
|
|
/>
|
|
|
|
<FormSpacer />
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
2020-03-17 18:49:01 +00:00
|
|
|
error={!!formErrors.streetAddress1}
|
|
|
|
helperText={getErrorMessage(formErrors.streetAddress1, intl)}
|
2019-10-30 14:34:24 +00:00
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Address line 1"
|
2019-08-26 21:39:21 +00:00
|
|
|
})}
|
2020-01-30 13:17:29 +00:00
|
|
|
name={"streetAddress1" as keyof AddressTypeInput}
|
2019-10-30 14:34:24 +00:00
|
|
|
onChange={onChange}
|
|
|
|
value={data.streetAddress1}
|
|
|
|
fullWidth
|
2019-08-09 10:17:04 +00:00
|
|
|
/>
|
2019-10-30 14:34:24 +00:00
|
|
|
<FormSpacer />
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
2020-03-17 18:49:01 +00:00
|
|
|
error={!!formErrors.streetAddress2}
|
|
|
|
helperText={getErrorMessage(formErrors.streetAddress2, intl)}
|
2019-10-30 14:34:24 +00:00
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Address line 2"
|
|
|
|
})}
|
2020-01-30 13:17:29 +00:00
|
|
|
name={"streetAddress2" as keyof AddressTypeInput}
|
2019-10-30 14:34:24 +00:00
|
|
|
onChange={onChange}
|
|
|
|
value={data.streetAddress2}
|
|
|
|
fullWidth
|
|
|
|
/>
|
|
|
|
<FormSpacer />
|
|
|
|
<Grid>
|
2019-08-09 10:17:04 +00:00
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
2020-03-17 18:49:01 +00:00
|
|
|
error={!!formErrors.city}
|
|
|
|
helperText={getErrorMessage(formErrors.city, intl)}
|
2019-08-26 21:39:21 +00:00
|
|
|
label={intl.formatMessage({
|
2019-10-30 14:34:24 +00:00
|
|
|
defaultMessage: "City"
|
2019-08-26 21:39:21 +00:00
|
|
|
})}
|
2020-01-30 13:17:29 +00:00
|
|
|
name={"city" as keyof AddressTypeInput}
|
2019-08-09 10:17:04 +00:00
|
|
|
onChange={onChange}
|
2019-10-30 14:34:24 +00:00
|
|
|
value={data.city}
|
2019-08-09 10:17:04 +00:00
|
|
|
fullWidth
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
2020-03-17 18:49:01 +00:00
|
|
|
error={!!formErrors.postalCode}
|
|
|
|
helperText={getErrorMessage(formErrors.postalCode, intl)}
|
2019-08-26 21:39:21 +00:00
|
|
|
label={intl.formatMessage({
|
2019-10-30 14:34:24 +00:00
|
|
|
defaultMessage: "ZIP / Postal code"
|
2019-08-26 21:39:21 +00:00
|
|
|
})}
|
2020-01-30 13:17:29 +00:00
|
|
|
name={"postalCode" as keyof AddressTypeInput}
|
2019-08-09 10:17:04 +00:00
|
|
|
onChange={onChange}
|
2019-10-30 14:34:24 +00:00
|
|
|
value={data.postalCode}
|
2019-08-09 10:17:04 +00:00
|
|
|
fullWidth
|
|
|
|
/>
|
2019-10-30 14:34:24 +00:00
|
|
|
</Grid>
|
|
|
|
<FormSpacer />
|
|
|
|
<Grid>
|
|
|
|
<SingleAutocompleteSelectField
|
2019-08-09 10:17:04 +00:00
|
|
|
disabled={disabled}
|
2019-10-30 14:34:24 +00:00
|
|
|
displayValue={displayCountry}
|
2020-03-17 18:49:01 +00:00
|
|
|
error={!!formErrors.country}
|
|
|
|
helperText={getErrorMessage(formErrors.country, intl)}
|
2019-08-26 21:39:21 +00:00
|
|
|
label={intl.formatMessage({
|
2019-10-30 14:34:24 +00:00
|
|
|
defaultMessage: "Country"
|
2019-08-26 21:39:21 +00:00
|
|
|
})}
|
2020-01-30 13:17:29 +00:00
|
|
|
name={"country" as keyof AddressTypeInput}
|
2019-10-30 14:34:24 +00:00
|
|
|
onChange={onCountryChange}
|
|
|
|
value={data.country}
|
|
|
|
choices={countries}
|
|
|
|
InputProps={{
|
2020-01-30 16:11:34 +00:00
|
|
|
inputProps: {
|
|
|
|
autocomplete: "plsdontautocomplete" // Somehow it shuts it down
|
|
|
|
}
|
2019-10-30 14:34:24 +00:00
|
|
|
}}
|
2019-08-09 10:17:04 +00:00
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
2020-03-17 18:49:01 +00:00
|
|
|
error={!!formErrors.countryArea}
|
|
|
|
helperText={getErrorMessage(formErrors.countryArea, intl)}
|
2019-08-26 21:39:21 +00:00
|
|
|
label={intl.formatMessage({
|
2019-10-30 14:34:24 +00:00
|
|
|
defaultMessage: "Country area"
|
2019-08-26 21:39:21 +00:00
|
|
|
})}
|
2020-01-30 13:17:29 +00:00
|
|
|
name={"countryArea" as keyof AddressTypeInput}
|
2019-08-26 21:39:21 +00:00
|
|
|
onChange={onChange}
|
2019-10-30 14:34:24 +00:00
|
|
|
value={data.countryArea}
|
|
|
|
fullWidth
|
2019-08-09 10:17:04 +00:00
|
|
|
/>
|
2019-10-30 14:34:24 +00:00
|
|
|
</Grid>
|
|
|
|
<FormSpacer />
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
2020-03-17 18:49:01 +00:00
|
|
|
error={!!formErrors.phone}
|
2019-10-30 14:34:24 +00:00
|
|
|
fullWidth
|
2020-03-17 18:49:01 +00:00
|
|
|
helperText={getErrorMessage(formErrors.phone, intl)}
|
2019-10-30 14:34:24 +00:00
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Phone"
|
|
|
|
})}
|
2020-01-30 13:17:29 +00:00
|
|
|
name={"phone" as keyof AddressTypeInput}
|
2019-10-30 14:34:24 +00:00
|
|
|
value={data.phone}
|
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|
2020-01-30 13:17:29 +00:00
|
|
|
CompanyAddressInput.displayName = "CompanyAddressInput";
|
|
|
|
export default CompanyAddressInput;
|