
* 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
149 lines
4.4 KiB
TypeScript
149 lines
4.4 KiB
TypeScript
import { Button, Typography } from "@material-ui/core";
|
||
import Container from "@saleor/components/Container";
|
||
import PageHeader from "@saleor/components/PageHeader";
|
||
import { Backlink } from "@saleor/macaw-ui";
|
||
import { makeStyles } from "@saleor/macaw-ui";
|
||
import { getStringOrPlaceholder, renderCollection } from "@saleor/misc";
|
||
import React from "react";
|
||
import { defineMessages, useIntl } from "react-intl";
|
||
|
||
import { AddressTypeEnum } from "../../../types/globalTypes";
|
||
import { CustomerAddresses_user } from "../../types/CustomerAddresses";
|
||
import CustomerAddress from "../CustomerAddress/CustomerAddress";
|
||
|
||
export interface CustomerAddressListPageProps {
|
||
customer: CustomerAddresses_user;
|
||
disabled: boolean;
|
||
onAdd: () => void;
|
||
onBack: () => void;
|
||
onEdit: (id: string) => void;
|
||
onRemove: (id: string) => void;
|
||
onSetAsDefault: (id: string, type: AddressTypeEnum) => void;
|
||
}
|
||
|
||
const messages = defineMessages({
|
||
addAddress: {
|
||
defaultMessage: "Add address",
|
||
description: "button"
|
||
},
|
||
doesntHaveAddresses: {
|
||
defaultMessage:
|
||
"This customer doesn’t have any adresses added to his address book. You can add address using the button below."
|
||
},
|
||
fullNameAddress: {
|
||
defaultMessage: "{fullName}'s Address Book",
|
||
description: "customer's address book, header"
|
||
},
|
||
fullNameDetail: {
|
||
defaultMessage: "{fullName} Details",
|
||
description: "customer details, header"
|
||
},
|
||
noAddressToShow: {
|
||
defaultMessage: "There is no address to show for this customer"
|
||
}
|
||
});
|
||
|
||
const useStyles = makeStyles(
|
||
theme => ({
|
||
addButton: {
|
||
marginTop: theme.spacing(2)
|
||
},
|
||
description: {
|
||
marginTop: theme.spacing(1)
|
||
},
|
||
empty: {
|
||
margin: `${theme.spacing(13)}px auto 0`,
|
||
textAlign: "center",
|
||
width: 600
|
||
},
|
||
root: {
|
||
display: "grid",
|
||
gap: `${theme.spacing(3)}px`,
|
||
gridTemplateColumns: "repeat(3, 1fr)",
|
||
[theme.breakpoints.down("md")]: {
|
||
gridTemplateColumns: "repeat(2, 1fr)"
|
||
},
|
||
[theme.breakpoints.down("sm")]: {
|
||
gridTemplateColumns: "repeat(1, 1fr)"
|
||
}
|
||
}
|
||
}),
|
||
{ name: "CustomerAddressListPage" }
|
||
);
|
||
|
||
const CustomerAddressListPage: React.FC<CustomerAddressListPageProps> = props => {
|
||
const {
|
||
customer,
|
||
disabled,
|
||
onAdd,
|
||
onBack,
|
||
onEdit,
|
||
onRemove,
|
||
onSetAsDefault
|
||
} = props;
|
||
const classes = useStyles(props);
|
||
|
||
const intl = useIntl();
|
||
|
||
const isEmpty = customer?.addresses?.length === 0;
|
||
const fullName = getStringOrPlaceholder(
|
||
customer && [customer.firstName, customer.lastName].join(" ")
|
||
);
|
||
|
||
return (
|
||
<Container>
|
||
<Backlink onClick={onBack}>
|
||
{intl.formatMessage(messages.fullNameDetail, { fullName })}
|
||
</Backlink>
|
||
{!isEmpty && (
|
||
<PageHeader
|
||
title={intl.formatMessage(messages.fullNameAddress, { fullName })}
|
||
>
|
||
<Button color="primary" variant="contained" onClick={onAdd}>
|
||
{intl.formatMessage(messages.addAddress)}
|
||
</Button>
|
||
</PageHeader>
|
||
)}
|
||
{isEmpty ? (
|
||
<div className={classes.empty}>
|
||
<Typography variant="h5">
|
||
{intl.formatMessage(messages.noAddressToShow)}
|
||
</Typography>
|
||
<Typography className={classes.description}>
|
||
{intl.formatMessage(messages.doesntHaveAddresses)}
|
||
</Typography>
|
||
<Button
|
||
className={classes.addButton}
|
||
color="primary"
|
||
variant="contained"
|
||
onClick={onAdd}
|
||
>
|
||
{intl.formatMessage(messages.addAddress)}
|
||
</Button>
|
||
</div>
|
||
) : (
|
||
<div className={classes.root}>
|
||
{renderCollection(customer?.addresses, (address, addressNumber) => (
|
||
<CustomerAddress
|
||
address={address}
|
||
addressNumber={addressNumber + 1}
|
||
disabled={disabled}
|
||
isDefaultBillingAddress={
|
||
customer?.defaultBillingAddress?.id === address?.id
|
||
}
|
||
isDefaultShippingAddress={
|
||
customer?.defaultShippingAddress?.id === address?.id
|
||
}
|
||
onEdit={() => onEdit(address.id)}
|
||
onRemove={() => onRemove(address.id)}
|
||
onSetAsDefault={type => onSetAsDefault(address.id, type)}
|
||
key={address?.id || "skeleton"}
|
||
/>
|
||
))}
|
||
</div>
|
||
)}
|
||
</Container>
|
||
);
|
||
};
|
||
CustomerAddressListPage.displayName = "CustomerAddressListPage";
|
||
export default CustomerAddressListPage;
|