saleor-dashboard/src/orders/components/OrderDraftPage/OrderDraftAlert.tsx
Dawid f01966f0d4
Handle errors on finalizing draft order (#2191)
* Add warning alert before finilizing draft order

* Add line error indicators in draft order view

* Handle unfilled fields errors before draft order finalize

* Handle draft order line errors

* Differentiate line alert severity

* Fix order line alert margin

* Remove unnecessairy comment

* Refactor order draft alert components

* Update order draft test snapshots

* Refaactor order details code

* Hide add products button when no products available on order draft page

* Hide no shipping methods warning if they cannot be determined

* Update product assignment dialog messaages

* Update order channel error messages

* Fix missing order lines in error crash
2022-08-10 10:11:32 +01:00

67 lines
1.9 KiB
TypeScript

import {
ChannelUsabilityDataQuery,
OrderDetailsFragment,
} from "@saleor/graphql";
import { Alert, AlertProps } from "@saleor/macaw-ui";
import React from "react";
import { MessageDescriptor, useIntl } from "react-intl";
import OrderAlerts from "../OrderAlerts";
import { alertMessages } from "./messages";
import { useAlertStyles } from "./styles";
const getAlerts = (
order?: OrderDetailsFragment,
channelUsabilityData?: ChannelUsabilityDataQuery,
) => {
const canDetermineShippingMethods =
order?.shippingAddress?.country.code && !!order?.lines?.length;
const isChannelInactive = order && !order.channel.isActive;
const noProductsInChannel = channelUsabilityData?.products.totalCount === 0;
const noShippingMethodsInChannel =
canDetermineShippingMethods && order?.shippingMethods.length === 0;
let alerts: MessageDescriptor[] = [];
if (isChannelInactive) {
alerts = [...alerts, alertMessages.inactiveChannel];
}
if (noProductsInChannel) {
alerts = [...alerts, alertMessages.noProductsInChannel];
}
if (noShippingMethodsInChannel) {
alerts = [...alerts, alertMessages.noShippingMethodsInChannel];
}
return alerts;
};
export type OrderDraftAlertProps = Omit<AlertProps, "variant" | "close"> & {
order?: OrderDetailsFragment;
channelUsabilityData?: ChannelUsabilityDataQuery;
};
const OrderDraftAlert: React.FC<OrderDraftAlertProps> = props => {
const { order, channelUsabilityData, ...alertProps } = props;
const classes = useAlertStyles();
const intl = useIntl();
const alerts = getAlerts(order, channelUsabilityData);
if (!alerts.length) {
return null;
}
return (
<Alert variant="warning" close className={classes.root} {...alertProps}>
<OrderAlerts
alerts={alerts}
alertsHeader={intl.formatMessage(alertMessages.manyAlerts)}
/>
</Alert>
);
};
OrderDraftAlert.displayName = "OrderDraftAlert";
export default OrderDraftAlert;