All files / modules/20-rbac/components/TrialHomePageTemplate StartTrialTemplate.tsx

100% Statements 49/49
69.23% Branches 18/26
100% Functions 5/5
100% Lines 48/48

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              14x 14x 14x 14x 14x 14x 14x 14x               14x 14x 14x 14x 14x 14x 14x 14x                                                   14x 13x 13x 13x     13x 13x 13x 13x 13x 13x 13x 13x     3x         3x 3x   2x   2x         1x         4x 1x   3x       13x 13x                                                 14x           13x   13x   13x         13x           13x   13x                         3x     13x                                  
/*
 * Copyright 2021 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 from 'react'
import { Heading, Layout, Text, Container, Button } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useParams, useHistory, Link } from 'react-router-dom'
import { useToaster } from '@common/components'
import { useStrings } from 'framework/strings'
import { useLicenseStore, handleUpdateLicenseStore } from 'framework/LicenseStore/LicenseStoreContext'
import {
  useStartTrialLicense,
  ResponseModuleLicenseDTO,
  StartTrialDTORequestBody,
  useStartFreeLicense,
  StartFreeLicenseQueryParams
} from 'services/cd-ng'
import type { AccountPathProps, Module } from '@common/interfaces/RouteInterfaces'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { Category, PlanActions, TrialActions } from '@common/constants/TrackingConstants'
import routes from '@common/RouteDefinitions'
import useStartTrialModal from '@common/modals/StartTrial/StartTrialModal'
import { Editions, ModuleLicenseType, SUBSCRIPTION_TAB_NAMES } from '@common/constants/SubscriptionTypes'
import { useFeatureFlags, useFeatureFlag } from '@common/hooks/useFeatureFlag'
import { FeatureFlag } from '@common/featureFlags'
import css from './StartTrialTemplate.module.scss'
 
interface StartTrialTemplateProps {
  title: string
  bgImageUrl: string
  isTrialInProgress?: boolean
  startTrialProps: Omit<StartTrialProps, 'startTrial' | 'module' | 'loading'>
  module: Module
}
 
interface StartTrialProps {
  description: string
  learnMore: {
    description: string
    url: string
  }
  startBtn: {
    description: string
    onClick?: () => void
  }
  shouldShowStartTrialModal?: boolean
  startTrial: () => Promise<ResponseModuleLicenseDTO>
  module: Module
  loading: boolean
}
 
const StartTrialComponent: React.FC<StartTrialProps> = startTrialProps => {
  const { description, learnMore, startBtn, shouldShowStartTrialModal, startTrial, module, loading } = startTrialProps
  const history = useHistory()
  const { accountId } = useParams<{
    accountId: string
  }>()
  const { showError } = useToaster()
  const { getString } = useStrings()
  const { showModal } = useStartTrialModal({ module, handleStartTrial })
  const { licenseInformation, updateLicenseStore } = useLicenseStore()
  const { FREE_PLAN_ENABLED, PLANS_ENABLED } = useFeatureFlags()
  const clickEvent = FREE_PLAN_ENABLED ? PlanActions.StartFreeClick : TrialActions.StartTrialClick
  const experience = FREE_PLAN_ENABLED ? ModuleLicenseType.FREE : ModuleLicenseType.TRIAL
  const modal = FREE_PLAN_ENABLED ? ModuleLicenseType.FREE : ModuleLicenseType.TRIAL
 
  async function handleStartTrial(): Promise<void> {
    trackEvent(clickEvent, {
      category: Category.SIGNUP,
      module,
      edition: FREE_PLAN_ENABLED ? Editions.FREE : Editions.ENTERPRISE
    })
    try {
      const data = await startTrial()
 
      handleUpdateLicenseStore({ ...licenseInformation }, updateLicenseStore, module, data?.data)
 
      history.push({
        pathname: routes.toModuleHome({ accountId, module }),
        search: `?modal=${modal}&&experience=${experience}`
      })
    } catch (error) {
      showError(error.data?.message)
    }
  }
 
  function handleStartButtonClick(): void {
    if (shouldShowStartTrialModal) {
      showModal()
    } else {
      handleStartTrial()
    }
  }
 
  const { trackEvent } = useTelemetry()
  return (
    <Layout.Vertical spacing="small">
      <Text padding={{ bottom: 'xxlarge' }} width={500}>
        {description}
      </Text>
      <a className={css.learnMore} href={learnMore.url} rel="noreferrer" target="_blank">
        {learnMore.description}
      </a>
      <Button
        width={300}
        height={45}
        intent="primary"
        text={startBtn.description}
        onClick={startBtn.onClick ? startBtn.onClick : handleStartButtonClick}
        disabled={loading}
      />
      {PLANS_ENABLED && (
        <Link to={routes.toSubscriptions({ accountId, moduleCard: module, tab: SUBSCRIPTION_TAB_NAMES.PLANS })}>
          {getString('common.exploreAllPlans')}
        </Link>
      )}
    </Layout.Vertical>
  )
}
 
export const StartTrialTemplate: React.FC<StartTrialTemplateProps> = ({
  title,
  bgImageUrl,
  startTrialProps,
  module
}) => {
  const { accountId } = useParams<AccountPathProps>()
 
  const isFreeEnabled = useFeatureFlag(FeatureFlag.FREE_PLAN_ENABLED)
 
  const startTrialRequestBody: StartTrialDTORequestBody = {
    moduleType: module.toUpperCase() as any,
    edition: Editions.ENTERPRISE
  }
 
  const { mutate: startTrial, loading: startingTrial } = useStartTrialLicense({
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  const moduleType = module.toUpperCase() as StartFreeLicenseQueryParams['moduleType']
 
  const { mutate: startFreePlan, loading: startingFree } = useStartFreeLicense({
    queryParams: {
      accountIdentifier: accountId,
      moduleType
    },
    requestOptions: {
      headers: {
        'content-type': 'application/json'
      }
    }
  })
 
  function handleStartTrial(): Promise<ResponseModuleLicenseDTO> {
    return isFreeEnabled ? startFreePlan() : startTrial(startTrialRequestBody)
  }
 
  return (
    <Container className={css.body} style={{ background: `transparent url(${bgImageUrl}) no-repeat` }}>
      <Layout.Vertical spacing="medium">
        <Heading font={{ weight: 'bold', size: 'large' }} color={Color.BLACK_100}>
          {title}
        </Heading>
 
        <StartTrialComponent
          {...startTrialProps}
          startTrial={handleStartTrial}
          module={module}
          loading={startingTrial || startingFree}
        />
      </Layout.Vertical>
    </Container>
  )
}