All files / modules/75-ce/components/PerspectiveExplorerGroupBy GroupByView.tsx

88.68% Statements 47/53
75% Branches 24/32
78.95% Functions 15/19
86.96% Lines 40/46

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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263              2x 2x 2x 2x 2x 2x               2x                           2x           1x   5x   1x       5x               5x                                                 2x 4x 4x                                                                     2x 6x 1x   6x 1x                           6x               6x                               2x 4x                                                     2x 8x                                                   2x             4x 4x 12x   4x 12x     4x         8x                     12x 12x 4x   8x                   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, { useState } from 'react'
import { TextInput, Button } from '@wings-software/uicore'
import { Popover, Position, PopoverInteractionKind, Classes, MenuItem } from '@blueprintjs/core'
import cx from 'classnames'
import { useStrings } from 'framework/strings'
import {
  ViewFieldIdentifier,
  QlceViewFieldInputInput,
  Maybe,
  QlceViewFieldIdentifierData,
  QlceViewField
} from 'services/ce/services'
import type { setGroupByFn } from '@ce/types'
import css from './GroupByView.module.scss'
 
interface BaseGroupByProps {
  groupBy: QlceViewFieldInputInput
  setGroupBy: setGroupByFn
}
 
interface GroupByDropDownProps {
  data: QlceViewFieldIdentifierData
  setGroupBy: setGroupByFn
  isBusinessMapping: boolean
  openBusinessMappingDrawer: () => void
}
 
export const GroupByDropDown: React.FC<GroupByDropDownProps> = ({
  data,
  setGroupBy,
  isBusinessMapping,
  openBusinessMappingDrawer
}) => {
  const { getString } = useStrings()
 
  const values = data.values.filter(val => val) as QlceViewField[]
 
  return (
    <>
      <ul className={css.groupByList}>
        {values.map((value, index) => {
          const onClick: () => void = () => {
            setGroupBy({
              identifier: data.identifier,
              identifierName: data.identifierName,
              fieldId: value.fieldId,
              fieldName: value.fieldName
            })
          }
          return (
            <li key={`fieldName-${index}`} className={cx(css.groupByListItems, Classes.POPOVER_DISMISS)}>
              <MenuItem text={value.fieldName} onClick={onClick} />
            </li>
          )
        })}
      </ul>
      {isBusinessMapping ? (
        <Button
          icon="plus"
          text={getString('ce.businessMapping.newButton')}
          onClick={openBusinessMappingDrawer}
          minimal
          intent="primary"
        />
      ) : null}
    </>
  )
}
 
interface GroupByViewProps extends BaseGroupByProps {
  field: QlceViewFieldIdentifierData
  openBusinessMappingDrawer: () => void
}
 
export const GroupByView: React.FC<GroupByViewProps> = ({ field, setGroupBy, groupBy, openBusinessMappingDrawer }) => {
  const isBusinessMapping = field.identifier === ViewFieldIdentifier.BusinessMapping
  return (
    <Popover
      key={`identifierName-${field.identifierName}`}
      interactionKind={PopoverInteractionKind.CLICK}
      position={Position.BOTTOM_LEFT}
      modifiers={{
        arrow: { enabled: false },
        flip: { enabled: true },
        keepTogether: { enabled: true },
        preventOverflow: { enabled: true }
      }}
      usePortal={false}
      content={
        <GroupByDropDown
          openBusinessMappingDrawer={openBusinessMappingDrawer}
          isBusinessMapping={isBusinessMapping}
          setGroupBy={setGroupBy}
          data={field}
        />
      }
    >
      {groupBy.identifier === field.identifier ? (
        <div className={cx(css.groupByItems, css.activeItem)}>{`${field.identifierName}: ${groupBy.fieldName}`}</div>
      ) : (
        <div className={css.groupByItems}>{field.identifierName}</div>
      )}
    </Popover>
  )
}
 
interface LabelDropDownProps {
  setGroupBy: setGroupByFn
  data: Maybe<string>[]
}
 
