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

310 lines
9.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";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-06-19 14:40:52 +00:00
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";
import CardMenu from "@saleor/components/CardMenu";
import CardTitle from "@saleor/components/CardTitle";
import Money from "@saleor/components/Money";
2019-11-04 14:25:23 +00:00
import ResponsiveTable from "@saleor/components/ResponsiveTable";
2019-06-19 14:40:52 +00:00
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";
import classNames from "classnames";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
import { getStringOrPlaceholder, maybe, renderCollection } from "../../../misc";
2019-06-19 14:40:52 +00:00
import { FulfillmentStatus } from "../../../types/globalTypes";
import { OrderDetails_order_fulfillments } from "../../types/OrderDetails";
2019-12-03 15:28:40 +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
},
2020-05-25 09:59:23 +00:00
colSku: {
textAlign: "right",
textOverflow: "ellipsis",
width: 120
},
2019-12-03 15:28:40 +00:00
colTotal: {
textAlign: "right",
width: 120
},
2020-04-21 22:37:06 +00:00
infoLabel: {
display: "inline-block"
},
infoLabelWithMargin: {
marginBottom: theme.spacing()
},
infoRow: {
padding: theme.spacing(2, 3)
},
2019-12-03 15:28:40 +00:00
orderNumber: {
display: "inline",
marginLeft: theme.spacing(1)
},
statusBar: {
paddingTop: 0
},
table: {
tableLayout: "fixed"
}
}),
{ name: "OrderFulfillment" }
);
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;
}
2020-04-21 22:37:06 +00:00
const numberOfColumns = 4;
2019-08-09 11:14:35 +00:00
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(
{
2020-04-27 14:26:57 +00:00
defaultMessage: "Cancelled ({quantity})",
description: "cancelled fulfillment, section header"
2019-10-30 14:34:24 +00:00
},
{
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 Fulfillment",
2019-10-30 14:34:24 +00:00
description: "button"
}),
onSelect: onOrderFulfillmentCancel
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
]}
/>
)
}
/>
2019-11-04 14:25:23 +00:00
<ResponsiveTable className={classes.table}>
2019-10-30 14:34:24 +00:00
<TableHead>
<TableRow>
<TableCell className={classes.colName}>
<span className={classes.colNameLabel}>
<FormattedMessage
defaultMessage="Product"
description="product name"
/>
</span>
</TableCell>
2020-05-25 09:59:23 +00:00
<TableCell className={classes.colSku}>
<FormattedMessage
defaultMessage="SKU"
description="ordered product sku"
/>
</TableCell>
2019-10-30 14:34:24 +00:00
<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>
2020-05-25 09:59:23 +00:00
<TableCell className={classes.colSku}>
{line?.orderLine.productSku || <Skeleton />}
</TableCell>
2019-08-09 11:14:35 +00:00
<TableCell className={classes.colQuantity}>
2020-05-25 09:59:23 +00:00
{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>
))}
2020-04-21 22:37:06 +00:00
<TableRow>
<TableCell className={classes.infoRow} colSpan={numberOfColumns}>
<Typography color="textSecondary" variant="body2">
<FormattedMessage
2020-04-21 22:37:06 +00:00
defaultMessage="Fulfilled from: {warehouseName}"
description="fulfillment group"
2019-10-30 14:34:24 +00:00
values={{
2020-04-21 22:37:06 +00:00
warehouseName: (
<Typography
className={classNames(classes.infoLabel, {
[classes.infoLabelWithMargin]: !!fulfillment?.trackingNumber
})}
color="textPrimary"
variant="body2"
>
{getStringOrPlaceholder(fulfillment?.warehouse?.name)}
2020-04-21 22:37:06 +00:00
</Typography>
)
2019-10-30 14:34:24 +00:00
}}
/>
2020-04-21 22:37:06 +00:00
</Typography>
<Typography color="textSecondary" variant="body2">
{fulfillment?.trackingNumber && (
<FormattedMessage
defaultMessage="Tracking Number: {trackingNumber}"
values={{
trackingNumber: (
<Typography
className={classes.infoLabel}
color="textPrimary"
variant="body2"
>
{fulfillment.trackingNumber}
</Typography>
)
}}
/>
)}
</Typography>
</TableCell>
</TableRow>
2019-10-30 14:34:24 +00:00
</TableBody>
2019-11-04 14:25:23 +00:00
</ResponsiveTable>
2019-10-30 14:34:24 +00:00
{status === FulfillmentStatus.FULFILLED && !fulfillment.trackingNumber && (
<CardActions>
<Button color="primary" onClick={onTrackingCodeAdd}>
<FormattedMessage
defaultMessage="Add tracking"
description="fulfillment group tracking number"
/>
</Button>
</CardActions>
)}
{status === FulfillmentStatus.FULFILLED && fulfillment.trackingNumber && (
<CardActions>
<Button color="primary" onClick={onTrackingCodeAdd}>
<FormattedMessage
defaultMessage="Edit tracking"
description="fulfillment group tracking number"
/>
</Button>
</CardActions>
)}
2019-10-30 14:34:24 +00:00
</Card>
);
};
2019-06-19 14:40:52 +00:00
OrderFulfillment.displayName = "OrderFulfillment";
export default OrderFulfillment;