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

88.64% Statements 39/44
72% Branches 18/25
75% Functions 6/8
90.7% Lines 39/43

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              1x 1x   1x 1x   1x 1x 1x     1x     1x   1x   1x 1x 1x 1x             1x                                                                 1x               11x 11x   11x 11x 11x 11x 11x   11x   11x 11x           9x       9x                       11x             22x 22x   22x               11x 11x     11x       11x         11x                       11x                     1x  
/*
 * 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, useState, useEffect } from 'react'
import cx from 'classnames'
 
import { useParams, useHistory } from 'react-router-dom'
import { Button, Layout } from '@wings-software/uicore'
import type { Editions } from '@common/constants/SubscriptionTypes'
import { SUBSCRIPTION_TAB_NAMES, ModuleLicenseType } from '@common/constants/SubscriptionTypes'
import { useStrings } from 'framework/strings'
import { useQueryParams } from '@common/hooks'
import type { ModuleName } from 'framework/types/ModuleName'
import type { ModuleLicenseDTO } from 'services/cd-ng'
import routes from '@common/RouteDefinitions'
import type { AccountPathProps, Module } from '@common/interfaces/RouteInterfaces'
 
import { useFeatureFlags } from '@common/hooks/useFeatureFlag'
import type { StringsMap } from 'stringTypes'
import { useCommunity } from 'framework/LicenseStore/useCommunity'
 
import SubscriptionOverview from './overview/SubscriptionOverview'
import SubscriptionBanner from './SubscriptionBanner'
import SubscriptionPlans from './plans/SubscriptionPlans'
import css from './SubscriptionsPage.module.scss'
 
export interface SubscriptionTabInfo {
  name: SUBSCRIPTION_TAB_NAMES
  label: keyof StringsMap
}
 
export const SUBSCRIPTION_TABS: SubscriptionTabInfo[] = [
  {
    name: SUBSCRIPTION_TAB_NAMES.OVERVIEW,
    label: 'common.subscriptions.tabs.overview'
  },
  {
    name: SUBSCRIPTION_TAB_NAMES.PLANS,
    label: 'common.subscriptions.tabs.plans'
  }
  // {
  //   name: SUBSCRIPTION_TAB_NAMES.BILLING,
  //   label: 'common.subscriptions.tabs.billing'
  // }
]
 
interface TrialInformation {
  days: number
  expiryDate: string
  isExpired: boolean
  expiredDays: number
  edition: Editions
  isFreeOrCommunity: boolean
}
 
interface SubscriptionTabProps {
  accountName?: string
  trialInfo: TrialInformation
  hasLicense?: boolean
  selectedModule: ModuleName
  licenseData?: ModuleLicenseDTO
  refetchGetLicense: () => void
}
 
const SubscriptionTab = ({
  accountName,
  trialInfo,
  selectedModule,
  hasLicense,
  licenseData,
  refetchGetLicense
}: SubscriptionTabProps): ReactElement => {
  const { PLANS_ENABLED } = useFeatureFlags()
  const isCommunity = useCommunity()
 
  const [selectedSubscriptionTab, setSelectedSubscriptionTab] = useState<SubscriptionTabInfo>(SUBSCRIPTION_TABS[0])
  const { getString } = useStrings()
  const { tab: queryTab } = useQueryParams<{ tab?: SUBSCRIPTION_TAB_NAMES }>()
  const { accountId } = useParams<AccountPathProps>()
  const history = useHistory()
 
  const { isFreeOrCommunity, edition, isExpired, expiredDays, days } = trialInfo
 
  useEffect(() => {
    Iif (queryTab) {
      setSelectedSubscriptionTab(SUBSCRIPTION_TABS.find(tab => tab.name === queryTab) || SUBSCRIPTION_TABS[0])
    }
  }, [queryTab])
 
  function getBanner(): React.ReactElement | null {
    Iif ((!isExpired && licenseData?.licenseType !== ModuleLicenseType.TRIAL && expiredDays > 14) || isFreeOrCommunity) {
      return null
    }
 
    return (
      <SubscriptionBanner
        module={selectedModule}
        edition={edition}
        days={days}
        expiredDays={expiredDays}
        isExpired={isExpired}
      />
    )
  }
 
  function getSubscriptionTabButtons(): React.ReactElement[] {
    const tabs = SUBSCRIPTION_TABS.map(tab => {
      function handleTabClick(): void {
        history.push(
          routes.toSubscriptions({ accountId, moduleCard: selectedModule.toLowerCase() as Module, tab: tab.name })
        )
      }
 
      const isSelected = tab === selectedSubscriptionTab
      const buttonClassnames = cx(css.subscriptionTabButton, isSelected && css.selected)
 
      return (
        <Button className={buttonClassnames} key={tab.label} round onClick={handleTabClick}>
          {getString(tab.label)}
        </Button>
      )
    })
 
    // show Plans tab only when feature flag is on, always show for community edition
    Eif (!isCommunity && !PLANS_ENABLED) {
      tabs.splice(1, 1)
    }
 
    return tabs
  }
 
  function getTabComponent(): React.ReactElement | null {
    switch (selectedSubscriptionTab.name) {
      case SUBSCRIPTION_TAB_NAMES.PLANS:
        return <SubscriptionPlans module={selectedModule} />
      case SUBSCRIPTION_TAB_NAMES.OVERVIEW:
      default:
        return (
          <SubscriptionOverview
            accountName={accountName}
            module={selectedModule}
            licenseData={licenseData}
            trialInformation={trialInfo}
            refetchGetLicense={refetchGetLicense}
          />
        )
    }
  }
 
  return (
    <React.Fragment>
      {hasLicense && getBanner()}
      <Layout.Horizontal className={css.subscriptionTabButtons} spacing="medium">
        {getSubscriptionTabButtons()}
      </Layout.Horizontal>
      {getTabComponent()}
    </React.Fragment>
  )
}
 
export default SubscriptionTab