All files / modules/75-ce/components/PerspectiveViews PerspectiveGridView.tsx

86.84% Statements 33/38
66.67% Branches 58/87
63.64% Functions 7/11
86.84% Lines 33/38

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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231              2x 2x 2x                   2x 2x 2x 2x 2x 2x 2x 2x                           2x           28x 28x     28x   28x                                       28x   28x                 28x       28x       28x 1x 1x       28x 28x   28x           1x                                 1x                                                                                                                         28x                                           2x           7x     28x                           2x  
/*
 * 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 { useHistory, useParams } from 'react-router-dom'
import {
  Icon,
  Text,
  Layout,
  Container,
  Card,
  FlexExpander,
  CardBody,
  useConfirmationDialog
} from '@wings-software/uicore'
import { Menu, Classes, Intent } from '@blueprintjs/core'
import { defaultTo } from 'lodash-es'
import routes from '@common/RouteDefinitions'
import { QlceView, ViewType, ViewState } from 'services/ce/services'
import { perspectiveDateLabelToDisplayText, SOURCE_ICON_MAPPING } from '@ce/utils/perspectiveUtils'
import formatCost from '@ce/utils/formatCost'
import { useStrings } from 'framework/strings'
import css from './PerspectiveGridView.module.scss'
 
interface PerspectiveGridCardProps {
  data: QlceView
  navigateToPerspectiveDetailsPage: (
    perspectiveId: string,
    viewState: ViewState,
    name: string,
    viewType: ViewType
  ) => void
  deletePerpsective: (perspectiveId: string, perspectiveName: string) => void
  clonePerspective: (perspectiveId: string, perspectiveName: string) => void
}
 
const PerpsectiveGridCard: (props: PerspectiveGridCardProps) => JSX.Element | null = ({
  data,
  navigateToPerspectiveDetailsPage,
  deletePerpsective,
  clonePerspective
}) => {
  const history = useHistory()
  const { accountId } = useParams<{
    accountId: string
  }>()
  const { getString } = useStrings()
 
  const { openDialog } = useConfirmationDialog({
    contentText: (
      <Text>
        {getString('ce.perspectives.confirmDeletePerspectiveMsg', {
          name: data.name
        })}
      </Text>
    ),
    titleText: getString('ce.perspectives.confirmDeletePerspectiveTitle'),
    confirmButtonText: getString('delete'),
    cancelButtonText: getString('cancel'),
    intent: Intent.DANGER,
    buttonIntent: Intent.DANGER,
    onCloseDialog: async (isConfirmed: boolean) => {
      if (isConfirmed) {
        data.id && deletePerpsective(data.id, defaultTo(data.name, ''))
      }
    }
  })
 
  const dateLabelToDisplayText = perspectiveDateLabelToDisplayText(getString)
 
  const onEditClick: (perspectiveId: string) => void = perspectiveId => {
    history.push(
      routes.toCECreatePerspective({
        accountId: accountId,
        perspectiveId: perspectiveId
      })
    )
  }
 
  const editClick: () => void = () => {
    data?.id && onEditClick(data.id)
  }
 
  const onDeleteClick: () => void = () => {
    openDialog()
  }
 
  const onCloneClick: () => void = () => {
    Eif (data?.id && data?.name) {
      clonePerspective(data.id, data.name)
    }
  }
 
  const viewType = data?.viewType
  const isDefaultPerspective = viewType === ViewType.Default
 
  return data ? (
    <Card
      key={data?.id}
      interactive
      className={css.cardClass}
      onClick={() => {
        data.id &&
          data.viewState &&
          navigateToPerspectiveDetailsPage(
            data?.id,
            data.viewState,
            defaultTo(data?.name, data.id),
            defaultTo(data.viewType, ViewType.Customer)
          )
      }}
    >
      <CardBody.Menu
        menuPopoverProps={{
          className: Classes.DARK
        }}
        menuContent={
          <Container
            onClick={e => {
              e.stopPropagation()
            }}
          >
            <Menu>
              <Menu.Item disabled={isDefaultPerspective} onClick={editClick} icon="edit" text="Edit" />
              <Menu.Item
                onClick={onCloneClick}
                icon="duplicate"
                text="Clone"
                data-testid={`clone-perspective-${data?.id}`}
              />
              <Menu.Item
                disabled={isDefaultPerspective}
                className={Classes.POPOVER_DISMISS}
                onClick={onDeleteClick}
                icon="trash"
                text="Delete"
              />
            </Menu>
          </Container>
        }
      />
 
      {data.viewState === ViewState.Draft && (
        <div className={css.ribbonTopLeft}>
          <span>Draft</span>
        </div>
      )}
      <Layout.Vertical spacing="small" className={css.cardContent}>
        {isDefaultPerspective && <Container className={css.sampleRibbon}></Container>}
 
        <Container height={23}>{isDefaultPerspective && <Icon name="harness" size={22} />}</Container>
        <Text font={{ weight: 'semi-bold' }} color="grey800" lineClamp={2}>
          {data?.name}
        </Text>
        {data && !isNaN(data?.totalCost) ? <Text color="grey800">{formatCost(data?.totalCost)}</Text> : null}
        {data?.timeRange ? (
          <Text font="small" color="grey400">
            {dateLabelToDisplayText[data.timeRange] || getString('common.repo_provider.customLabel')}
          </Text>
        ) : null}
        <FlexExpander />
        {data?.dataSources && data.dataSources.length ? (
          <Container padding={{ top: 'large' }}>
            <Text
              font="xsmall"
              color="grey400"
              margin={{
                bottom: 'small'
              }}
            >
              Data Sources
            </Text>
 
            <Layout.Horizontal
              spacing="small"
              style={{
                alignItems: 'center'
              }}
            >
              {data.dataSources.map(source =>
                source ? <Icon key={source} size={22} name={SOURCE_ICON_MAPPING[source]} /> : null
              )}
            </Layout.Horizontal>
          </Container>
        ) : null}
      </Layout.Vertical>
    </Card>
  ) : null
}
 
interface PerspectiveListViewProps {
  pespectiveData: QlceView[]
  navigateToPerspectiveDetailsPage: (
    perspectiveId: string,
    viewState: ViewState,
    name: string,
    viewType: ViewType
  ) => void
  deletePerpsective: (perspectiveId: string, perspectiveName: string) => void
  clonePerspective: (perspectiveId: string, perspectiveName: string) => void
}
 
const PerspectiveListView: React.FC<PerspectiveListViewProps> = ({
  pespectiveData,
  navigateToPerspectiveDetailsPage,
  deletePerpsective,
  clonePerspective
}) => {
  return (
    <Container className={css.mainGridContainer}>
      {pespectiveData.map(data => {
        return (
          <PerpsectiveGridCard
            navigateToPerspectiveDetailsPage={navigateToPerspectiveDetailsPage}
            clonePerspective={clonePerspective}
            deletePerpsective={deletePerpsective}
            key={data.id}
            data={data}
          />
        )
      })}
    </Container>
  )
}
 
export default PerspectiveListView