saleor-dashboard/src/orders/components/OrderDraftDetailsSummary/OrderDraftDetailsSummary.tsx
Dominik Żegleń 62817568a7
Use MacawUI (#1229)
* 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
2021-07-21 10:59:52 +02:00

263 lines
7 KiB
TypeScript

import { Typography } from "@material-ui/core";
import HorizontalSpacer from "@saleor/apps/components/HorizontalSpacer";
import Link from "@saleor/components/Link";
import Money from "@saleor/components/Money";
import { makeStyles } from "@saleor/macaw-ui";
import { OrderDiscountContextConsumerProps } from "@saleor/products/components/OrderDiscountProviders/OrderDiscountProvider";
import { OrderDiscountData } from "@saleor/products/components/OrderDiscountProviders/types";
import { DiscountValueTypeEnum } from "@saleor/types/globalTypes";
import React, { useRef } from "react";
import { useIntl } from "react-intl";
import { defineMessages } from "react-intl";
import { OrderDetails_order } from "../../types/OrderDetails";
import OrderDiscountCommonModal from "../OrderDiscountCommonModal";
import { ORDER_DISCOUNT } from "../OrderDiscountCommonModal/types";
const useStyles = makeStyles(
theme => ({
root: {
...theme.typography.body1,
lineHeight: 1.9,
width: "100%"
},
textRight: {
textAlign: "right"
},
subtitle: {
color: theme.palette.grey[500],
paddingRight: theme.spacing(1)
},
relativeRow: {
position: "relative"
},
percentDiscountLabelContainer: {
display: "flex",
flexDirection: "row",
alignItems: "baseline",
justifyContent: "flex-end"
},
shippingMethodContainer: {
display: "flex",
flexDirection: "row",
alignItems: "baseline"
}
}),
{ name: "OrderDraftDetailsSummary" }
);
const messages = defineMessages({
addCustomerInfo: {
defaultMessage: "add customer first",
description: "add customer first label"
},
subtotal: {
defaultMessage: "Subtotal",
description: "subtotal price"
},
addDiscount: {
defaultMessage: "Add Discount",
description: "add discount button"
},
discount: {
defaultMessage: "Discount",
description: "discount button"
},
addShippingCarrier: {
defaultMessage: "Add shipping carrier",
description: "add shipping carrier button"
},
noShippingCarriers: {
defaultMessage: "No applicable shipping carriers",
description: "no shipping carriers title"
},
total: {
defaultMessage: "Total",
description: "total price"
},
taxes: {
defaultMessage: "Taxes (VAT included)",
description: "taxes title"
}
});
const PRICE_PLACEHOLDER = "---";
interface OrderDraftDetailsSummaryProps
extends OrderDiscountContextConsumerProps {
disabled?: boolean;
order: OrderDetails_order;
onShippingMethodEdit: () => void;
}
const OrderDraftDetailsSummary: React.FC<OrderDraftDetailsSummaryProps> = props => {
const {
order,
onShippingMethodEdit,
orderDiscount,
addOrderDiscount,
removeOrderDiscount,
openDialog,
closeDialog,
isDialogOpen,
orderDiscountAddStatus,
orderDiscountRemoveStatus,
undiscountedPrice
} = props;
const intl = useIntl();
const classes = useStyles(props);
const popperAnchorRef = useRef<HTMLTableRowElement | null>(null);
if (!order) {
return null;
}
const {
subtotal,
total,
shippingMethod,
shippingMethodName,
availableShippingMethods,
shippingPrice,
shippingAddress,
isShippingRequired
} = order;
const hasChosenShippingMethod =
shippingMethod !== null && shippingMethodName !== null;
const hasAvailableShippingMethods =
!!availableShippingMethods?.length || isShippingRequired;
const discountTitle = orderDiscount
? messages.discount
: messages.addDiscount;
const getOrderDiscountLabel = (orderDiscountData: OrderDiscountData) => {
if (!orderDiscountData) {
return PRICE_PLACEHOLDER;
}
const {
value: discountValue,
calculationMode,
amount: discountAmount
} = orderDiscountData;
const currency = total.gross.currency;
if (calculationMode === DiscountValueTypeEnum.PERCENTAGE) {
return (
<div className={classes.percentDiscountLabelContainer}>
<Typography
className={classes.subtitle}
>{`(${discountValue}%)`}</Typography>
<Money money={discountAmount} />
</div>
);
}
return <Money money={{ amount: discountValue, currency }} />;
};
const getShippingMethodComponent = () => {
if (hasChosenShippingMethod) {
return (
<Link onClick={onShippingMethodEdit}>{`${shippingMethodName}`}</Link>
);
}
const shippingCarrierBase = intl.formatMessage(messages.addShippingCarrier);
if (!!shippingAddress) {
return (
<Link
onClick={onShippingMethodEdit}
data-test-id="add-shipping-carrier"
>
{shippingCarrierBase}
</Link>
);
}
const addCustomerInfo = intl.formatMessage(messages.addCustomerInfo);
return (
<div className={classes.shippingMethodContainer}>
<Link underline disabled onClick={onShippingMethodEdit}>
{shippingCarrierBase}
</Link>
<HorizontalSpacer />
<Typography variant="caption">{`(${addCustomerInfo})`}</Typography>
</div>
);
};
return (
<table className={classes.root}>
<tbody>
<tr className={classes.relativeRow} ref={popperAnchorRef}>
<td>
<Link onClick={openDialog}>
{intl.formatMessage(discountTitle)}
</Link>
<OrderDiscountCommonModal
dialogPlacement="bottom-start"
modalType={ORDER_DISCOUNT}
anchorRef={popperAnchorRef}
existingDiscount={orderDiscount}
maxPrice={undiscountedPrice}
isOpen={isDialogOpen}
onConfirm={addOrderDiscount}
onClose={closeDialog}
onRemove={removeOrderDiscount}
confirmStatus={orderDiscountAddStatus}
removeStatus={orderDiscountRemoveStatus}
/>
</td>
<td className={classes.textRight}>
{getOrderDiscountLabel(orderDiscount)}
</td>
</tr>
<tr>
<td>{intl.formatMessage(messages.subtotal)}</td>
<td className={classes.textRight}>
<Money money={subtotal.gross} />
</td>
</tr>
<tr>
{hasAvailableShippingMethods && (
<td>{getShippingMethodComponent()}</td>
)}
{!hasAvailableShippingMethods && (
<td>{intl.formatMessage(messages.noShippingCarriers)}</td>
)}
<td className={classes.textRight}>
{hasChosenShippingMethod ? (
<Money money={shippingPrice.gross} />
) : (
PRICE_PLACEHOLDER
)}
</td>
</tr>
<tr>
<td>{intl.formatMessage(messages.taxes)}</td>
<td className={classes.textRight}>
<Money money={order.total.tax} />
</td>
</tr>
<tr>
<td>{intl.formatMessage(messages.total)}</td>
<td className={classes.textRight}>
<Money money={total.gross} />
</td>
</tr>
</tbody>
</table>
);
};
export default OrderDraftDetailsSummary;