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

68.29% Statements 28/41
69.23% Branches 18/26
8.33% Functions 1/12
67.5% Lines 27/40

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              1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x 1x   1x 1x           1x 1x 1x           1x                 1x 1x   1x                 1x                   1x                           1x               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 from 'react'
import { useParams } from 'react-router-dom'
import { Container, FlexExpander, Layout, Text, PageError } from '@wings-software/uicore'
import { SelectSegmentsModalButton } from '@cf/components/SelectSegmentsModalButton/SelectSegmentsModalButton'
import { ContainerSpinner } from '@common/components/ContainerSpinner/ContainerSpinner'
import StringWithTooltip from '@common/components/StringWithTooltip/StringWithTooltip'
import { getErrorMessage } from '@cf/utils/CFUtils'
import { NoDataFoundRow } from '@cf/components/NoDataFoundRow/NoDataFoundRow'
import { useStrings } from 'framework/strings'
import { useAddTargetsToExcludeList, useAddTargetsToIncludeList } from '@cf/utils/SegmentUtils'
import {
  GetTargetSegmentsQueryParams,
  Segment,
  Target,
  /* TargetDetailSegment, */ useGetTargetSegments
} from 'services/cf'
// import { ItemBriefInfo } from '@cf/components/ItemBriefInfo/ItemBriefInfo'
import useActiveEnvironment from '@cf/hooks/useActiveEnvironment'
import { IncludeSegmentRow } from './IncludeSegmentRow'
import { ExcludeSegmentRow } from './ExcludeSegmentRow'
 
