import clsx from "clsx" import { useAdminGetDiscountByCode, useAdminShippingOptions, } from "medusa-react" import { useContext, useEffect, useMemo, useState } from "react" import { useWatch } from "react-hook-form" import { useTranslation } from "react-i18next" import Avatar from "../../../../components/atoms/avatar" import Button from "../../../../components/fundamentals/button" import CrossIcon from "../../../../components/fundamentals/icons/cross-icon" import PlusIcon from "../../../../components/fundamentals/icons/plus-icon" import ImagePlaceholder from "../../../../components/fundamentals/image-placeholder" import Input from "../../../../components/molecules/input" import { SteppedContext } from "../../../../components/molecules/modal/stepped-modal" import Table from "../../../../components/molecules/table" import isNullishObject from "../../../../utils/is-nullish-object" import { displayAmount, extractOptionPrice } from "../../../../utils/prices" import { useNewOrderForm } from "../form" const Summary = () => { const [showAddDiscount, setShowAddDiscount] = useState(false) const [discError, setDiscError] = useState(undefined) const [code, setCode] = useState(undefined) const { t } = useTranslation() const { form, context: { items, region: regionObj, selectedShippingOption }, } = useNewOrderForm() const shipping = useWatch({ defaultValue: undefined, control: form.control, name: "shipping_address", }) const billing = useWatch({ defaultValue: undefined, control: form.control, name: "billing_address", }) const email = useWatch({ control: form.control, name: "email", }) const region = useWatch({ control: form.control, name: "region", }) const discountCode = useWatch({ control: form.control, name: "discount_code", }) const shippingOption = useWatch({ control: form.control, name: "shipping_option", }) const customShippingPrice = useWatch({ control: form.control, name: "custom_shipping_price", }) const { discount, status, isFetching } = useAdminGetDiscountByCode( discountCode!, { enabled: !!discountCode, } ) const { shipping_options } = useAdminShippingOptions( { region_id: region?.value }, { enabled: !!region && !!shippingOption, } ) const shippingOptionPrice = useMemo(() => { if (!shippingOption || !shipping_options) { return 0 } const option = shipping_options.find((o) => o.id === shippingOption.value) if (!option) { return 0 } return option.amount || 0 }, [shipping_options, shippingOption]) const handleAddDiscount = async () => { form.setValue("discount_code", code) } useEffect(() => { if (!discount || !regionObj) { return } if (!discount.regions.find((d) => d.id === regionObj.id)) { setDiscError( t( "components-the-discount-is-not-applicable-to-the-selected-region", "The discount is not applicable to the selected region" ) ) setCode(undefined) form.setValue("discount_code", undefined) setShowAddDiscount(true) } }, [discount]) useEffect(() => { if (status === "error") { setDiscError( t( "components-the-discount-code-is-invalid", "The discount code is invalid" ) ) setCode(undefined) form.setValue("discount_code", undefined) setShowAddDiscount(true) } }, [status]) const onDiscountRemove = () => { form.setValue("discount_code", undefined) setShowAddDiscount(false) setCode("") } return (
Details {t("components-quantity", "Quantity")} {t("components-price-excl-taxes", "Price (excl. Taxes)")} {regionObj && items && items.fields.map((item) => { return (
{item.thumbnail ? ( ) : ( )}
{item.product_title} {item.title}
{item.quantity} {displayAmount(regionObj?.currency_code, item.unit_price)}
) })}
{!showAddDiscount && !discount?.rule && (
)} {showAddDiscount && !discount?.rule && ( <>
setDiscError(undefined)} value={code} onChange={(e) => setCode(e.target.value)} />
{discError && {discError}}
)} {discount && regionObj && (
{t("components-discount", "Discount")} {t("select-shipping-code", "(Code: {{code}})", { code: discount.code, })} onDiscountRemove()} className="inter-small-semibold text-violet-60 cursor-pointer" >
{t("components-type", "Type")} {discount.rule.type !== "free_shipping" ? `${discount.rule.type .charAt(0) .toUpperCase()}${discount.rule.type.slice(1)}` : "Free Shipping"}
{discount.rule.type !== "free_shipping" && (
{t("components-value", "Value")} {discount.rule.type === "fixed" ? `${displayAmount( regionObj.currency_code, discount.rule.value )} ${regionObj.currency_code.toUpperCase()}` : `${discount.rule.value} %`}
)}
)}
{email}
{selectedShippingOption && (
{!isNullishObject(shipping) && shipping && (
{t("components-address", "Address")} {shipping.address_1}, {shipping.address_2} {`${shipping.postal_code} ${shipping.city}, ${shipping.country_code?.label}`}
)} {regionObj && (
{t("components-shipping-method", "Shipping method")} {selectedShippingOption.name} -{" "} {customShippingPrice && regionObj ? (

{extractOptionPrice(shippingOptionPrice, regionObj)} {displayAmount( regionObj.currency_code, customShippingPrice )} {regionObj.currency_code.toUpperCase()}

) : ( extractOptionPrice(selectedShippingOption.amount, regionObj) )}
)}
)} {!isNullishObject(billing) && billing && ( {t("components-address", "Address")} {billing.address_1}, {billing.address_2} {`${billing.postal_code} ${billing.city}, ${billing.country_code.label}`} )}
) } const SummarySection = ({ title, editIndex, children }) => { const { setPage } = useContext(SteppedContext) const { t } = useTranslation() return (
{title} setPage(editIndex)} className="inter-small-semibold text-violet-60 cursor-pointer" > {t("components-edit", "Edit")}
{children}
) } export default Summary