saleor-dashboard/src/orders/components/OrderFulfillment/OrderFulfillment.tsx

245 lines
7.5 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
import CardActions from "@material-ui/core/CardActions";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-06-19 14:40:52 +00:00
import Table from "@material-ui/core/Table";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableHead from "@material-ui/core/TableHead";
import TableRow from "@material-ui/core/TableRow";
import Typography from "@material-ui/core/Typography";
2019-08-09 10:26:22 +00:00
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import CardMenu from "@saleor/components/CardMenu";
import CardTitle from "@saleor/components/CardTitle";
import Money from "@saleor/components/Money";
import Skeleton from "@saleor/components/Skeleton";
import StatusLabel from "@saleor/components/StatusLabel";
2019-08-09 11:14:35 +00:00
import TableCellAvatar, {
AVATAR_MARGIN
} from "@saleor/components/TableCellAvatar";
2019-06-19 14:40:52 +00:00
import { maybe, renderCollection } from "../../../misc";
import { FulfillmentStatus } from "../../../types/globalTypes";
import { OrderDetails_order_fulfillments } from "../../types/OrderDetails";
2019-10-30 14:34:24 +00:00
const useStyles = makeStyles(theme => ({
clickableRow: {
cursor: "pointer"
},
colName: {
width: "auto"
},
colNameLabel: {
marginLeft: AVATAR_MARGIN
},
colPrice: {
textAlign: "right",
width: 120
},
colQuantity: {
textAlign: "center",
width: 120
},
colTotal: {
textAlign: "right",
width: 120
},
2019-08-09 11:14:35 +00:00
2019-10-30 14:34:24 +00:00
orderNumber: {
display: "inline",
marginLeft: theme.spacing(1)
},
statusBar: {
paddingTop: 0
},
table: {
tableLayout: "fixed"
}
}));
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
interface OrderFulfillmentProps {
2019-06-19 14:40:52 +00:00
fulfillment: OrderDetails_order_fulfillments;
orderNumber: string;
onOrderFulfillmentCancel: () => void;
onTrackingCodeAdd: () => void;
}
2019-08-09 11:14:35 +00:00
const numberOfColumns = 3;
2019-10-30 14:34:24 +00:00
const OrderFulfillment: React.FC<OrderFulfillmentProps> = props => {
const {
2019-06-19 14:40:52 +00:00
fulfillment,
orderNumber,
onOrderFulfillmentCancel,
onTrackingCodeAdd
2019-10-30 14:34:24 +00:00
} = props;
const classes = useStyles(props);
const intl = useIntl();
2019-10-30 14:34:24 +00:00
const lines = maybe(() => fulfillment.lines);
const status = maybe(() => fulfillment.status);
const quantity = lines
? lines.map(line => line.quantity).reduce((prev, curr) => prev + curr, 0)
: "...";
2019-10-30 14:34:24 +00:00
return (
<Card>
<CardTitle
title={
!!lines ? (
<StatusLabel
label={
<>
{status === FulfillmentStatus.FULFILLED
? intl.formatMessage(
{
defaultMessage: "Fulfilled ({quantity})",
description: "section header"
},
{
quantity
}
)
: intl.formatMessage(
{
defaultMessage: "Fulfilled ({quantity})",
description: "section header"
},
{
quantity
}
2019-06-19 14:40:52 +00:00
)}
2019-10-30 14:34:24 +00:00
<Typography className={classes.orderNumber} variant="body1">
{maybe(
() => `#${orderNumber}-${fulfillment.fulfillmentOrder}`
)}
</Typography>
</>
}
status={
status === FulfillmentStatus.FULFILLED ? "success" : "error"
}
/>
) : (
<Skeleton />
)
}
toolbar={
maybe(() => fulfillment.status) === FulfillmentStatus.FULFILLED && (
<CardMenu
menuItems={[
{
label: intl.formatMessage({
defaultMessage: "Cancel shipment",
description: "button"
}),
onSelect: onOrderFulfillmentCancel
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
]}
/>
)
}
/>
<Table className={classes.table}>
<TableHead>
<TableRow>
<TableCell className={classes.colName}>
<span className={classes.colNameLabel}>
<FormattedMessage
defaultMessage="Product"
description="product name"
/>
</span>
</TableCell>
<TableCell className={classes.colQuantity}>
<FormattedMessage
defaultMessage="Quantity"
description="ordered product quantity"
/>
</TableCell>
<TableCell className={classes.colPrice}>
<FormattedMessage
defaultMessage="Price"
description="product price"
2019-06-19 14:40:52 +00:00
/>
2019-10-30 14:34:24 +00:00
</TableCell>
<TableCell className={classes.colTotal}>
<FormattedMessage
defaultMessage="Total"
description="order line total price"
2019-06-19 14:40:52 +00:00
/>
2019-10-30 14:34:24 +00:00
</TableCell>
</TableRow>
</TableHead>
<TableBody>
{renderCollection(lines, line => (
<TableRow
className={!!line ? classes.clickableRow : undefined}
hover={!!line}
key={maybe(() => line.id)}
>
<TableCellAvatar
className={classes.colName}
thumbnail={maybe(() => line.orderLine.thumbnail.url)}
>
{maybe(() => line.orderLine.productName) || <Skeleton />}
</TableCellAvatar>
2019-08-09 11:14:35 +00:00
<TableCell className={classes.colQuantity}>
2019-10-30 14:34:24 +00:00
{maybe(() => line.quantity) || <Skeleton />}
2019-06-19 14:40:52 +00:00
</TableCell>
2019-08-09 11:14:35 +00:00
<TableCell className={classes.colPrice}>
2019-10-30 14:34:24 +00:00
{maybe(() => line.orderLine.unitPrice.gross) ? (
<Money money={line.orderLine.unitPrice.gross} />
) : (
<Skeleton />
)}
2019-06-19 14:40:52 +00:00
</TableCell>
2019-08-09 11:14:35 +00:00
<TableCell className={classes.colTotal}>
2019-10-30 14:34:24 +00:00
{maybe(
() => line.quantity * line.orderLine.unitPrice.gross.amount
) ? (
<Money
money={{
amount:
line.quantity * line.orderLine.unitPrice.gross.amount,
currency: line.orderLine.unitPrice.gross.currency
}}
/>
) : (
<Skeleton />
)}
</TableCell>
</TableRow>
))}
{maybe(() => fulfillment.trackingNumber) && (
<TableRow>
<TableCell colSpan={numberOfColumns}>
<FormattedMessage
2019-10-30 14:34:24 +00:00
defaultMessage="Tracking Number: {trackingNumber}"
values={{
trackingNumber: fulfillment.trackingNumber
}}
/>
2019-06-19 14:40:52 +00:00
</TableCell>
</TableRow>
2019-10-30 14:34:24 +00:00
)}
</TableBody>
</Table>
{status === FulfillmentStatus.FULFILLED && !fulfillment.trackingNumber && (
<CardActions>
<Button color="primary" onClick={onTrackingCodeAdd}>
<FormattedMessage
defaultMessage="Add tracking"
description="fulfillment group tracking number"
/>
</Button>
</CardActions>
)}
</Card>
);
};
2019-06-19 14:40:52 +00:00
OrderFulfillment.displayName = "OrderFulfillment";
export default OrderFulfillment;