All files / modules/75-ce/components/BusinessMappingBuilder/CostBucketStep CostBucketStep.tsx

95.24% Statements 20/21
100% Branches 2/2
80% Functions 4/5
95.24% Lines 20/21

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              8x 8x   8x 8x 8x   8x   8x 8x 8x                                     8x                 15x 15x 15x   15x   15x                                     15x                                                                   15x   1x     1x                                                                               8x  
/*
 * 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 { Container, Button, ButtonSize, Color } from '@harness/uicore'
import { FieldArray } from 'formik'
 
import React from 'react'
import { DragDropContext, Droppable, DropResult } from 'react-beautiful-dnd'
import { EMPTY_PERSPECTIVE_RULE } from '@ce/utils/perspectiveUtils'
import type { CostBucketWidgetType, CostTargetType, SharedCostType } from '@ce/types'
import { useStrings } from 'framework/strings'
import type { QlceViewFieldIdentifierData } from 'services/ce/services'
import CostBucketBuilder from '../CostBucketBuilder'
import { getCostBucketTitleMap, getNewBucketButtonText } from '../constants'
import Step from '../Step/Step'
 
interface CostBucketStepProps {
  formikProps: any
  fieldValuesList: QlceViewFieldIdentifierData[]
  namespace: string
  value: SharedCostType[] | CostTargetType[]
  isSharedCost?: boolean
  widgetType: CostBucketWidgetType
  stepProps: {
    color: Color
    background: Color
    total: number
    current: number
    defaultOpen: boolean
    isComingSoon?: boolean
  }
}
 
const CostBucketStep: (props: CostBucketStepProps) => React.ReactElement = ({
  formikProps,
  fieldValuesList,
  namespace,
  value,
  isSharedCost,
  widgetType,
  stepProps
}) => {
  const { getString } = useStrings()
  const titleMap = getCostBucketTitleMap(getString)
  const newBucketButtonText = getNewBucketButtonText(getString)
 
  const namespaceKey = formikProps.values[`${namespace}Key`]
 
  return (
    <FieldArray
      name={namespace}
      render={arrayHelper => {
        /* istanbul ignore next */
        const addNewCostBucket: () => void = () => {
          arrayHelper.push({
            name: '',
            isOpen: true,
            isViewerOpen: false,
            strategy: 'FIXED',
            rules: [
              {
                viewConditions: [EMPTY_PERSPECTIVE_RULE]
              }
            ]
          })
        }
 
        return (
          <Step
            actionButtonProps={{
              showActionButton: value.length > 0,
              actionButtonText: newBucketButtonText[widgetType],
              actionOnClick: addNewCostBucket
            }}
            stepProps={stepProps}
            title={titleMap[widgetType]}
          >
            <Container
              margin={{
                top: 'medium'
              }}
              border={{
                top: true
              }}
            >
              <DragDropContext
                onDragEnd={
                  /* istanbul ignore next */ (result: DropResult) => {
                    if (!result.destination) {
                      return
                    }
                    const res = Array.from(value)
                    const [removed] = res.splice(result.source.index, 1)
                    res.splice(result.destination.index, 0, removed)
                    formikProps.setFieldValue(`${namespace}Key`, namespaceKey + 1)
                    formikProps.setFieldValue(namespace, res as any)
                  }
                }
              >
                <Droppable droppableId={`droppable-${namespace}`} type={`cost-bucket-${namespace}`}>
                  {provided => (
                    <div ref={provided.innerRef}>
                      {value.map((val: CostTargetType | SharedCostType, index: number) => {
                        const removeCostBucket = () => {
                          arrayHelper.remove(index)
                        }
                        return (
                          <CostBucketBuilder
                            namespace={namespace}
                            key={`${namespace}-${index}-${namespaceKey}`}
                            removeCostBucket={removeCostBucket}
                            value={val}
                            index={index}
                            fieldValuesList={fieldValuesList}
                            setFieldValue={formikProps.setFieldValue}
                            validateField={formikProps.validateField}
                            isSharedCost={isSharedCost}
                            widgetType={widgetType}
                          />
                        )
                      })}
                      {provided.placeholder}
                    </div>
                  )}
                </Droppable>
              </DragDropContext>
              {value.length < 1 ? (
                <Button
                  icon="plus"
                  text={newBucketButtonText[widgetType]}
                  minimal
                  margin={{
                    top: 'medium'
                  }}
                  size={ButtonSize.SMALL}
                  onClick={addNewCostBucket}
                />
              ) : null}
            </Container>
          </Step>
        )
      }}
    />
  )
}
 
export default CostBucketStep