All files / modules/33-auth-settings/pages/subscriptions/plans planUtils.tsx

64.47% Statements 49/76
59.7% Branches 80/134
81.82% Functions 9/11
65.75% Lines 48/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276              2x 2x 2x 2x 2x 2x           2x   2x 2x 2x                                                       2x                   18x 18x 18x   18x 18x               18x     18x     18x 18x 18x                                                                   18x       18x   18x     2x                                 2x 18x     18x 18x 18x 18x     18x                                       18x             18x                 2x 18x 18x   18x   18x 4x 4x                                                                       14x                                     2x 18x 18x 18x 1x                             17x            
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { ReactElement } from 'react'
import { isEmpty } from 'lodash-es'
import cx from 'classnames'
import { Color } from '@harness/design-system'
import { Layout, Text, Button, ButtonVariation, Popover } from '@wings-software/uicore'
import { Position, PopoverInteractionKind, Classes } from '@blueprintjs/core'
import type { Editions } from '@common/constants/SubscriptionTypes'
import type { EditionActionDTO } from 'services/cd-ng'
import type { StringsMap } from 'stringTypes'
import type { PlansFragment, Maybe } from 'services/common/services'
import type { PlanCalculatedProps, BtnProps } from './PlanContainer'
import css from './Plan.module.scss'
 
export enum TIME_TYPE {
  YEARLY = 'yearly',
  MONTHLY = 'monthly'
}
 
export type PlanProp = Maybe<{ __typename?: 'ComponentPricingPagePlansZone' } & PlansFragment>
 
export interface PlanData {
  planProps: PlanProp
  btnProps?: BtnProps[]
  currentPlanProps?: {
    isCurrentPlan?: boolean
    isTrial?: boolean
    isPaid?: boolean
  }
}
 
interface GetBtnPropsProps {
  plan: PlanProp
  getString: (key: keyof StringsMap, vars?: Record<string, any> | undefined) => string
  handleStartPlan: (planEdition: Editions) => Promise<void>
  handleContactSales: () => void
  handleExtendTrial: (edition: Editions) => Promise<void>
  handleManageSubscription: () => void
  btnLoading: boolean
  actions?: {
    [key: string]: EditionActionDTO[]
  }
}
 
export function getBtnProps({
  plan,
  getString,
  handleStartPlan,
  handleContactSales,
  handleExtendTrial,
  handleManageSubscription,
  btnLoading,
  actions
}: GetBtnPropsProps): PlanCalculatedProps['btnProps'] {
  const btnProps: BtnProps[] = []
  const planEdition = plan?.title && (plan?.title?.toUpperCase() as Editions)
  const planActions = (planEdition && actions?.[planEdition]) || []
  // for March's launch, we hide manage subscription, upgrade, subscribe until the functions are fullfilled
  planActions
    ?.filter(action => action.action && !['MANAGE', 'SUBSCRIBE', 'UPGRADE'].includes(action.action))
    .forEach(action => {
      let onClick,
        order,
        planDisabledStr: string | undefined,
        isContactSales: boolean | undefined,
        isContactSupport: boolean | undefined
      const buttonText =
        action.action &&
        PLAN_BTN_ACTIONS[action.action] &&
        getString(PLAN_BTN_ACTIONS[action.action] as keyof StringsMap)
      switch (action.action) {
        case 'START_FREE':
        case 'START_TRIAL':
          order = 0
          onClick = () => planEdition && handleStartPlan(planEdition)
          break
        case 'EXTEND_TRIAL':
          order = 0
          onClick = () => planEdition && handleExtendTrial(planEdition)
          break
        case 'MANAGE':
          order = 0
          onClick = handleManageSubscription
          break
        case 'SUBSCRIBE':
        case 'UPGRADE':
          order = 1
          onClick = undefined
          break
        case 'CONTACT_SALES':
          order = 2
          onClick = handleContactSales
          isContactSales = true
          break
        case 'CONTACT_SUPPORT':
          order = 2
          isContactSupport = true
          break
        case 'DISABLED_BY_ENTERPRISE':
        case 'DISABLED_BY_TEAM':
          order = 0
          onClick = undefined
          planDisabledStr = action.reason
          break
        default:
          order = 0
          onClick = undefined
      }
 
      btnProps.push({ buttonText, onClick, btnLoading, order, planDisabledStr, isContactSales, isContactSupport })
    })
 
  // sort btns for display order
  btnProps.sort((btn1, btn2) => btn1.order - btn2.order)
 
  return btnProps
}
 
export const PLAN_BTN_ACTIONS: { [key in NonNullable<EditionActionDTO['action']>]?: string } = {
  START_FREE: 'common.startFree',
  START_TRIAL: 'common.start14dayTrial',
  EXTEND_TRIAL: 'common.banners.trial.expired.extendTrial',
  SUBSCRIBE: 'common.subscriptions.overview.subscribe',
  UPGRADE: 'common.upgrade',
  CONTACT_SALES: 'common.banners.trial.contactSales',
  CONTACT_SUPPORT: 'common.contactSupport',
  MANAGE: 'common.plans.manageSubscription'
}
 
