import { __, CouponsPlus } from "../../../globals"; import { ComponentMeta } from "../../ComponentsMeta"; import { iconWithClassName } from "../../IconWithClassName"; import { CardRenderer, CardRendererContext } from "../../CardRenderers"; import { usingTestableOptions } from "../CardHelpers"; import { ComponentCategory } from "../../components"; import { inclusionValueValidatorFieldsMap } from "../InclusionValueValidator"; import { FieldType } from "../../fields"; import { SelectOption } from "../../fields/MultiSelectSearch"; import { fieldsCacheContainer } from "../../FieldsCache"; import { colors } from "../../Colors"; import { CustomProductOptionValue } from "../Products.card"; import { TierType } from "../../tiers"; import { getCardComponentData } from "../../cards"; import { request } from "../../Color"; const _checkOnLabel = { billingAdress: __('Billing Address'), shippingAddress: __('Shipping Address'), ipAddress: __('IP Address'), } export type LocationOptions = { inclusionType: 'allowed' | 'forbidden', //locationDepth: 'country' | 'state', no longer needed in 3.0+! areas: string[], // string of codes, e.g. ['US', 'MX',] OR ['US:CA', 'US:NY'] checkOn: { billingAddress: boolean, shippingAddress: boolean, IP: boolean, } } const type = CouponsPlus?.components?.conditions?.Location?.type || 'Location' export const LocationMeta: ComponentMeta = { id: type, type: 'conditions', relatedSearchTerms: [ 'place', 'area', 'zone', 'state', 'province', 'region', 'country', 'territory', ], category: ComponentCategory.Account, priority: 4, supportsTiers: true, tierFields: ['areas'], tierType: TierType.Fixed, configurable: context => true, // configurable even in tier bases! icon: iconWithClassName(({className, context}) => { if (context === 'card') { // solid return } // outline return }), fieldsMap: ({options: {}}: { options: Partial }, scope) => [ ...scope !== 'tier-base' ? (inclusionValueValidatorFieldsMap('conditions', type, (options => ({ SelectStateProps: { defaultItemOptions: { // Label: CustomProductsFieldLabel } }, SelectSearchProps: { MultiSelectSearchItemProps: { options: { // Label: CustomProductsFieldLabel } } , customOptionValueResolver: (option) => ({ type: 'product', option }), } })), 'areas', undefined, undefined, { inclusionType: !scope.startsWith('tier') })) : [], scope !== 'tier-subchild' && [ { id: 'checkOn', renderType: FieldType.Tab, title: __('What to check'), description: __('All the selected options need to match.'), //title: locationDepth === 'country' ? __('Countries') : __('States'), fieldOptions: { format: 'booleanDictionary', multiple: true, direction: 'vertical', } }, ], ] }; export const LocationRenderer: CardRenderer = { type, icon: LocationMeta.icon!, prefix: '*', example: scope => undefined, AboveComputedValue: usingTestableOptions(({inclusionType}, {context}) => { if ((['tier-base', 'tier-subchild'] as CardRendererContext[]).includes(context)) { return false } if (inclusionType === 'allowed') { return __('Eligible Locations') } else { return __('Ineligible Locations') } }), ComputedValue: usingTestableOptions(({areas = [], checkOn}, {context, color}) => { const metaData = CouponsPlus.components.conditions[LocationMeta.id] /** * A function so we dont get errors on tier subchild (partials) because they dont have a checkOn properties (individual tiers only support areas, base tiers only support checkOn.) */ const getCheckOnLabel = (): string => { let checkOnLabel: string if (checkOn.billingAddress && checkOn.shippingAddress && checkOn.IP) { checkOnLabel = __('Strict') } else { const checkOnLabels: string[] = [] if (checkOn.billingAddress) { checkOnLabels.push(_checkOnLabel.billingAdress) } if (checkOn.shippingAddress) { checkOnLabels.push(_checkOnLabel.shippingAddress) } if (checkOn.IP) { checkOnLabels.push(_checkOnLabel.ipAddress) } checkOnLabel = checkOnLabels.length === 2 ? checkOnLabels.join(__('* and *').replaceAll('*', '')) : checkOnLabels.join(', ') } return checkOnLabel }; return
{context === 'tier-base' &&

{getCardComponentData('condition', LocationMeta.id).name}

} {context !== 'tier-subchild' &&

{getCheckOnLabel()}

} {context !== 'tier-base' &&
{areas.map((id) => { const selectedProductOption: SelectOption | undefined = fieldsCacheContainer.get('locations').get(id) as SelectOption | undefined if (!selectedProductOption) { return '?' } // will change ts in the future let color = colors.purple return
{selectedProductOption.label}
})} {areas.length === 0 && {__('Select a location')}}
}
; }), }