import React, { useEffect, useState } from "react"; import { LoadingButton, LoadingButtonProps } from "@mui/lab"; import { Box } from "@mui/material"; import CircularProgress from "@mui/material/CircularProgress"; import Grid from "@mui/material/Grid2"; import { useSnackbar } from "notistack"; import Card from "../../../../components/Card"; import CardActions from "../../../../components/Card/CardActions"; import CardCancelButton from "../../../../components/Card/CardCancelButton"; import { CardContent } from "../../../../components/Card/CardContent"; import CardHeader from "../../../../components/Card/CardHeader"; import CardSaveButton from "../../../../components/Card/CardSaveButton"; import Chip from "../../../../components/Chip"; import { useApi } from "../../../../contexts/ApiContext"; import { useCardContext } from "../../../../contexts/CardContext"; import { useDialog } from "../../../../contexts/DialogContext"; import { useUser } from "../../../../contexts/UserContext"; import { hasPermission } from "../../../../util/has_permission"; import type { OrderDetail, OrderItem, OrderListItem } from "../../types/order"; import RecipientCard from "../RecipientCard"; import ShipmentCard from "../ShipmentCard"; import CancelOrderButton from "./CancelOrderButton"; import OrderDestinationCard from "./OrderDestinationCard"; import OrderItemsTable from "./OrderItemsTable"; import OrderStateChip from "./OrderStateChip"; export interface OrderDetailProps { order: OrderListItem; } const OrderDetail: React.FC = ({ order }) => { const api = useApi(); const { enqueueSnackbar } = useSnackbar(); const [details, setDetails] = useState(null); const [buttonsLoading, setButtonsLoading] = useState(false); const [selected, setSelected] = useState([]); const { user } = useUser(); const loadDetails = () => { setDetails(null); api.operations["fulfillment.order:detail"] .call({ params: { reference: order.reference, }, }) .then(async (response) => setDetails(await response.json())); }; useEffect(() => { if (details == null) loadDetails(); }); const openDialog = useDialog(); const markAsArrived = async () => { if ( !(await openDialog( "Are you sure?", "This will notify the customer that the order is ready for pick up", )) ) { return; } setButtonsLoading(true); api.operations["fulfillment.order:ready-for-pickup"] .call({ params: { reference: order.reference }, body: selected, }) .then((response) => { if (response.ok) { enqueueSnackbar("Order is now ready for pickup", { variant: "success", }); } else { enqueueSnackbar("Failed to mark order as ready for pickup", { variant: "error", }); throw response; } }) .finally(() => { setButtonsLoading(false); loadDetails(); }); }; const markAsDelivered = async () => { if (!(await openDialog("Are you sure?", "This will mark the order as delivered"))) { return; } setButtonsLoading(true); api.operations["fulfillment.order:delivered"] .call({ params: { reference: order.reference }, }) .then((response) => { if (response.ok) { enqueueSnackbar("Successfully marked order as delivered", { variant: "success", }); } else { enqueueSnackbar("Failed to mark order as delivered", { variant: "error", }); throw response; } }) .finally(() => { setButtonsLoading(false); loadDetails(); }); }; const doCancel = async () => { if ( !(await openDialog( "Are you sure?", "This will fully cancel the order and release the authorized payment back to the customer", )) ) { return; } api.operations["fulfillment.order:cancel"] .call({ params: { reference: order.reference }, }) .then((response) => { if (response.ok) { enqueueSnackbar("Order cancelled", { variant: "success", }); } else { enqueueSnackbar("Failed to cancel order", { variant: "error", }); throw response; } }) .finally(() => { setButtonsLoading(false); loadDetails(); }); }; const StatusButton = () => { const { isEditing } = useCardContext(); if (details == null) return null; const props: LoadingButtonProps = { variant: "contained", color: "secondary", loading: buttonsLoading, disabled: buttonsLoading, }; if (["DELIVERED", "CANCELLED"].includes(details.state)) { return null; } if (isEditing) { props.disabled ||= selected.length === 0; return ( Ready for pickup ); } if (isEditing && ["IN_TRANSIT", "READY_FOR_PICKUP", "SENT"].includes(details.state)) { return ( Delivered ); } return null; }; return details != null ? ( {details.channel ? : null} } sx={{ borderBottomWidth: 1, borderBottomStyle: "solid", borderBottomColor: "divider", }} title="Order" > {details.channel ? : null} {hasPermission(user, "fulfillment.change_order") && ( )} {details.shipment ? ( ) : null} {details.recipient ? ( ) : null} ) : ( ); }; export default OrderDetail;