export const LabelDropDown: React.FC<LabelDropDownProps> = ({ data, setGroupBy }) => {
  const values = data.filter(val => val) as string[]
  const [searchText, setSearchText] = useState('')
 
  const filteredResults = values.filter(val => val.includes(searchText))
  return (
    <section className={css.labelDropDownContainer}>
      <TextInput
        className={css.search}
        autoFocus
        placeholder="Search Value"
        onChange={e => {
          const event = e as any
          const value = event.target.value
          setSearchText(value)
        }}
      />
      <ul className={css.groupByList}>
        {filteredResults.map(value => {
          const onClick: () => void = () => {
            setGroupBy({
              identifier: ViewFieldIdentifier.Label,
              identifierName: 'Label',
              fieldId: 'labels.value',
              fieldName: value
            })
          }
          return (
            <li key={`fieldName-${value}`} className={cx(css.groupByListItems, Classes.POPOVER_DISMISS)}>
              <MenuItem text={value} onClick={onClick} />
            </li>
          )
        })}
      </ul>
      {!filteredResults.length ? <div className={css.noResults}>No results found</div> : null}
    </section>
  )
}
 
interface LabelViewProps extends BaseGroupByProps {
  labelData: Maybe<string>[]
}
 
export const LabelView: React.FC<LabelViewProps> = ({ setGroupBy, labelData, groupBy }) => {
  return (
    <Popover
      key={`identifier-name-label`}
      interactionKind={PopoverInteractionKind.CLICK}
      position={Position.BOTTOM_LEFT}
      modifiers={{
        arrow: { enabled: false },
        flip: { enabled: true },
        keepTogether: { enabled: true },
        preventOverflow: { enabled: true }
      }}
      usePortal={false}
      content={<LabelDropDown setGroupBy={setGroupBy} data={labelData} />}
    >
      {groupBy.identifier === 'LABEL' ? (
        <div className={cx(css.groupByItems, css.activeItem)}>{`Label: ${groupBy.fieldName}`}</div>
      ) : (
        <div className={css.groupByItems}>Label</div>
      )}
    </Popover>
  )
}
 
interface CommonGroupByViewProps extends BaseGroupByProps {
  value: QlceViewField
}
 
const CommonGroupByView: React.FC<CommonGroupByViewProps> = ({ groupBy, setGroupBy, value }) => {
  return groupBy.identifier === ViewFieldIdentifier.Common && groupBy.fieldId === value.fieldId ? (
    <div className={cx(css.groupByItems, css.activeItem)}>{value.fieldName}</div>
  ) : (
    <div
      className={css.groupByItems}
      key={`common-${value.fieldId}`}
      onClick={() => {
        setGroupBy({
          identifier: ViewFieldIdentifier.Common,
          identifierName: 'Common',
          fieldId: value.fieldId,
          fieldName: value.fieldName
        })
      }}
    >
      {value.fieldName}
    </div>
  )
}
 
interface GroupByComponentProps extends BaseGroupByProps {
  labelData: Maybe<string>[]
  fieldIdentifierData: Maybe<QlceViewFieldIdentifierData>[]
  openBusinessMappingDrawer: () => void
}
 
const GroupByComponent: React.FC<GroupByComponentProps> = ({
  labelData,
  fieldIdentifierData,
  groupBy,
  setGroupBy,
  openBusinessMappingDrawer
}) => {
  const { getString } = useStrings()
  const commonFields = fieldIdentifierData?.filter(
    field => field && field.identifier === ViewFieldIdentifier.Common
  ) as QlceViewFieldIdentifierData[]
  const otherFields = fieldIdentifierData?.filter(
    field => field && field.identifier !== ViewFieldIdentifier.Common
  ) as QlceViewFieldIdentifierData[]
 
  return (
    <section className={css.groupByContainer}>
      <label className={css.groupByLabel}> {getString('ce.perspectives.createPerspective.preview.groupBy')}</label>
      <div className={css.groupBys}>
        {otherFields.map(field => {
          return field.values.length ? (
            <GroupByView
              openBusinessMappingDrawer={openBusinessMappingDrawer}
              field={field}
              setGroupBy={setGroupBy}
              groupBy={groupBy}
            />
          ) : null
        })}
        {commonFields.length
          ? commonFields[0].values.map(value => {
              Eif (value) {
                if (value.fieldId === 'label') {
                  return <LabelView setGroupBy={setGroupBy} labelData={labelData} groupBy={groupBy} />
                } else {
                  return <CommonGroupByView groupBy={groupBy} setGroupBy={setGroupBy} value={value} />
                }
              }
            })
          : null}
      </div>
    </section>
  )
}
 
export default GroupByComponent