All files / modules/75-cf/pages/target-details TargetDetailPage.tsx

90.91% Statements 50/55
80.43% Branches 37/46
60% Functions 3/5
90.74% Lines 49/54

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              1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x 7x 7x           7x 7x           7x                   7x 7x                       7x 7x                 7x                 7x         1x   1x 1x   1x                 1x                     7x   7x   7x 1x 1x                   6x   1x               1x 1x           5x                                                                
/*
 * 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 { useHistory, useParams } from 'react-router-dom'
import { Container, Layout, PageError } from '@wings-software/uicore'
import { Intent } from '@harness/design-system'
import { String, useStrings } from 'framework/strings'
import { DeleteTargetQueryParams, GetTargetQueryParams, Target, useDeleteTarget, useGetTarget } from 'services/cf'
import routes from '@common/RouteDefinitions'
import { ContainerSpinner } from '@common/components/ContainerSpinner/ContainerSpinner'
import { PageSpinner, useToaster } from '@common/components'
import { formatDate, formatTime, getErrorMessage, showToaster } from '@cf/utils/CFUtils'
import { useConfirmAction } from '@common/hooks'
import { useDocumentTitle } from '@common/hooks/useDocumentTitle'
import { useGetEnvironment } from 'services/cd-ng'
import { DetailPageTemplate } from '@cf/components/DetailPageTemplate/DetailPageTemplate'
import useActiveEnvironment from '@cf/hooks/useActiveEnvironment'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import TargetManagementToolbar from '@cf/components/TargetManagementToolbar/TargetManagementToolbar'
import { useGitSync } from '@cf/hooks/useGitSync'
import { TargetSettings } from './target-settings/TargetSettings'
import { FlagSettings } from './flag-settings/FlagSettings'
 
import css from './TargetDetailPage.module.scss'
 
export const TargetDetailPage: React.FC = () => {
  const { getString } = useStrings()
  const { showError, clear } = useToaster()
  const {
    accountId: accountIdentifier,
    orgIdentifier,
    projectIdentifier,
    targetIdentifier
  } = useParams<Record<string, string>>()
  const { activeEnvironment: environmentIdentifier, withActiveEnvironment } = useActiveEnvironment()
  const {
    data: target,
    loading,
    refetch,
    error
  } = useGetTarget({
    identifier: targetIdentifier,
    queryParams: {
      accountIdentifier,
      orgIdentifier,
      projectIdentifier,
      environmentIdentifier
    } as GetTargetQueryParams
  })
 
  const label = `${getString('cf.shared.targetManagement')}: ${getString('pipeline.targets.title')}`
  const breadcrumbs = [
    {
      label,
      url: withActiveEnvironment(
        routes.toCFTargets({
          accountId: accountIdentifier,
          orgIdentifier,
          projectIdentifier
        })
      )
    }
  ]
  const history = useHistory()
  const { mutate: deleteTarget } = useDeleteTarget({
    queryParams: {
      projectIdentifier,
      environmentIdentifier: target?.environment as string,
      accountIdentifier,
      orgIdentifier
    } as DeleteTargetQueryParams
  })
 
  const { data: environment } = useGetEnvironment({
    environmentIdentifier,
    queryParams: {
      accountId: accountIdentifier,
      projectIdentifier,
      orgIdentifier
    }
  })
 
  const deleteTargetConfirm = useConfirmAction({
    title: getString('cf.targets.deleteTarget'),
    message: <String stringID="cf.targets.deleteTargetMessage" vars={{ name: target?.name }} />,
    intent: Intent.DANGER,
    action: async () => {
      clear()
 
      try {
        deleteTarget(target?.identifier as string)
          .then(() => {
            history.push(
              withActiveEnvironment(
                routes.toCFTargets({
                  projectIdentifier,
                  orgIdentifier,
                  accountId: accountIdentifier
                })
              )
            )
            showToaster(getString('cf.messages.targetDeleted'))
          })
          .catch(_error => {
            showError(getErrorMessage(_error), 0, 'cf.delete.target.error')
          })
      } catch (_error) {
        showError(getErrorMessage(_error), 0, 'cf.delete.target.error')
      }
    }
  })
 
  const gitSync = useGitSync()
 
  useDocumentTitle(label)
 
  if (loading) {
    Eif (!target) {
      return <PageSpinner />
    }
 
    return (
      <Container className={css.fullSizeContentStyle}>
        <ContainerSpinner />
      </Container>
    )
  }
 
  if (error) {
    const ErrorComponent = (
      <PageError
        message={getErrorMessage(error)}
        onClick={() => {
          refetch()
        }}
      />
    )
 
    Eif (!target) {
      return ErrorComponent
    }
 
    return <Container className={css.fullSizeContentStyle}>{ErrorComponent}</Container>
  }
 
  return (
    <DetailPageTemplate
      menuItems={[
        {
          icon: 'cross',
          text: getString('delete'),
          onClick: deleteTargetConfirm,
          permission: {
            resource: { resourceType: ResourceType.ENVIRONMENT, resourceIdentifier: environmentIdentifier },
            permission: PermissionIdentifier.DELETE_FF_TARGETGROUP
          }
        }
      ]}
      breadcrumbs={breadcrumbs}
      title={target?.name}
      subTitle={getString('cf.targetDetail.createdOnDate', {
        date: formatDate(target?.createdAt as number),
        time: formatTime(target?.createdAt as number)
      })}
      identifier={target?.identifier}
      metaData={{ environment: environment?.data?.name as string }}
    >
      <Layout.Vertical height="100%" className={css.gitSyncContainer}>
        {gitSync.isGitSyncActionsEnabled && <TargetManagementToolbar gitSync={gitSync} />}
        <Layout.Horizontal height="100%">
          <TargetSettings target={target as Target} />
          <FlagSettings target={target} gitSync={gitSync} />
        </Layout.Horizontal>
      </Layout.Vertical>
    </DetailPageTemplate>
  )
}