2019-06-19 14:40:52 +00:00
|
|
|
import Button from "@material-ui/core/Button";
|
|
|
|
import Dialog from "@material-ui/core/Dialog";
|
|
|
|
import DialogActions from "@material-ui/core/DialogActions";
|
|
|
|
import DialogContent from "@material-ui/core/DialogContent";
|
|
|
|
import DialogTitle from "@material-ui/core/DialogTitle";
|
|
|
|
import AddressEdit from "@saleor/components/AddressEdit";
|
|
|
|
import ConfirmButton, {
|
|
|
|
ConfirmButtonTransitionState
|
|
|
|
} from "@saleor/components/ConfirmButton";
|
|
|
|
import Form from "@saleor/components/Form";
|
2020-07-07 10:14:12 +00:00
|
|
|
import { AccountErrorFragment } from "@saleor/fragments/types/AccountErrorFragment";
|
2019-11-06 15:21:58 +00:00
|
|
|
import useAddressValidation from "@saleor/hooks/useAddressValidation";
|
|
|
|
import useModalDialogErrors from "@saleor/hooks/useModalDialogErrors";
|
2019-08-09 11:14:35 +00:00
|
|
|
import useStateFromProps from "@saleor/hooks/useStateFromProps";
|
2019-08-23 12:23:59 +00:00
|
|
|
import { buttonMessages } from "@saleor/intl";
|
2021-03-30 07:40:18 +00:00
|
|
|
import { createStyles, WithStyles, withStyles } from "@saleor/theme";
|
2019-11-06 15:21:58 +00:00
|
|
|
import { AddressInput } from "@saleor/types/globalTypes";
|
2019-08-09 11:14:35 +00:00
|
|
|
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
|
|
|
import { FormattedMessage } from "react-intl";
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
import { AddressTypeInput } from "../../types";
|
|
|
|
import { CustomerAddresses_user_addresses } from "../../types/CustomerAddresses";
|
|
|
|
|
|
|
|
export interface CustomerAddressDialogProps {
|
|
|
|
address: CustomerAddresses_user_addresses;
|
|
|
|
confirmButtonState: ConfirmButtonTransitionState;
|
|
|
|
countries: Array<{
|
|
|
|
code: string;
|
|
|
|
label: string;
|
|
|
|
}>;
|
2020-03-11 09:55:14 +00:00
|
|
|
errors: AccountErrorFragment[];
|
2019-06-19 14:40:52 +00:00
|
|
|
open: boolean;
|
|
|
|
variant: "create" | "edit";
|
|
|
|
onClose: () => void;
|
2019-11-06 15:21:58 +00:00
|
|
|
onConfirm: (data: AddressInput) => void;
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const styles = createStyles({
|
|
|
|
overflow: {
|
|
|
|
overflowY: "visible"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-24 14:14:48 +00:00
|
|
|
const CustomerAddressDialog = withStyles(
|
|
|
|
styles,
|
|
|
|
{}
|
|
|
|
)(
|
2019-06-19 14:40:52 +00:00
|
|
|
({
|
|
|
|
address,
|
|
|
|
classes,
|
|
|
|
confirmButtonState,
|
|
|
|
countries,
|
|
|
|
errors,
|
|
|
|
open,
|
|
|
|
variant,
|
|
|
|
onClose,
|
|
|
|
onConfirm
|
|
|
|
}: CustomerAddressDialogProps & WithStyles<typeof styles>) => {
|
2019-08-09 11:14:35 +00:00
|
|
|
const [countryDisplayName, setCountryDisplayName] = useStateFromProps(
|
2021-01-07 10:17:40 +00:00
|
|
|
address?.country.country || ""
|
2019-08-09 11:14:35 +00:00
|
|
|
);
|
2019-11-06 15:21:58 +00:00
|
|
|
const {
|
|
|
|
errors: validationErrors,
|
|
|
|
submit: handleSubmit
|
|
|
|
} = useAddressValidation(onConfirm);
|
|
|
|
const dialogErrors = useModalDialogErrors(
|
|
|
|
[...errors, ...validationErrors],
|
|
|
|
open
|
|
|
|
);
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
const initialForm: AddressTypeInput = {
|
2021-01-07 10:17:40 +00:00
|
|
|
city: address?.city || "",
|
|
|
|
cityArea: address?.cityArea || "",
|
|
|
|
companyName: address?.companyName || "",
|
|
|
|
country: address?.country.code || "",
|
|
|
|
countryArea: address?.countryArea || "",
|
|
|
|
firstName: address?.firstName || "",
|
|
|
|
lastName: address?.lastName || "",
|
|
|
|
phone: address?.phone || "",
|
|
|
|
postalCode: address?.postalCode || "",
|
|
|
|
streetAddress1: address?.streetAddress1 || "",
|
|
|
|
streetAddress2: address?.streetAddress2 || ""
|
2019-06-19 14:40:52 +00:00
|
|
|
};
|
2019-08-09 11:14:35 +00:00
|
|
|
|
2021-01-07 10:17:40 +00:00
|
|
|
const countryChoices =
|
|
|
|
countries?.map(country => ({
|
|
|
|
label: country.label,
|
|
|
|
value: country.code
|
|
|
|
})) || [];
|
2019-08-09 11:14:35 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
|
|
|
<Dialog
|
|
|
|
onClose={onClose}
|
|
|
|
open={open}
|
|
|
|
classes={{ paper: classes.overflow }}
|
|
|
|
fullWidth
|
|
|
|
maxWidth="sm"
|
|
|
|
>
|
2020-02-24 14:14:48 +00:00
|
|
|
<Form initial={initialForm} onSubmit={handleSubmit}>
|
|
|
|
{({ change, data }) => {
|
2019-08-09 11:14:35 +00:00
|
|
|
const handleCountrySelect = createSingleAutocompleteSelectHandler(
|
|
|
|
change,
|
|
|
|
setCountryDisplayName,
|
|
|
|
countryChoices
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DialogTitle>
|
2019-08-23 12:23:59 +00:00
|
|
|
{variant === "create" ? (
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Add Address"
|
|
|
|
description="dialog title"
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Edit Address"
|
|
|
|
description="dialog title"
|
|
|
|
/>
|
|
|
|
)}
|
2019-08-09 11:14:35 +00:00
|
|
|
</DialogTitle>
|
|
|
|
<DialogContent className={classes.overflow}>
|
|
|
|
<AddressEdit
|
|
|
|
countries={countryChoices}
|
|
|
|
data={data}
|
|
|
|
countryDisplayValue={countryDisplayName}
|
2020-02-24 14:14:48 +00:00
|
|
|
errors={dialogErrors}
|
2019-08-09 11:14:35 +00:00
|
|
|
onChange={change}
|
|
|
|
onCountryChange={handleCountrySelect}
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
|
|
<Button onClick={onClose}>
|
2019-10-21 14:46:12 +00:00
|
|
|
<FormattedMessage {...buttonMessages.back} />
|
2019-08-09 11:14:35 +00:00
|
|
|
</Button>
|
|
|
|
<ConfirmButton
|
|
|
|
transitionState={confirmButtonState}
|
|
|
|
color="primary"
|
|
|
|
variant="contained"
|
|
|
|
type="submit"
|
|
|
|
>
|
2019-08-23 12:23:59 +00:00
|
|
|
<FormattedMessage {...buttonMessages.save} />
|
2019-08-09 11:14:35 +00:00
|
|
|
</ConfirmButton>
|
|
|
|
</DialogActions>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
2019-06-19 14:40:52 +00:00
|
|
|
</Form>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
CustomerAddressDialog.displayName = "CustomerAddressDialog";
|
|
|
|
export default CustomerAddressDialog;
|