interface GetBtnsProps {
  isPlanDisabled: boolean
  btnProps?: BtnProps[]
  getString: (key: keyof StringsMap, vars?: Record<string, any> | undefined) => string
}
 
export function getBtns({ isPlanDisabled, btnProps, getString }: GetBtnsProps): ReactElement {
  Iif (isPlanDisabled) {
    return <></>
  }
  const btns: ReactElement[] = []
  const length = btnProps?.length || 0
  btnProps?.forEach(btn => {
    const { onClick, btnLoading, buttonText, isContactSales, isContactSupport } = btn
 
    // contact sales|support displays as link when there are other buttons
    Iif ((isContactSales || isContactSupport) && length > 1) {
      btns.push(
        <Layout.Horizontal spacing="small" flex={{ alignItems: 'baseline', justifyContent: 'center' }}>
          <Text>{getString('common.or')}</Text>
          <Button
            font={{ size: 'small' }}
            key={buttonText}
            onClick={onClick}
            loading={btnLoading}
            variation={ButtonVariation.LINK}
            className={css.noPadding}
          >
            {buttonText}
          </Button>
        </Layout.Horizontal>
      )
      return
    }
 
    // or else, just a button
    btns.push(
      <Button key={buttonText} onClick={onClick} loading={btnLoading} variation={ButtonVariation.PRIMARY}>
        {buttonText}
      </Button>
    )
  })
 
  return <Layout.Vertical spacing={'small'}>{btns}</Layout.Vertical>
}
 
interface GetPriceTipsProps {
  timeType: TIME_TYPE
  plan: PlanData
  textColorClassName: string
}
 
export function getPriceTips({ timeType, plan, textColorClassName }: GetPriceTipsProps): React.ReactElement {
  const priceTips = timeType === TIME_TYPE.MONTHLY ? plan.planProps?.priceTips : plan.planProps?.yearlyPriceTips
  const priceTerm = timeType === TIME_TYPE.MONTHLY ? plan.planProps?.priceTerm : plan.planProps?.yearlyPriceTerm
  const priceTermTips =
    timeType === TIME_TYPE.MONTHLY ? plan.planProps?.priceTermTips : plan.planProps?.yearlyPriceTermTips
 
  if (!isEmpty(priceTerm) && !isEmpty(priceTermTips)) {
    const tips = priceTips?.split(priceTerm || '')
    return (
      <Layout.Horizontal spacing="xsmall" flex={{ alignItems: 'baseline' }}>
        <Text
          color={Color.BLACK}
          font={{ weight: 'light', size: 'small' }}
          padding={{ left: 'large' }}
          className={css.centerText}
        >
          {tips?.[0]}
        </Text>
        <Popover
          popoverClassName={Classes.DARK}
          position={Position.BOTTOM}
          interactionKind={PopoverInteractionKind.HOVER}
          content={
            <Text width={200} padding="medium" color={Color.WHITE}>
              {priceTermTips || ''}
            </Text>
          }
        >
          <Text font={{ weight: 'light', size: 'small' }} className={cx(css.centerText, textColorClassName)}>
            {priceTerm}
          </Text>
        </Popover>
        <Text
          color={Color.BLACK}
          font={{ weight: 'light', size: 'small' }}
          padding={{ right: 'large' }}
          className={css.centerText}
        >
          {tips?.[1]}
        </Text>
      </Layout.Horizontal>
    )
  }
 
  return (
    <Text
      color={Color.BLACK}
      font={{ weight: 'light', size: 'small' }}
      padding={{ left: 'large', right: 'large' }}
      className={css.centerText}
    >
      {priceTips}
    </Text>
  )
}
 
interface GetPriceProps {
  plan: PlanData
  timeType: TIME_TYPE
  openMarketoContactSales: () => void
  getString: (key: keyof StringsMap, vars?: Record<string, any> | undefined) => string
}
 
export function getPrice({ timeType, plan, openMarketoContactSales, getString }: GetPriceProps): React.ReactElement {
  const CUSTOM_PRICING = 'custom pricing'
  const price = timeType === TIME_TYPE.MONTHLY ? plan.planProps?.price : plan?.planProps?.yearlyPrice
  if (price?.toLowerCase() === CUSTOM_PRICING) {
    return (
      <Layout.Horizontal spacing="xsmall" flex={{ alignItems: 'baseline' }}>
        <Button
          onClick={openMarketoContactSales}
          variation={ButtonVariation.LINK}
          className={cx(css.noPadding, css.fontLarge)}
        >
          {getString('common.banners.trial.contactSales')}
        </Button>
        <Text color={Color.BLACK} font={{ size: 'medium' }}>
          {getString('common.customPricing')}
        </Text>
      </Layout.Horizontal>
    )
  }
  return (
    <Text font={{ weight: 'semi-bold', size: 'large' }} color={Color.BLACK}>
      {price}
    </Text>
  )
}