saleor-dashboard/src/orders/components/OrderCustomerAddressesEditDialog/OrderCustomerAddressesSearch.tsx
Paweł Chyła adf3342d5c
Replace ConfirmButton from old macaw (#3647)
* New ConfirmButton component

* Update macaw ui

* Remove old confim button

* New confirm button

* Replace all place with new confirm button

* Remove ConfirmButtonTransitionState use from mcaw

* Does not change button width when showing loader and success state

* Test ConfirmButton

* Remove story, update tests

* WIP change pull_request to push for chromatic

* Revert "WIP change pull_request to push for chromatic"

This reverts commit 8f0909bf54f185898a7f1d236f072d6544fd5d86.

* Add comments

* Remove css prop from DialogTable

* Make confirm button larger in order send refund
2023-05-30 09:42:22 +02:00

165 lines
5.1 KiB
TypeScript

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<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 className={classes.dialogContent}>
{intl.formatMessage(messages.searchInfo)}
<VerticalSpacer spacing={2} />
<TextField
value={query}
variant="outlined"
onChange={handleChange}
placeholder={"Search addresses"}
fullWidth
InputProps={{
startAdornment: (
<InputAdornment position="start">
<SearchIcon />
</InputAdornment>
),
}}
inputProps={{ className: classes.searchInput }}
/>
<VerticalSpacer spacing={2} />
<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}
/>
<VerticalSpacer spacing={2} />
</React.Fragment>
))}
</div>
{!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,
)}
/>
)}
</DialogContent>
<DialogActions>
<Button onClick={exitSearch} variant="secondary">
<FormattedMessage {...buttonMessages.cancel} />
</Button>
<ConfirmButton
variant="primary"
transitionState={transitionState}
type={openFromCustomerChange ? undefined : "submit"}
onClick={handleSelect}
>
<FormattedMessage {...buttonMessages.select} />
</ConfirmButton>
</DialogActions>
</>
);
};
OrderCustomerAddressesSearch.displayName = "OrderCustomerAddressesSearch";
export default OrderCustomerAddressesSearch;