export const TabSegments: React.FC<{ target?: Target | null }> = ({ target }) => {
  const { getString } = useStrings()
  const {
    accountId: accountIdentifier,
    orgIdentifier,
    projectIdentifier,
    targetIdentifier
  } = useParams<Record<string, string>>()
  const { activeEnvironment: environmentIdentifier } = useActiveEnvironment()
  const patchParams = {
    accountIdentifier,
    orgIdentifier,
    projectIdentifier,
    environmentIdentifier
  }
  const { loading, error, data, refetch } = useGetTargetSegments({
    identifier: targetIdentifier,
    queryParams: {
      accountIdentifier,
      orgIdentifier,
      projectIdentifier,
      environmentIdentifier
    } as GetTargetSegmentsQueryParams
  })
  const _useAddTargetsToIncludeList = useAddTargetsToIncludeList(patchParams)
  const _useAddTargetsToExcludeList = useAddTargetsToExcludeList(patchParams)
 
  const addTargetToSegments = async (segments: Segment[]) => {
    // Note: Due to https://harness.atlassian.net/browse/FFM-603 not done, we make
    // multiple patch APIs instead of one
    return await Promise.all(
      segments.map(segment => _useAddTargetsToIncludeList(segment.identifier, [targetIdentifier]))
    ).then(() => {
      refetch()
    })
  }
  const excludeTargetFromSegments = async (segments: Segment[]): Promise<void> => {
    // Note: Due to https://harness.atlassian.net/browse/FFM-603 not done, we make
    // multiple patch APIs instead of one
    return await Promise.all(
      segments.map(segment => _useAddTargetsToExcludeList(segment.identifier, [targetIdentifier]))
    ).then(() => {
      refetch()
    })
  }
 
  Iif (error) {
    return (
      <Container padding={{ top: 'huge' }}>
        <PageError
          message={getErrorMessage(error)}
          width={450}
          onClick={() => {
            refetch()
          }}
        />
      </Container>
    )
  }
 
  Iif (loading) {
    return (
      <Container height="100%" width="100%" padding={{ top: 'huge' }}>
        <ContainerSpinner />
      </Container>
    )
  }
 
  return (
    <>
      {/* MANUALLY ADDED / INCLUDE LIST */}
      <Container padding={{ top: 'xsmall', right: 'xxlarge', left: 'xxlarge', bottom: 'xxlarge' }}>
        <Layout.Horizontal style={{ alignItems: 'center' }}>
          <Text style={{ color: '#4F5162', fontSize: '10px', fontWeight: 'bold' }} padding={{ left: 'small' }}>
            <StringWithTooltip
              stringId="cf.targetDetail.manuallyAdded"
              tooltipId="ff_targetTargetGroups_manuallyAdded"
            />
          </Text>
          <FlexExpander />
          <SelectSegmentsModalButton
            text={getString('cf.targetDetail.addToSegment')}
            minimal
            intent="primary"
            accountIdentifier={accountIdentifier}
            orgIdentifier={orgIdentifier}
            projectIdentifier={projectIdentifier}
            environmentIdentifier={environmentIdentifier}
            targetIdentifier={targetIdentifier}
            modalTitle={getString('cf.targetDetail.addTargetToSegment')}
            onSubmit={addTargetToSegments}
            style={{ paddingRight: 'var(--spacing-small)' }}
          />
        </Layout.Horizontal>
        {(!!data?.includedSegments?.length && (
          <Layout.Vertical spacing="small">
            {data.includedSegments.map(segment => (
              <IncludeSegmentRow
                key={segment.identifier}
                target={target}
                segment={segment}
                patchParams={patchParams}
                refetch={() => refetch()}
              />
            ))}
          </Layout.Vertical>
        )) || <NoDataFoundRow message={getString('cf.targetDetail.noSegmentAdded')} />}
      </Container>
 
      {/* AUTOMATICALLY ADDED THROUGH CONDITIONS */}
      {/* <Container padding={{ top: 'xsmall', right: 'xxlarge', left: 'xxlarge', bottom: 'xxlarge' }}>
        <Layout.Horizontal style={{ alignItems: 'center' }}>
          <Text
            style={{ color: '#4F5162', fontSize: '10px', fontWeight: 'bold' }}
            padding={{ left: 'small', bottom: 'small' }}
            data-tooltip-id="ff_targetTargetGroups_autoAdded"
          >
            {getString('cf.targetDetail.autoAdded')}
          </Text>
          <FlexExpander />
        </Layout.Horizontal>
 
        {(!!data?.ruleSegments?.length && (
          <Layout.Vertical spacing="small">
            {data.ruleSegments.map(segment => {
              const { identifier, name, description } = segment as TargetDetailSegment & { description: string }
              return <ItemBriefInfo key={identifier} name={name as string} description={description} />
            })}
          </Layout.Vertical>
        )) || <NoDataFoundRow message={getString('cf.targetDetail.noSegmentMatched')} />}
      </Container> */}
 
      {/* EXCLUSION LIST */}
      <Container padding={{ top: 'xsmall', right: 'xxlarge', left: 'xxlarge', bottom: 'xxlarge' }}>
        <Layout.Horizontal style={{ alignItems: 'center' }}>
          <Text style={{ color: '#4F5162', fontSize: '10px', fontWeight: 'bold' }} padding={{ left: 'small' }}>
            <StringWithTooltip
              stringId="cf.targetDetail.exclusionList"
              tooltipId="ff_targetTargetGroups_exclusionList"
            />
          </Text>
          <FlexExpander />
          <SelectSegmentsModalButton
            text={getString('cf.targetDetail.excludefromSegment')}
            minimal
            intent="primary"
            accountIdentifier={accountIdentifier}
            orgIdentifier={orgIdentifier}
            projectIdentifier={projectIdentifier}
            environmentIdentifier={environmentIdentifier}
            targetIdentifier={targetIdentifier}
            modalTitle={getString('cf.targetDetail.excludeTargetFromSegment')}
            submitButtonTitle={getString('cf.targetDetail.exclude')}
            onSubmit={excludeTargetFromSegments}
            style={{ paddingRight: 'var(--spacing-small)' }}
          />
        </Layout.Horizontal>
        {(!!data?.excludedSegments?.length && (
          <Layout.Vertical spacing="small">
            {data.excludedSegments.map(segment => (
              <ExcludeSegmentRow
                key={segment.identifier}
                target={target}
                segment={segment}
                patchParams={patchParams}
                refetch={() => refetch()}
              />
            ))}
          </Layout.Vertical>
        )) || <NoDataFoundRow message={getString('cf.targetDetail.noSegmentExcluded')} />}
      </Container>
    </>
  )
}