import { ConfirmButton, ConfirmButtonTransitionState, } from "@dashboard/components/ConfirmButton"; import VerticalSpacer from "@dashboard/components/VerticalSpacer"; import CustomerAddressChoiceCard from "@dashboard/customers/components/CustomerAddressChoiceCard"; import { AddressFragment, AddressTypeEnum } from "@dashboard/graphql"; import { FormChange } from "@dashboard/hooks/useForm"; import { buttonMessages } from "@dashboard/intl"; import { getById } from "@dashboard/misc"; import { Checkbox, DialogActions, DialogContent, FormControlLabel, InputAdornment, TextField, } from "@material-ui/core"; import { Button, SearchIcon } from "@saleor/macaw-ui"; import React from "react"; import { FormattedMessage, useIntl } from "react-intl"; import { dialogMessages as messages } from "./messages"; import { useStyles } from "./styles"; import { parseQuery, stringifyAddress } from "./utils"; export interface OrderCustomerAddressesSearchProps { type: AddressTypeEnum; cloneAddress: boolean; formChange: FormChange; openFromCustomerChange: boolean; transitionState: ConfirmButtonTransitionState; selectedCustomerAddressId: string; customerAddresses: AddressFragment[]; onChangeCustomerShippingAddress: (customerAddress: AddressFragment) => void; onChangeCustomerBillingAddress: (customerAddress: AddressFragment) => void; exitSearch(); } const OrderCustomerAddressesSearch: React.FC< OrderCustomerAddressesSearchProps > = props => { const { type, cloneAddress, formChange, transitionState, openFromCustomerChange, 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); } if (openFromCustomerChange) { exitSearch(); } }; const handleChange = (e: React.ChangeEvent) => { setQuery(e.target.value); }; const filteredCustomerAddresses = customerAddresses.filter(address => { const parsedAddress = stringifyAddress(address); return parsedAddress.search(new RegExp(parseQuery(query), "i")) >= 0; }); return ( <> {intl.formatMessage(messages.searchInfo)} ), }} inputProps={{ className: classes.searchInput }} />
{filteredCustomerAddresses.length === 0 ? intl.formatMessage(messages.noResultsFound) : filteredCustomerAddresses?.map(address => ( setTemporarySelectedAddress(address)} address={address} /> ))}
{!openFromCustomerChange && filteredCustomerAddresses.length !== 0 && ( formChange({ target: { name: "cloneAddress", value: !cloneAddress, }, }) } /> } label={intl.formatMessage( type === AddressTypeEnum.SHIPPING ? messages.billingSameAsShipping : messages.shippingSameAsBilling, )} /> )}
); }; OrderCustomerAddressesSearch.displayName = "OrderCustomerAddressesSearch"; export default OrderCustomerAddressesSearch;