import {__, CouponsPlus} from "../../../globals" import {ComponentMeta} from "../../ComponentsMeta" import {iconWithClassName} from "../../IconWithClassName" import {CardRenderer, CardRendererContext} from "../../CardRenderers"; import {usingTestableOptions} from "../CardHelpers" import {invert} from "lodash"; import {ComponentCategory} from "../../components"; import {inclusionValueValidatorFieldsMap} from "../InclusionValueValidator"; import {TierType} from "../../tiers"; import {getCardComponentData} from "../../cards"; export type UserRoleOptions = { roles: string[], inclusionType: 'allowed' | 'forbidden' } const type = CouponsPlus?.components?.conditions?.UserRole?.type || 'UserRole' export const UserRoleMeta: ComponentMeta = { id: type, type: 'conditions', category: ComponentCategory.Account, priority: 1, supportsTiers: true, tierFields: ['roles'], tierType: TierType.Fixed, configurable: context => context !== 'tier-base', // only 'allowed' is allowed in base tier icon: iconWithClassName(({className, context}) => { if (context === 'card') { // solid return } // outline return }), fieldsMap: (d, scope) => inclusionValueValidatorFieldsMap('conditions', type, undefined, 'roles', { options: invert(CouponsPlus?.userRoles) }, undefined, { inclusionType: !scope.startsWith('tier'), }) }; export const UserRoleRenderer: CardRenderer = { type, icon: UserRoleMeta.icon!, prefix: '*', example: __('Gold Members, Silver Members, exclude Subscribers, etc.'), AboveComputedValue: usingTestableOptions(({inclusionType}, {context}) => { if ((['tier-base', 'tier-subchild'] as CardRendererContext[]).includes(context)) { return false } if (inclusionType === 'allowed') { return __('Eligible User Roles') } else { return __('Ineligible User Roles') } }), ComputedValue: usingTestableOptions(({roles}, {context}) => { if (context === 'tier-base') { return getCardComponentData('condition', UserRoleMeta.id).name } if (!roles || roles.length === 0) { return {__('Select a role')} } const metaData = CouponsPlus.components.conditions[UserRoleMeta.id] return
{roles.map(role => { return
{CouponsPlus?.userRoles[role] || role}
; })}
; }), }