import { Order } from "@medusajs/medusa" import { useAdminCustomer, useAdminCustomers, useAdminUpdateOrder, } from "medusa-react" import moment from "moment" import React from "react" import { useTranslation } from "react-i18next" import { DisplayTotalAmount, FulfillmentStatusComponent, PaymentStatusComponent, } from "../../../domain/orders/details/templates" import { useDebounce } from "../../../hooks/use-debounce" import useNotification from "../../../hooks/use-notification" import { Option } from "../../../types/shared" import Badge from "../../fundamentals/badge" import Button from "../../fundamentals/button" import Modal from "../../molecules/modal" import Select from "../../molecules/select/next-select/select" type TransferOrdersModalProps = { order: Order onDismiss: () => void } const TransferOrdersModal: React.FC = ({ order, onDismiss, }) => { const { t } = useTranslation() const [customersQuery, setCustomersQuery] = React.useState("") const debouncedCustomersQuery = useDebounce(customersQuery, 400) const { customers } = useAdminCustomers({ q: debouncedCustomersQuery, has_account: true, limit: 30, offset: 0, }) const notification = useNotification() const [selectedCustomer, setSelectedCustomer] = React.useState<{ label: string value: string } | null>(null) const { mutate: updateOrder, isLoading } = useAdminUpdateOrder(order.id) const { customer, isLoading: isLoadingCustomer } = useAdminCustomer( selectedCustomer?.value || "" ) const onSubmit = async () => { if (isLoadingCustomer || !customer) { return } if (customer.id === order.customer_id) { notification( t("transfer-orders-modal-info", "Info"), t( "transfer-orders-modal-customer-is-already-the-owner-of-the-order", "Customer is already the owner of the order" ), "info" ) onDismiss() return } updateOrder( { customer_id: customer?.id, email: customer.email }, { onSuccess: () => { notification( t("transfer-orders-modal-success", "Success"), t( "transfer-orders-modal-successfully-transferred-order-to-different-customer", "Successfully transferred order to different customer" ), "success" ) onDismiss() }, onError: () => { notification( t("transfer-orders-modal-error", "Error"), t( "transfer-orders-modal-could-not-transfer-order-to-different-customer", "Could not transfer order to different customer" ), "error" ) }, } ) } const getCustomerOption = (customer: { id: string email: string first_name?: string last_name?: string }) => { if (!customer) { return undefined } const customerLabel = (c: { email: string first_name?: string last_name?: string }) => { if (c.first_name && c.last_name) { return `${c.first_name} ${c.last_name} - ${c.email}` } else if (c.first_name) { return `${c.first_name} - ${c.email}` } else if (c.last_name) { return `${c.last_name} - ${c.email}` } return `${c.email}` } return { value: customer.id, label: customerLabel(customer), } } const customerOptions = React.useMemo(() => { const isOption = (c: Option | undefined): c is Option => { return !!c } return customers?.map((c) => getCustomerOption(c)).filter(isOption) || [] }, [customers]) return (

{t("transfer-orders-modal-transfer-order", "Transfer order")}

{t("transfer-orders-modal-order", "Order")}

{`#${order.display_id}`} {moment(new Date(order.created_at)).format("MMM D, H:mm A")}
{t("transfer-orders-modal-current-owner", "Current Owner")} {t( "transfer-orders-modal-the-customer-currently-related-to-this-order", "The customer currently related to this order" )}
{ setSelectedCustomer(value) }} isMulti={false} options={customerOptions} isSearchable={true} onInputChange={(value) => { setCustomersQuery(value) }} truncateOption={true} />
) } export default TransferOrdersModal