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

69.39% Statements 34/49
18.18% Branches 6/33
20% Functions 1/5
69.39% Lines 34/49

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              1x   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           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 from 'react'
 
import moment from 'moment'
import { useParams } from 'react-router-dom'
import { Card, Heading, Layout, PageSpinner } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { StringsMap } from 'stringTypes'
import { useStrings } from 'framework/strings'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { useContactSalesMktoModal } from '@common/modals/ContactSales/useContactSalesMktoModal'
import { useLicenseStore, handleUpdateLicenseStore } from 'framework/LicenseStore/LicenseStoreContext'
import { useExtendTrialLicense, StartTrialDTO, FeedbackFormDTO, useSaveFeedback } from 'services/cd-ng'
import { useToaster } from '@common/components'
import type { ModuleName } from 'framework/types/ModuleName'
import type { ModuleLicenseDTO } from 'services/cd-ng'
import {
  useExtendTrialOrFeedbackModal,
  FORM_TYPE,
  FeedbackFormValues
} from '@common/modals/ExtendTrial/useExtendTrialOrFeedbackModal'
import { Editions } from '@common/constants/SubscriptionTypes'
import SubscriptionDetailsCardFooter from './SubscriptionDetailsCardFooter'
import type { TrialInformation } from '../SubscriptionsPage'
import SubscriptionDetailsCardBody from './SubscriptionDetailsCardBody'
import pageCss from '../SubscriptionsPage.module.scss'
 
interface SubscriptionDetailsCardProps {
  accountName?: string
  licenseData?: ModuleLicenseDTO
  module: ModuleName
  trialInformation: TrialInformation
  refetchGetLicense?: () => void
}
 
const SubscriptionDetailsCard: React.FC<SubscriptionDetailsCardProps> = props => {
  const { accountName, module, licenseData, trialInformation, refetchGetLicense } = props
  const { days, expiryDate, isExpired, expiredDays, edition, isFreeOrCommunity } = trialInformation
 
  const { getString } = useStrings()
  const { showError, showSuccess } = useToaster()
  const { currentUserInfo } = useAppStore()
  const { licenseInformation, updateLicenseStore } = useLicenseStore()
  const { accountId } = useParams<AccountPathProps>()
  const { openMarketoContactSales, loading: loadingContactSales } = useContactSalesMktoModal({})
 
  const { mutate: extendTrial, loading: extendingTrial } = useExtendTrialLicense({
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  const { mutate: saveFeedback, loading: sendingFeedback } = useSaveFeedback({
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  const handleFeedbackSubmit = async (values: FeedbackFormValues): Promise<void> => {
    try {
      await saveFeedback({
        accountId,
        moduleType: module as FeedbackFormDTO['moduleType'],
        email: currentUserInfo.email,
        score: Number(values.experience),
        suggestion: values.suggestion
      }).then(() => {
        if (isExpired) {
          refetchGetLicense?.()
        } else {
          closeExtendTrialOrFeedbackModal()
        }
      })
      showSuccess(getString('common.banners.trial.feedbackSuccess'))
    } catch (error) {
      showError(error.data?.message || error.message)
    }
  }
 
  const { openExtendTrialOrFeedbackModal, closeExtendTrialOrFeedbackModal } = useExtendTrialOrFeedbackModal({
    onSubmit: handleFeedbackSubmit,
    onCloseModal: () => {
      if (isExpired) {
        refetchGetLicense?.()
      }
    },
    module,
    expiryDateStr: moment(expiryDate).format('MMMM D YYYY'),
    formType: isExpired ? FORM_TYPE.EXTEND_TRIAL : FORM_TYPE.FEEDBACK,
    moduleDescription: getString(`${module.toLowerCase()}.continuous` as keyof StringsMap),
    loading: sendingFeedback
  })
 
  const handleExtendTrial = async (): Promise<void> => {
    try {
      const data = await extendTrial({
        moduleType: module as StartTrialDTO['moduleType'],
        edition: Editions.ENTERPRISE
      })
      handleUpdateLicenseStore({ ...licenseInformation }, updateLicenseStore, module as any, data?.data)
      openExtendTrialOrFeedbackModal()
    } catch (error) {
      showError(error.data?.message)
    }
  }
 
  const loading = loadingContactSales || extendingTrial || sendingFeedback
 
  Iif (loading) {
    return <PageSpinner />
  }
 
  return (
    <Card className={pageCss.outterCard}>
      <Layout.Vertical spacing="xxlarge" flex={{ alignItems: 'baseline', justifyContent: 'space-between' }}>
        <Heading color={Color.BLACK} font={{ size: 'medium' }}>
          {getString('common.subscriptions.overview.details')}
        </Heading>
        <SubscriptionDetailsCardBody
          licenseData={licenseData}
          edition={edition}
          isFreeOrCommunity={isFreeOrCommunity}
          isExpired={isExpired}
          days={days}
          expiryDate={expiryDate}
          expiredDays={expiredDays}
          accountName={accountName}
        />
        <SubscriptionDetailsCardFooter
          openMarketoContactSales={openMarketoContactSales}
          handleExtendTrial={handleExtendTrial}
          licenseData={licenseData}
          module={module}
          isExpired={isExpired}
          expiredDays={expiredDays}
        />
      </Layout.Vertical>
    </Card>
  )
}
 
export default SubscriptionDetailsCard