2021-12-03 11:59:29 +00:00
|
|
|
import {
|
2022-02-02 11:22:39 +00:00
|
|
|
Checkbox,
|
2021-12-03 11:59:29 +00:00
|
|
|
DialogActions,
|
|
|
|
DialogContent,
|
2022-02-02 11:22:39 +00:00
|
|
|
FormControlLabel,
|
2021-12-03 11:59:29 +00:00
|
|
|
InputAdornment,
|
|
|
|
TextField
|
|
|
|
} from "@material-ui/core";
|
2022-02-02 11:22:39 +00:00
|
|
|
import VerticalSpacer from "@saleor/apps/components/VerticalSpacer";
|
2021-12-03 11:59:29 +00:00
|
|
|
import { ConfirmButton } from "@saleor/components/ConfirmButton";
|
|
|
|
import CustomerAddressChoiceCard from "@saleor/customers/components/CustomerAddressChoiceCard";
|
|
|
|
import { CustomerAddresses_user_addresses } from "@saleor/customers/types/CustomerAddresses";
|
2022-02-02 11:22:39 +00:00
|
|
|
import { FormChange } from "@saleor/hooks/useForm";
|
2021-12-03 11:59:29 +00:00
|
|
|
import { buttonMessages } from "@saleor/intl";
|
2022-02-02 11:22:39 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
ConfirmButtonTransitionState,
|
|
|
|
SearchIcon
|
|
|
|
} from "@saleor/macaw-ui";
|
2021-12-03 11:59:29 +00:00
|
|
|
import { AddressTypeEnum } from "@saleor/types/globalTypes";
|
|
|
|
import React from "react";
|
|
|
|
import { FormattedMessage, useIntl } from "react-intl";
|
|
|
|
|
|
|
|
import { getById } from "../OrderReturnPage/utils";
|
2022-02-02 11:22:39 +00:00
|
|
|
import { dialogMessages as messages } from "./messages";
|
2021-12-03 11:59:29 +00:00
|
|
|
import { useStyles } from "./styles";
|
|
|
|
import { parseQuery, stringifyAddress } from "./utils";
|
|
|
|
|
|
|
|
export interface OrderCustomerAddressesSearchProps {
|
|
|
|
type: AddressTypeEnum;
|
2022-02-02 11:22:39 +00:00
|
|
|
cloneAddress: boolean;
|
|
|
|
formChange: FormChange;
|
|
|
|
openFromCustomerChange: boolean;
|
|
|
|
transitionState: ConfirmButtonTransitionState;
|
|
|
|
selectedCustomerAddressId: string;
|
2021-12-03 11:59:29 +00:00
|
|
|
customerAddresses: CustomerAddresses_user_addresses[];
|
|
|
|
onChangeCustomerShippingAddress: (
|
|
|
|
customerAddress: CustomerAddresses_user_addresses
|
|
|
|
) => void;
|
|
|
|
onChangeCustomerBillingAddress: (
|
|
|
|
customerAddress: CustomerAddresses_user_addresses
|
|
|
|
) => void;
|
|
|
|
exitSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
const OrderCustomerAddressesSearch: React.FC<OrderCustomerAddressesSearchProps> = props => {
|
|
|
|
const {
|
|
|
|
type,
|
2022-02-02 11:22:39 +00:00
|
|
|
cloneAddress,
|
|
|
|
formChange,
|
|
|
|
transitionState,
|
|
|
|
openFromCustomerChange,
|
2021-12-03 11:59:29 +00:00
|
|
|
selectedCustomerAddressId,
|
|
|
|
customerAddresses,
|
|
|
|
onChangeCustomerShippingAddress,
|
|
|
|
onChangeCustomerBillingAddress,
|
|
|
|
exitSearch
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const intl = useIntl();
|
|
|
|
const classes = useStyles(props);
|
|
|
|
|
|
|
|
const initialAddress = customerAddresses.find(
|
|
|
|
getById(selectedCustomerAddressId)
|
|
|
|
);
|
|
|
|
|
|
|
|
const [query, setQuery] = React.useState("");
|
|
|
|
const [
|
|
|
|
temporarySelectedAddress,
|
|
|
|
setTemporarySelectedAddress
|
|
|
|
] = React.useState(initialAddress);
|
|
|
|
|
|
|
|
const handleSelect = () => {
|
|
|
|
if (type === AddressTypeEnum.SHIPPING) {
|
|
|
|
onChangeCustomerShippingAddress(temporarySelectedAddress);
|
|
|
|
} else {
|
|
|
|
onChangeCustomerBillingAddress(temporarySelectedAddress);
|
|
|
|
}
|
2022-02-02 11:22:39 +00:00
|
|
|
if (openFromCustomerChange) {
|
|
|
|
exitSearch();
|
|
|
|
}
|
2021-12-03 11:59:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setQuery(e.target.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const filteredCustomerAddresses = customerAddresses.filter(address => {
|
|
|
|
const parsedAddress = stringifyAddress(address);
|
|
|
|
|
|
|
|
return parsedAddress.search(new RegExp(parseQuery(query), "i")) >= 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DialogContent>
|
|
|
|
{intl.formatMessage(messages.searchInfo)}
|
2022-02-02 11:22:39 +00:00
|
|
|
<VerticalSpacer spacing={2} />
|
2021-12-03 11:59:29 +00:00
|
|
|
<TextField
|
|
|
|
value={query}
|
|
|
|
variant="outlined"
|
|
|
|
onChange={handleChange}
|
|
|
|
placeholder={"Search addresses"}
|
|
|
|
fullWidth
|
|
|
|
InputProps={{
|
|
|
|
startAdornment: (
|
|
|
|
<InputAdornment position="start">
|
|
|
|
<SearchIcon />
|
|
|
|
</InputAdornment>
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
inputProps={{ className: classes.searchInput }}
|
|
|
|
/>
|
2022-02-02 11:22:39 +00:00
|
|
|
<VerticalSpacer spacing={2} />
|
2021-12-03 11:59:29 +00:00
|
|
|
<div className={classes.scrollableWrapper}>
|
|
|
|
{filteredCustomerAddresses.length === 0
|
|
|
|
? intl.formatMessage(messages.noResultsFound)
|
|
|
|
: filteredCustomerAddresses?.map(address => (
|
|
|
|
<React.Fragment key={address.id}>
|
|
|
|
<CustomerAddressChoiceCard
|
|
|
|
selected={address.id === temporarySelectedAddress.id}
|
|
|
|
onSelect={() => setTemporarySelectedAddress(address)}
|
|
|
|
address={address}
|
|
|
|
/>
|
2022-02-02 11:22:39 +00:00
|
|
|
<VerticalSpacer spacing={2} />
|
2021-12-03 11:59:29 +00:00
|
|
|
</React.Fragment>
|
|
|
|
))}
|
|
|
|
</div>
|
2022-02-02 11:22:39 +00:00
|
|
|
{!openFromCustomerChange && filteredCustomerAddresses.length !== 0 && (
|
|
|
|
<FormControlLabel
|
|
|
|
control={
|
|
|
|
<Checkbox
|
|
|
|
checked={cloneAddress}
|
|
|
|
name="cloneAddress"
|
|
|
|
onChange={() =>
|
|
|
|
formChange({
|
|
|
|
target: {
|
|
|
|
name: "cloneAddress",
|
|
|
|
value: !cloneAddress
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
label={intl.formatMessage(
|
|
|
|
type === AddressTypeEnum.SHIPPING
|
|
|
|
? messages.billingSameAsShipping
|
|
|
|
: messages.shippingSameAsBilling
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
)}
|
2021-12-03 11:59:29 +00:00
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
2022-02-02 11:22:39 +00:00
|
|
|
<Button onClick={exitSearch} variant="secondary">
|
2021-12-03 11:59:29 +00:00
|
|
|
<FormattedMessage {...buttonMessages.cancel} />
|
|
|
|
</Button>
|
|
|
|
<ConfirmButton
|
2022-01-28 12:34:20 +00:00
|
|
|
variant="primary"
|
2022-02-02 11:22:39 +00:00
|
|
|
transitionState={transitionState}
|
|
|
|
type={openFromCustomerChange ? undefined : "submit"}
|
2021-12-03 11:59:29 +00:00
|
|
|
onClick={handleSelect}
|
|
|
|
>
|
|
|
|
<FormattedMessage {...buttonMessages.select} />
|
|
|
|
</ConfirmButton>
|
|
|
|
</DialogActions>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
OrderCustomerAddressesSearch.displayName = "OrderCustomerAddressesSearch";
|
|
|
|
export default OrderCustomerAddressesSearch;
|