import React, {FC, useEffect, useState} from "react"; import {MultiSelect} from "./fields/MultiSelect"; import {ProductsFieldProps} from "./cards/Products"; import {__, CouponsPlus} from "../globals"; import {MultiSelectSearch} from "./fields/MultiSelectSearch"; import {mapLocalMetaFieldOptions} from "./cards/FieldHelpers"; import {NumberInput, NumberInputProps} from "./fields/NumberInput"; import {getBOGOOfferNumberInputProps, getNumberContainerClasses} from "./fields/NumberFieldUtils"; import {Tabs} from "./fields/tabs"; import {mapFromMeta} from "./fields/TabHelpers"; import {BuyXGetXOptions, Qty, qtyType} from "./cards/BuyXGetX"; import {BuyXGetYSpecificProductWindowContextData} from "./atoms"; export type BuyXGetYSpecificProductConfigFieldsProps = { context?: BuyXGetYSpecificProductWindowContextData | null onDataModified: (data: Required>['data']) => void } const qtyTypesOptions = [ { id: 'simple', value: 'simple', label: __('Fixed Qty.'), }, { id: 'advanced', value: 'advanced', label: __('Any Qty.'), } ] export const BuyXGetYSpecificProductConfigFields: FC = ({context, onDataModified}) => { const [productId, setProductId] = useState(context?.data?.id ?? null); const [isVariable, setIsVariable] = useState(context?.data?.isVariable ?? false); const [quantity, setQuantity] = useState(context?.data?.quantity ?? 1); const [qtyType, setQtyType] = useState(context?.data?.qtyType ?? 'simple'); const [qty, setQty] = useState(context?.data?.qty ?? {type: 'any', amount: 1}); const [discountType, setDiscountType] = useState(context?.data?.discount?.type ?? 'free'); const [discountAmount, setDiscountAmount] = useState(context?.data?.discount?.amount ?? 100); const [notInCartMode, setNotInCartMode] = useState(context?.data?.missingItemsInCart.mode ?? 'auto-add'); useEffect(() => { // when type changes to advanced, change apply mode to manual if (qtyType === 'advanced' && notInCartMode !== 'manual-add') { setNotInCartMode('manual-add'); } }, [qtyType]) useEffect(() => { onDataModified({ ...(context?.data || {}), // @ts-ignore id: productId, isVariable, quantity, qtyType, qty, discount: { type: discountType, amount: discountAmount }, missingItemsInCart: { mode: notInCartMode } }) }, [productId, isVariable, quantity, discountType, discountAmount, notInCartMode, qtyType, qty]) let inputtype = ({ free: 'free', ["percentage"]: 'percentage', ["amount"]: "currency", ["fixed-price"]: "currency", } as Record)[discountType]; return
value) as string[]} onSelectedValue={(value) => { setProductId(value.id) setIsVariable(value.value.option.isVariable) }} onRemovedValue={() => { }} {...ProductsFieldProps({ SelectSearchProps: { placeholder: () => productId ? __('Search to replace selected product...') : __('Search product to discount...'), } })} /> {productId &&
{qtyType === 'advanced' &&

{__('Unlimited')}

} {qtyType === 'advanced' &&

{__('Manual only')}

}
} input={{ top:() =>
{ setQtyType(id as qtyType) }} />
}} labels={{ //top: __('Quantity'), }} />
{ setDiscountType(id as BuyXGetXOptions['discount']['type']) }} />

{__('When these items aren\'t in the cart')}

} ; }