import { InformationCircleSolid } from "@medusajs/icons" import { Badge, CurrencyInput, Divider, Label, Text, Tooltip, } from "@medusajs/ui" import { Fragment, ReactNode } from "react" import { Control, useWatch, useFormContext } from "react-hook-form" import { Trans, useTranslation } from "react-i18next" import { z } from "zod" import { castNumber } from "../../../../../lib/cast-number" import { CurrencyInfo } from "../../../../../lib/data/currencies" import { getLocaleAmount } from "../../../../../lib/money-amount-helpers" import { CreateShippingOptionSchemaType } from "../../../location-service-zone-shipping-option-create/components/create-shipping-options-form/schema" import { CondtionalPriceRuleSchema, UpdateConditionalPriceRuleSchema, } from "../../schema" import { ConditionalPriceInfo } from "../../types" import { getCustomShippingOptionPriceFieldName } from "../../utils/get-custom-shipping-option-price-field-info" import { useShippingOptionPrice } from "../shipping-option-price-provider" 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 ConditionalPriceFormSchema = z.union([ CondtionalPriceRuleSchema, UpdateConditionalPriceRuleSchema, ]) type ConditionalPriceFormSchemaType = z.infer interface ConditionalPriceFormProps { info: ConditionalPriceInfo variant: "create" | "update" } const ConditionContainer = ({ children }: { children: ReactNode }) => (
{children}
) const ConditionDisplay = ({ index, control, currency, }: { index: number control: Control currency: CurrencyInfo }) => { const { t, i18n } = useTranslation() const gte = useWatch({ control, name: `prices.${index}.gte`, }) const lte = useWatch({ control, name: `prices.${index}.lte`, }) const castGte = gte ? castNumber(gte) : undefined const castLte = lte ? castNumber(lte) : undefined if (!castGte && !castLte) { return null } if (castGte && !castLte) { return ( , , ]} values={{ attribute: t( "stockLocations.shippingOptions.conditionalPrices.attributes.cartItemTotal" ), gte: getLocaleAmount(castGte, currency.code), }} /> ) } if (!castGte && castLte) { return ( , , ]} values={{ attribute: t( "stockLocations.shippingOptions.conditionalPrices.attributes.cartItemTotal" ), lte: getLocaleAmount(castLte, currency.code), }} /> ) } if (castGte && castLte) { return ( , , , ]} values={{ attribute: t( "stockLocations.shippingOptions.conditionalPrices.attributes.cartItemTotal" ), gte: getLocaleAmount(castGte, currency.code), lte: getLocaleAmount(castLte, currency.code), }} /> ) } return null } const ConditionalConditionItem = ({ index, control, currency, }: { index: number control: Control currency: CurrencyInfo }) => { const { t } = useTranslation() return ( <> (
{t( "stockLocations.shippingOptions.conditionalPrices.rules.amount" )}
onChange(values?.value ? values?.value : "") } autoFocus={false} {...props} />
)} /> ( ( onChange(values?.value ? values?.value : "") } {...fieldProps} /> )} /> )} /> ( ( onChange(values?.value ? values?.value : "") } {...fieldProps} /> )} /> )} /> ) } const ReadOnlyConditions = ({ index, control, currency, }: { index: number control: Control currency: CurrencyInfo }) => { const { t } = useTranslation() const item = useWatch({ control, name: `prices.${index}`, }) if (item.eq == null && item.gt == null && item.lt == null) { return null } return (
{t( "stockLocations.shippingOptions.conditionalPrices.customRules.label" )}
{item.eq != null && (
)} {item.gt != null && (
)} {item.lt != null && (
)}
) } export const ConditionalPriceForm = ({ info, variant, }: ConditionalPriceFormProps) => { const { t } = useTranslation() const { getValues, setValue: setFormValue } = useFormContext() const { onCloseConditionalPricesModal } = useShippingOptionPrice() const { field, type, currency, name: header } = info const name = getCustomShippingOptionPriceFieldName(field, type) return ( { setFormValue(name, values.prices, { shouldDirty: true, shouldValidate: true, shouldTouch: true, }) onCloseConditionalPricesModal() }} onClose={onCloseConditionalPricesModal} currency={currency} header={t("stockLocations.shippingOptions.conditionalPrices.header", { name: header, })} description={t( "stockLocations.shippingOptions.conditionalPrices.description" )} addPriceLabel={t( "stockLocations.shippingOptions.conditionalPrices.actions.addPrice" )} fieldConfig={{ min: "gte", max: "lte", minLabel: t( "stockLocations.shippingOptions.conditionalPrices.rules.gte" ), maxLabel: t( "stockLocations.shippingOptions.conditionalPrices.rules.lte" ), }} renderConditionTrigger={(props) => } renderConditionItem={(props) => } /> ) }