import { Address } from "@medusajs/medusa" import { useAdminDeleteDraftOrder, useAdminDraftOrder, useAdminDraftOrderRegisterPayment, useAdminStore, useAdminUpdateDraftOrder, } from "medusa-react" import moment from "moment" import { useEffect, useState } from "react" import { useNavigate, useParams } from "react-router-dom" import { useTranslation } from "react-i18next" import Avatar from "../../../components/atoms/avatar" import BackButton from "../../../components/atoms/back-button" import CopyToClipboard from "../../../components/atoms/copy-to-clipboard" import Spinner from "../../../components/atoms/spinner" import WidgetContainer from "../../../components/extensions/widget-container" import Button from "../../../components/fundamentals/button" import DetailsIcon from "../../../components/fundamentals/details-icon" import DollarSignIcon from "../../../components/fundamentals/icons/dollar-sign-icon" import TruckIcon from "../../../components/fundamentals/icons/truck-icon" import StatusDot from "../../../components/fundamentals/status-indicator" import JSONView from "../../../components/molecules/json-view" import BodyCard from "../../../components/organisms/body-card" import ConfirmationPrompt from "../../../components/organisms/confirmation-prompt" import DeletePrompt from "../../../components/organisms/delete-prompt" import { AddressType } from "../../../components/templates/address-form" import useNotification from "../../../hooks/use-notification" import { useWidgets } from "../../../providers/widget-provider" import { isoAlpha2Countries } from "../../../utils/countries" import { getErrorMessage } from "../../../utils/error-messages" import extractCustomerName from "../../../utils/extract-customer-name" import { formatAmountWithSymbol } from "../../../utils/prices" import AddressModal from "../details/address-modal" import DraftSummaryCard from "../details/detail-cards/draft-summary" import { DisplayTotal, FormattedAddress } from "../details/templates" type DeletePromptData = { resource: string onDelete: () => any show: boolean } const DraftOrderDetails = () => { const { id } = useParams() const { t } = useTranslation() const initDeleteState: DeletePromptData = { resource: "", onDelete: () => Promise.resolve(console.log("Delete resource")), show: false, } const [deletePromptData, setDeletePromptData] = useState(initDeleteState) const [addressModal, setAddressModal] = useState(null) const [showMarkAsPaidConfirmation, setShowAsPaidConfirmation] = useState(false) const { draft_order, isLoading } = useAdminDraftOrder(id!) const { store, isLoading: isLoadingStore } = useAdminStore() const [paymentLink, setPaymentLink] = useState("") useEffect(() => { if (store && draft_order && store.payment_link_template) { setPaymentLink( store.payment_link_template.replace(/\{cart_id\}/, draft_order.cart_id) ) } }, [isLoading, isLoadingStore]) const markPaid = useAdminDraftOrderRegisterPayment(id!) const cancelOrder = useAdminDeleteDraftOrder(id!) const updateOrder = useAdminUpdateDraftOrder(id!) const navigate = useNavigate() const notification = useNotification() const OrderStatusComponent = () => { switch (draft_order?.status) { case "completed": return ( ) case "open": return ( ) default: return null } } const PaymentActionables = () => { // Default label and action const label = t("draft-orders-mark-as-paid", "Mark as paid") const action = () => setShowAsPaidConfirmation(true) return ( ) } const onMarkAsPaidConfirm = async () => { try { await markPaid.mutateAsync() notification( t("draft-orders-success", "Success"), t( "draft-orders-successfully-mark-as-paid", "Successfully mark as paid" ), "success" ) } catch (err) { notification( t("draft-orders-error", "Error"), getErrorMessage(err), "error" ) } finally { setShowAsPaidConfirmation(false) } } const handleDeleteOrder = async () => { return cancelOrder.mutate(void {}, { onSuccess: () => notification( t("draft-orders-success", "Success"), t( "draft-orders-successfully-canceled-order", "Successfully canceled order" ), "success" ), onError: (err) => notification( t("draft-orders-error", "Error"), getErrorMessage(err), "error" ), }) } const { getWidgets } = useWidgets() const afterWidgets = getWidgets("draft_order.details.after") const beforeWidgets = getWidgets("draft_order.details.before") const { cart } = draft_order || {} const { region } = cart || {} return (
{isLoading || !draft_order ? ( ) : (
{beforeWidgets?.length > 0 && (
{beforeWidgets.map((w, i) => { return ( ) })}
)} } customActionable={ draft_order?.status === "completed" && ( ) } forceDropdown={draft_order?.status === "completed" ? false : true} actionables={ draft_order?.status === "completed" ? [ { label: t("draft-orders-go-to-order", "Go to Order"), icon: null, onClick: () => console.log("Should not be here"), }, ] : [ { label: t( "draft-orders-cancel-draft-order", "Cancel Draft Order" ), icon: null, // icon: , variant: "danger", onClick: () => setDeletePromptData({ resource: t( "draft-orders-draft-order", "Draft Order" ), onDelete: () => handleDeleteOrder(), show: true, }), }, ] } >
{t("draft-orders-email", "Email")}
{cart?.email}
{t("draft-orders-phone", "Phone")}
{cart?.shipping_address?.phone || "N/A"}
{t("draft-orders-amount", "Amount {{currency_code}}", { currency_code: region?.currency_code.toUpperCase(), })}
{cart?.total && region?.currency_code ? formatAmountWithSymbol({ amount: cart?.total, currency: region?.currency_code, }) : "N/A"}
} >
{draft_order?.status !== "completed" && (
{t("draft-orders-payment-link", "Payment link:")} {store?.payment_link_template ? ( ) : ( t( "draft-orders-configure-payment-link-in-store-settings", "Configure payment link in store settings" ) )}
)}
{cart?.shipping_methods.map((method) => (
{t("draft-orders-shipping-method", "Shipping Method")} {method?.shipping_option.name || ""}
{t("draft-orders-data", "Data")}{" "} {t("draft-orders-1-item", "(1 item)")}
))}
, onClick: () => setAddressModal({ address: cart?.shipping_address, type: AddressType.SHIPPING, }), }, { label: t( "draft-orders-edit-billing-address", "Edit Billing Address" ), icon: , onClick: () => { if (cart?.billing_address) { setAddressModal({ address: cart?.billing_address, type: AddressType.BILLING, }) } }, }, { label: t("draft-orders-go-to-customer", "Go to Customer"), icon: , // TODO: Change to Contact icon onClick: () => navigate(`/a/customers/${cart?.customer.id}`), }, ]} >

{extractCustomerName(cart)}

{cart?.shipping_address && ( {cart.shipping_address.city},{" "} { isoAlpha2Countries[ cart.shipping_address.country_code?.toUpperCase() ] } )}
{t("draft-orders-contact", "Contact")}
{cart?.email} {cart?.shipping_address?.phone || ""}
{afterWidgets?.length > 0 && (
{afterWidgets.map((w, i) => { return ( ) })}
)}
)} {addressModal && ( setAddressModal(null)} onSave={updateOrder.mutate} address={addressModal.address} type={addressModal.type} allowedCountries={region?.countries} /> )} {/* An attempt to make a reusable delete prompt, so we don't have to hold +10 state variables for showing different prompts */} {deletePromptData.show && ( deletePromptData.onDelete()} handleClose={() => setDeletePromptData(initDeleteState)} /> )} {showMarkAsPaidConfirmation && ( setShowAsPaidConfirmation(false)} onConfirm={onMarkAsPaidConfirm} /> )}
) } export default DraftOrderDetails