saleor-dashboard/src/components/CompanyAddressInput/CompanyAddressForm.tsx
Dominik Żegleń 62817568a7
Use MacawUI (#1229)
* Replace withStyleswith useStyles (#1100)

* Replace withStyleswith useStyles

* Update messages

* Use rem as a spacing unit (#1101)

* Use rems as spacing units

* Fix visual bugs

* Update stories

* Use macaw-ui as theme provider (#1108)

* Use macaw ui as a theme provider

* Add react-dom to aliases

* Fix jest module resolution

* Update useTheme hook usage

* Fix test wrapper

* Use macaw from git repo

* Fix CI

* Update stories

* Fix aliasing

* Extract savebar to macaw ui (#1146)

* wip

* Use savebar from macaw

* Use confirm button from macaw

* Improve file structure

* Use sidebar context from macaw

* Update macaw

* Update macaw version

* Remove savebar from storybook

* Update stories

* Use alerts and notifications from macaw (#1166)

* Use alerts from macaw

* Add notifications from macaw

* Update stories

* Pin macaw version

* Encapsulate limit reached in one component

* Remove unused imports

* Use backlinks from macaw (#1183)

* Use backlink from macaw

* Update macaw version

* Use macaw sidebar (#1148)

* Use sidebar from macaw

* Use shipped logo

* Use lowercase

* Update stories

* Use user chip from macaw (#1191)

* Use user chip from macaw

* Use dedicated components for menu items

* Simplify code

* Bump version and fix types (#1210)

* Rename onBack to onClick

* Rename UserChip to UserChipMenu

* Rename IMenuItem to SidebarMenuItem

* Update macaw version

* Fix tables after changes in macaw (#1220)

* Update macaw version

* Update changelog

* Update stories

* Fix after rebase

* Update to macaw 0.2.0

* Lint files

* Update macaw to 0.2.2
2021-07-21 10:59:52 +02:00

219 lines
6.4 KiB
TypeScript

import { TextField } from "@material-ui/core";
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";
import { AccountErrorFragment } from "@saleor/fragments/types/AccountErrorFragment";
import { ShopErrorFragment } from "@saleor/fragments/types/ShopErrorFragment";
import { WarehouseErrorFragment } from "@saleor/fragments/types/WarehouseErrorFragment";
import { ChangeEvent } from "@saleor/hooks/useForm";
import { makeStyles } from "@saleor/macaw-ui";
import { getFormErrors } from "@saleor/utils/errors";
import getAccountErrorMessage from "@saleor/utils/errors/account";
import getShopErrorMessage from "@saleor/utils/errors/shop";
import getWarehouseErrorMessage from "@saleor/utils/errors/warehouse";
import React from "react";
import { IntlShape, useIntl } from "react-intl";
export interface CompanyAddressFormProps {
countries: SingleAutocompleteChoiceType[];
data: AddressTypeInput;
displayCountry: string;
errors: Array<
AccountErrorFragment | ShopErrorFragment | WarehouseErrorFragment
>;
disabled: boolean;
onChange: (event: ChangeEvent) => void;
onCountryChange: (event: ChangeEvent) => void;
}
const useStyles = makeStyles(
{
root: {}
},
{ name: "CompanyAddressForm" }
);
function getErrorMessage(
err: AccountErrorFragment | ShopErrorFragment | WarehouseErrorFragment,
intl: IntlShape
): string {
switch (err?.__typename) {
case "AccountError":
return getAccountErrorMessage(err, intl);
case "WarehouseError":
return getWarehouseErrorMessage(err, intl);
default:
return getShopErrorMessage(err, intl);
}
}
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",
"countryArea",
"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
InputProps={{
autoComplete: "organization"
}}
/>
<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
InputProps={{
autoComplete: "address-line1"
}}
/>
<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
InputProps={{
autoComplete: "address-line2"
}}
/>
<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
InputProps={{
autoComplete: "address-level2"
}}
/>
<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
InputProps={{
autoComplete: "postal-code"
}}
/>
</Grid>
<FormSpacer />
<Grid>
<SingleAutocompleteSelectField
data-test-id="address-edit-country-select-field"
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: {
autoComplete: "none"
}
}}
/>
<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
InputProps={{
autoComplete: "address-level1"
}}
/>
</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}
InputProps={{
autoComplete: "tel"
}}
/>
</div>
);
};
CompanyAddressForm.displayName = "CompanyAddressForm";
export default CompanyAddressForm;