All files / modules/75-ce/pages/home CETrialHomePage.tsx

88.46% Statements 46/52
61.11% Branches 22/36
62.5% Functions 5/8
88.46% Lines 46/52

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              1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 2x   2x 2x 2x 2x 2x 2x 2x   2x                 2x           2x                         2x     2x     2x               2x     1x     2x 1x 1x   1x   1x           1x 1x           2x       2x                       2x 1x       2x                   1x  
/*
 * 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, { useEffect } from 'react'
import { useHistory, useParams } from 'react-router-dom'
import { pick } from 'lodash-es'
import { useStrings } from 'framework/strings'
import routes from '@common/RouteDefinitions'
import { StartTrialTemplate } from '@rbac/components/TrialHomePageTemplate/StartTrialTemplate'
import { useStartTrialLicense, useStartFreeLicense, ResponseModuleLicenseDTO } from 'services/cd-ng'
import useCreateConnector from '@ce/components/CreateConnector/CreateConnector'
import useCETrialModal from '@ce/modals/CETrialModal/useCETrialModal'
import { useToaster } from '@common/components'
import { handleUpdateLicenseStore, useLicenseStore } from 'framework/LicenseStore/LicenseStoreContext'
import type { AccountPathProps, Module } from '@common/interfaces/RouteInterfaces'
import { Editions, ModuleLicenseType } from '@common/constants/SubscriptionTypes'
import { useFeatureFlag } from '@common/hooks/useFeatureFlag'
import { FeatureFlag } from '@common/featureFlags'
import { useQueryParams } from '@common/hooks'
import bgImage from './images/cehomebg.svg'
 
const CETrialHomePage: React.FC = () => {
  const { getString } = useStrings()
 
  const { accountId } = useParams<AccountPathProps>()
  const history = useHistory()
  const { licenseInformation, updateLicenseStore } = useLicenseStore()
  const { experience } = useQueryParams<{ experience?: ModuleLicenseType }>()
  const isFreeEnabled = useFeatureFlag(FeatureFlag.FREE_PLAN_ENABLED)
  const module = 'ce'
  const moduleType = 'CE'
 
  const { openModal } = useCreateConnector({
    onSuccess: () => {
      history.push(routes.toCEOverview({ accountId }))
    },
    onClose: () => {
      history.push(routes.toCEOverview({ accountId }))
    }
  })
 
  const { mutate: startTrial } = useStartTrialLicense({
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  const { mutate: startFreePlan } = useStartFreeLicense({
    queryParams: {
      accountIdentifier: accountId,
      moduleType
    },
    requestOptions: {
      headers: {
        'content-type': 'application/json'
      }
    }
  })
 
  function getExperience(): ModuleLicenseType {
    Iif (experience) {
      return experience
    }
    return isFreeEnabled ? ModuleLicenseType.FREE : ModuleLicenseType.TRIAL
  }
 
  const { showModal, hideModal } = useCETrialModal({
    onContinue: () => {
      hideModal()
      openModal()
    },
    experience: getExperience()
  })
 
  const { showError } = useToaster()
 
  function startPlan(): Promise<ResponseModuleLicenseDTO> {
    return isFreeEnabled ? startFreePlan() : startTrial({ moduleType, edition: Editions.ENTERPRISE })
  }
 
  const handleStartTrial = async (): Promise<void> => {
    try {
      const data = await startPlan()
 
      const expiryTime = data?.data?.expiryTime
 
      const updatedLicenseInfo = data?.data && {
        ...licenseInformation?.[moduleType],
        ...pick(data?.data, ['licenseType', 'edition']),
        expiryTime
      }
 
      handleUpdateLicenseStore({ ...licenseInformation }, updateLicenseStore, module as Module, updatedLicenseInfo)
      showModal()
    } catch (error) {
      showError(error.data?.message)
    }
  }
 
  const startBtnDescription = isFreeEnabled
    ? getString('common.startFreePlan', { module: 'CCM' })
    : getString('ce.ceTrialHomePage.startTrial.startBtn.description')
 
  const startTrialProps = {
    description: getString('ce.homepage.slogan'),
    learnMore: {
      description: getString('ce.learnMore'),
      url: 'https://ngdocs.harness.io/article/dvspc6ub0v-create-cost-perspectives'
    },
    startBtn: {
      description: startBtnDescription,
      onClick: handleStartTrial
    }
  }
 
  useEffect(() => {
    experience && showModal()
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [experience])
 
  return (
    <StartTrialTemplate
      title={getString('common.purpose.ce.continuous')}
      bgImageUrl={bgImage}
      startTrialProps={startTrialProps}
      module={module}
    />
  )
}
 
export default CETrialHomePage