import { ReactNode } from "react" import { Control, useWatch } from "react-hook-form" import { Trans, useTranslation } from "react-i18next" import { z } from "zod" import { Badge, CurrencyInput, Divider, Text } from "@medusajs/ui" import { CubeSolid } from "@medusajs/icons" import { i18n } from "../../../../../components/utilities/i18n" import { CurrencyInfo } from "../../../../../lib/data/currencies" import { PriceListUpdateCurrencyPrice } from "../../schemas" import { formatQuantityPrices } from "../../../common/utils" import { Form } from "../../../../../components/common/form" import { formatValue } from "react-currency-input-field" import { TieredPriceInput } from "../../../../../common/components/tiered-price-form/tiered-price-input" import { TieredPriceForm } from "../../../../../common/components/tiered-price-form/tiered-price-form" const QuantityPriceRuleSchema = z .object({ amount: z.string().optional(), min_quantity: z.string().nullish(), max_quantity: z.string().nullish(), id: z.string().optional(), }) .refine( (data) => { const min = data.min_quantity ? parseInt(data.min_quantity, 10) : null const max = data.max_quantity ? parseInt(data.max_quantity, 10) : null return min === null || max === null || min <= max }, { message: i18n.t("priceLists.quantityPricing.errors.minGreaterThanMax"), path: ["min_quantity"], } ) const QuantityPriceFormSchema = z.object({ prices: z.array(QuantityPriceRuleSchema), }) type QuantityPriceFormSchemaType = z.infer interface QuantityPriceFormProps { info: { currency: CurrencyInfo name: string prices: PriceListUpdateCurrencyPrice[] } onClose: () => void onSave: (prices: PriceListUpdateCurrencyPrice[]) => void } const ConditionContainer = ({ children }: { children: ReactNode }) => (
{children}
) const QuantityConditionTrigger = ({ index, control, }: { index: number control: Control }) => { const { t } = useTranslation() const minQuantity = useWatch({ control, name: `prices.${index}.min_quantity`, }) const maxQuantity = useWatch({ control, name: `prices.${index}.max_quantity`, }) const min = minQuantity || undefined const max = maxQuantity || undefined if (!min && !max) { return null } const attribute = t("priceLists.quantityPricing.attributes.quantity") if (min && !max) { return ( , , ]} values={{ attribute, min }} /> ) } if (!min && max) { return ( , , ]} values={{ attribute, max }} /> ) } return ( , , , ]} values={{ attribute, min, max }} /> ) } const QuantityConditionItem = ({ index, control, currency, }: { index: number control: Control currency: CurrencyInfo }) => { const { t } = useTranslation() return ( <> (
{t("priceLists.quantityPricing.rules.amount")}
onChange(values?.value ? values?.value : "") } autoFocus={false} {...props} />
)} /> ( (
{t("priceLists.quantityPricing.rules.qty")} { const val = e.target.value.replace(/\D/g, "") if (val === "") { onChange("") return } if (parseInt(val, 10) < 1) { return } onChange(val) }} {...fieldProps} />
)} /> )} /> ( (
{t("priceLists.quantityPricing.rules.qty")} { const val = e.target.value.replace(/\D/g, "") if (val === "") { onChange("") return } if (parseInt(val, 10) < 1) { return } onChange(val) }} {...fieldProps} />
)} /> )} /> ) } export const QuantityPriceForm = ({ info, onClose, onSave, }: QuantityPriceFormProps) => { const { t } = useTranslation() const { currency, name, prices: initialPrices } = info return ( ({ amount: p.amount?.toString() || "", min_quantity: p.min_quantity?.toString() ?? null, max_quantity: p.max_quantity?.toString() ?? null, id: p.id ?? undefined, }) )} defaultRow={{ amount: "", min_quantity: "", max_quantity: null }} onSubmit={(values) => onSave(formatQuantityPrices(values.prices))} onClose={onClose} currency={currency} header={t("priceLists.quantityPricing.header", { name: name, })} description={t("priceLists.quantityPricing.description")} addPriceLabel={t("priceLists.quantityPricing.actions.addPrice")} fieldConfig={{ min: "min_quantity", max: "max_quantity", minLabel: t("priceLists.quantityPricing.rules.minQuantity"), maxLabel: t("priceLists.quantityPricing.rules.maxQuantity"), }} renderConditionTrigger={(props) => ( )} renderConditionItem={(props) => } /> ) }