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

285 lines
9.2 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 CardContent from "@material-ui/core/CardContent";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-06-19 14:40:52 +00:00
import CardTitle from "@saleor/components/CardTitle";
import { Hr } from "@saleor/components/Hr";
import Money, { subtractMoney } from "@saleor/components/Money";
import Skeleton from "@saleor/components/Skeleton";
import StatusLabel from "@saleor/components/StatusLabel";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import { maybe, transformPaymentStatus } from "../../../misc";
import { OrderAction, OrderStatus } from "../../../types/globalTypes";
import { OrderDetails_order } from "../../types/OrderDetails";
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
root: {
...theme.typography.body1,
lineHeight: 1.9,
width: "100%"
},
textRight: {
textAlign: "right"
},
totalRow: {
fontWeight: 600
}
}),
{ name: "OrderPayment" }
);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
interface OrderPaymentProps {
2019-06-19 14:40:52 +00:00
order: OrderDetails_order;
onCapture: () => void;
onMarkAsPaid: () => void;
onRefund: () => void;
onVoid: () => void;
}
2019-10-30 14:34:24 +00:00
const OrderPayment: React.FC<OrderPaymentProps> = props => {
const { order, onCapture, onMarkAsPaid, onRefund, onVoid } = props;
const classes = useStyles(props);
const intl = useIntl();
2019-10-30 14:34:24 +00:00
const canCapture = maybe(() => order.actions, []).includes(
OrderAction.CAPTURE
);
const canVoid = maybe(() => order.actions, []).includes(OrderAction.VOID);
const canRefund = maybe(() => order.actions, []).includes(OrderAction.REFUND);
const canMarkAsPaid = maybe(() => order.actions, []).includes(
OrderAction.MARK_AS_PAID
);
const payment = transformPaymentStatus(
maybe(() => order.paymentStatus),
intl
);
return (
<Card>
<CardTitle
title={
maybe(() => order.paymentStatus) === undefined ? (
<Skeleton />
) : (
<StatusLabel label={payment.localized} status={payment.status} />
)
}
/>
<CardContent>
<table className={classes.root}>
<tbody>
<tr>
<td>
<FormattedMessage
defaultMessage="Subtotal"
description="order subtotal price"
/>
</td>
<td>
{maybe(() => order.lines) === undefined ? (
<Skeleton />
) : (
<FormattedMessage
2019-10-30 14:34:24 +00:00
defaultMessage="{quantity} items"
description="ordered products"
values={{
quantity: order.lines
.map(line => line.quantity)
.reduce((curr, prev) => prev + curr, 0)
}}
/>
2019-10-30 14:34:24 +00:00
)}
</td>
<td className={classes.textRight}>
{maybe(() => order.subtotal.gross) === undefined ? (
<Skeleton />
) : (
<Money money={order.subtotal.gross} />
)}
</td>
</tr>
<tr>
<td>
<FormattedMessage defaultMessage="Taxes" />
</td>
<td>
{maybe(() => order.total.tax) === undefined ? (
<Skeleton />
) : order.total.tax.amount > 0 ? (
intl.formatMessage({
defaultMessage: "VAT included",
description: "vat included in order price"
})
) : (
intl.formatMessage({
defaultMessage: "does not apply",
description: "vat not included in order price",
id: "orderPaymentVATDoesNotApply"
})
)}
</td>
<td className={classes.textRight}>
{maybe(() => order.total.tax) === undefined ? (
<Skeleton />
) : (
<Money money={order.total.tax} />
)}
</td>
</tr>
<tr>
<td>
<FormattedMessage
defaultMessage="Shipping"
description="order shipping method name"
/>
</td>
<td>
{maybe(() => order.shippingMethodName) === undefined &&
maybe(() => order.shippingPrice) === undefined ? (
<Skeleton />
) : order.shippingMethodName === null ? (
intl.formatMessage({
defaultMessage: "does not apply",
description: "order does not require shipping",
id: "orderPaymentShippingDoesNotApply"
})
) : (
order.shippingMethodName
)}
</td>
<td className={classes.textRight}>
{maybe(() => order.shippingPrice.gross) === undefined ? (
<Skeleton />
) : (
<Money money={order.shippingPrice.gross} />
)}
</td>
</tr>
<tr className={classes.totalRow}>
<td>
<FormattedMessage
defaultMessage="Total"
description="order total price"
/>
</td>
<td />
<td className={classes.textRight}>
{maybe(() => order.total.gross) === undefined ? (
<Skeleton />
) : (
<Money money={order.total.gross} />
)}
</td>
</tr>
</tbody>
</table>
</CardContent>
<Hr />
<CardContent>
<table className={classes.root}>
<tbody>
<tr>
<td>
<FormattedMessage
defaultMessage="Preauthorized amount"
description="order payment"
/>
</td>
<td className={classes.textRight}>
{maybe(() => order.totalAuthorized.amount) === undefined ? (
<Skeleton />
) : (
<Money money={order.totalAuthorized} />
)}
</td>
</tr>
<tr>
<td>
<FormattedMessage
defaultMessage="Captured amount"
description="order payment"
/>
</td>
<td className={classes.textRight}>
{maybe(() => order.totalCaptured.amount) === undefined ? (
<Skeleton />
) : (
<Money money={order.totalCaptured} />
)}
</td>
</tr>
<tr className={classes.totalRow}>
<td>
<FormattedMessage
defaultMessage="Outstanding Balance"
description="order payment"
/>
</td>
<td className={classes.textRight}>
{maybe(
() => order.total.gross.amount && order.totalCaptured.amount
) === undefined ? (
<Skeleton />
) : (
<Money
money={subtractMoney(
order.totalCaptured,
order.total.gross
)}
/>
2019-10-30 14:34:24 +00:00
)}
</td>
</tr>
</tbody>
</table>
</CardContent>
{maybe(() => order.status) !== OrderStatus.CANCELED &&
(canCapture || canRefund || canVoid || canMarkAsPaid) && (
<>
<Hr />
<CardActions>
{canCapture && (
<Button color="primary" variant="text" onClick={onCapture}>
<FormattedMessage
2019-10-30 14:34:24 +00:00
defaultMessage="Capture"
description="capture payment, button"
/>
2019-10-30 14:34:24 +00:00
</Button>
)}
{canRefund && (
<Button color="primary" variant="text" onClick={onRefund}>
<FormattedMessage
2019-10-30 14:34:24 +00:00
defaultMessage="Refund"
description="button"
/>
2019-10-30 14:34:24 +00:00
</Button>
)}
{canVoid && (
<Button color="primary" variant="text" onClick={onVoid}>
<FormattedMessage
2019-10-30 14:34:24 +00:00
defaultMessage="Void"
description="void payment, button"
/>
2019-10-30 14:34:24 +00:00
</Button>
)}
{canMarkAsPaid && (
<Button color="primary" variant="text" onClick={onMarkAsPaid}>
<FormattedMessage
2019-10-30 14:34:24 +00:00
defaultMessage="Mark as paid"
description="order, button"
/>
2019-10-30 14:34:24 +00:00
</Button>
)}
</CardActions>
</>
)}
</Card>
);
};
2019-06-19 14:40:52 +00:00
OrderPayment.displayName = "OrderPayment";
export default OrderPayment;