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

218 lines
6.9 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";
import {
createStyles,
Theme,
withStyles,
WithStyles
} from "@material-ui/core/styles";
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";
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 i18n from "../../../i18n";
import { maybe, renderCollection } from "../../../misc";
import { FulfillmentStatus } from "../../../types/globalTypes";
import { OrderDetails_order_fulfillments } from "../../types/OrderDetails";
const styles = (theme: Theme) =>
createStyles({
clickableRow: {
cursor: "pointer"
},
2019-08-09 11:14:35 +00:00
colName: {
width: "auto"
},
colNameLabel: {
marginLeft: AVATAR_MARGIN
},
colPrice: {
textAlign: "right",
width: 120
},
colQuantity: {
textAlign: "center",
width: 120
},
colTotal: {
textAlign: "right",
width: 120
},
2019-06-19 14:40:52 +00:00
orderNumber: {
display: "inline",
marginLeft: theme.spacing.unit
},
statusBar: {
paddingTop: 0
},
2019-08-09 11:14:35 +00:00
table: {
tableLayout: "fixed"
2019-06-19 14:40:52 +00:00
}
});
interface OrderFulfillmentProps extends WithStyles<typeof styles> {
fulfillment: OrderDetails_order_fulfillments;
orderNumber: string;
onOrderFulfillmentCancel: () => void;
onTrackingCodeAdd: () => void;
}
2019-08-09 11:14:35 +00:00
const numberOfColumns = 3;
2019-06-19 14:40:52 +00:00
const OrderFulfillment = withStyles(styles, { name: "OrderFulfillment" })(
({
classes,
fulfillment,
orderNumber,
onOrderFulfillmentCancel,
onTrackingCodeAdd
}: OrderFulfillmentProps) => {
const lines = maybe(() => fulfillment.lines);
const status = maybe(() => fulfillment.status);
return (
<Card>
<CardTitle
title={
!!lines ? (
<StatusLabel
label={
<>
{status === FulfillmentStatus.FULFILLED
? i18n.t("Fulfilled ({{ quantity }})", {
quantity: lines
.map(line => line.quantity)
.reduce((prev, curr) => prev + curr, 0)
})
: i18n.t("Cancelled ({{ quantity }})", {
quantity: lines
.map(line => line.quantity)
.reduce((prev, curr) => prev + curr, 0)
})}
<Typography className={classes.orderNumber} variant="body2">
{maybe(
() => `#${orderNumber}-${fulfillment.fulfillmentOrder}`
)}
</Typography>
</>
}
status={
status === FulfillmentStatus.FULFILLED ? "success" : "error"
}
/>
) : (
<Skeleton />
)
}
toolbar={
maybe(() => fulfillment.status) === FulfillmentStatus.FULFILLED && (
<CardMenu
menuItems={[
{
label: i18n.t("Cancel shipment", {
context: "button"
}),
onSelect: onOrderFulfillmentCancel
}
]}
/>
)
}
/>
2019-08-09 11:14:35 +00:00
<Table className={classes.table}>
2019-06-19 14:40:52 +00:00
<TableHead>
<TableRow>
2019-08-09 11:14:35 +00:00
<TableCell className={classes.colName}>
<span className={classes.colNameLabel}>
{i18n.t("Product")}
</span>
2019-06-19 14:40:52 +00:00
</TableCell>
2019-08-09 11:14:35 +00:00
<TableCell className={classes.colQuantity}>
2019-06-19 14:40:52 +00:00
{i18n.t("Quantity")}
</TableCell>
2019-08-09 11:14:35 +00:00
<TableCell className={classes.colPrice}>
2019-06-19 14:40:52 +00:00
{i18n.t("Price")}
</TableCell>
2019-08-09 11:14:35 +00:00
<TableCell className={classes.colTotal}>
2019-06-19 14:40:52 +00:00
{i18n.t("Total")}
</TableCell>
</TableRow>
</TableHead>
<TableBody>
{renderCollection(lines, line => (
<TableRow
className={!!line ? classes.clickableRow : undefined}
hover={!!line}
key={maybe(() => line.id)}
>
<TableCellAvatar
2019-08-09 11:14:35 +00:00
className={classes.colName}
2019-06-19 14:40:52 +00:00
thumbnail={maybe(() => line.orderLine.thumbnail.url)}
2019-08-09 11:14:35 +00:00
>
2019-06-19 14:40:52 +00:00
{maybe(() => line.orderLine.productName) || <Skeleton />}
2019-08-09 11:14:35 +00:00
</TableCellAvatar>
<TableCell className={classes.colQuantity}>
2019-06-19 14:40:52 +00:00
{maybe(() => line.quantity) || <Skeleton />}
</TableCell>
2019-08-09 11:14:35 +00:00
<TableCell className={classes.colPrice}>
2019-06-19 14:40:52 +00:00
{maybe(() => line.orderLine.unitPrice.gross) ? (
<Money money={line.orderLine.unitPrice.gross} />
) : (
<Skeleton />
)}
</TableCell>
2019-08-09 11:14:35 +00:00
<TableCell className={classes.colTotal}>
2019-06-19 14:40:52 +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>
2019-08-09 11:14:35 +00:00
<TableCell colSpan={numberOfColumns}>
2019-06-19 14:40:52 +00:00
{i18n.t("Tracking Number: {{ trackingNumber }}", {
trackingNumber: fulfillment.trackingNumber
})}
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
{status === FulfillmentStatus.FULFILLED && !fulfillment.trackingNumber && (
<CardActions>
<Button color="primary" onClick={onTrackingCodeAdd}>
{i18n.t("Add tracking")}
</Button>
</CardActions>
)}
</Card>
);
}
);
OrderFulfillment.displayName = "OrderFulfillment";
export default OrderFulfillment;