import {ComponentMeta} from "../ComponentsMeta"; import React from "react"; import {iconWithClassName} from "../IconWithClassName"; import {CardRenderer} from "../CardRenderers"; import {__} from "../../globals"; import {usingOfferOptions} from "./CardHelpers"; import {FieldType} from "../fields"; import {NumberProps} from "../fields/Number"; import {OfferCardContent} from "./OfferCardContent"; import {renderCurrencyValue} from "../fields/helpers"; import classNames from "classnames"; import context from "../../Context"; import {RowOptions} from "./Fields/RowOptions"; export type ShippingDiscountOptions = { type: 'percentage' | 'amount' | 'finalCost' amount: number } export const type = 'ShippingDiscount' export const ShippingDiscountMeta: ComponentMeta = { id: type, type: 'offers', icon: iconWithClassName(({className, context}) => { if (context === 'card') { // solid return } // outline return }), fieldsMap: ({options: {type}}) => [ [ { id: 'type', renderType: FieldType.Tab, } /* { id: 'type', renderType: FieldType.Select, fieldOptions: {/!*optionsKey: '_allowed',*!/ centered: true}, fieldProps: {size: 'small', style: 'gray', width: 'auto'} as MultiSelectSearchProps, } */ ], [ new RowOptions({ type: 'featured', colWidth: 'auto' }), { id: 'amount', renderType: FieldType.Number, fieldProps: { hasAmountStructure: false, type: type === 'percentage' ? 'percentage' : 'currency', border: false } as Partial } ] ] } export const ShippingDiscountRenderer: CardRenderer = { type: type, prefix: "*", example: __('50% off, $5 off, $10 shipping'), icon: ShippingDiscountMeta.icon!, AboveComputedValue: () => false, ComputedValue: usingOfferOptions(({amount, type}, {color}, offer) => { const renderOfferMainValue = (value: string | number, type?: 'normal' | 'number' | 'percentage' | 'amount' | 'fixed-price') => { let off = OFF; return
{type === 'amount' &&
{
{renderCurrencyValue(value)}
} {off}
} {type === 'fixed-price' &&
For{
{renderCurrencyValue(value)}
}
} { //@ts-ignore !(['amount' as const, 'fixed-price' as const]).includes(type!) && value } {type === 'percentage' && % {off}}
; } const renderDiscountValue = () => { if (type === 'percentage') { return renderOfferMainValue(amount, 'percentage'); } if (type === 'amount') { return renderOfferMainValue(amount, 'amount'); } if (type === 'finalCost') { return renderOfferMainValue(amount, 'fixed-price'); } // amount or fixed-price return {amount}; } return {true &&
{
{renderDiscountValue()}
}
}
}), }