All files / modules/10-common/components/Wizard WizardUtils.tsx

83.05% Statements 49/59
74.14% Branches 43/58
100% Functions 6/6
84.31% Lines 43/51

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              5x 5x 5x 5x 5x   5x 5x               5x                                   5x                                             717x 656x 656x   656x 458x     458x 11x       656x 20x   656x 31x 31x     656x     5x                                                             717x   717x 717x 717x                 717x                           5x                         21x 21x 21x 18x   2x   16x 16x             5x                               1x   1x                       1x       5x                                 4x             4x                          
/*
 * 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, { RefObject, SetStateAction, Dispatch } from 'react'
import { Icon, IconName, PageSpinner } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import cx from 'classnames'
import { parse } from 'yaml'
import type { FormikProps, FormikErrors } from 'formik'
import { isUndefined, range } from 'lodash-es'
import YAMLBuilder from '@common/components/YAMLBuilder/YamlBuilder'
 
import type {
  YamlBuilderHandlerBinding,
  YamlBuilderProps,
  InvocationMapFunction
} from '@common/interfaces/YAMLBuilderProps'
 
import css from './Wizard.module.scss'
 
export interface FormikPropsInterface {
  initialValues: any
  validationSchema?: any
  validate?: ({
    formikProps,
    latestYaml
  }: {
    formikProps: FormikProps<any>
    latestYaml?: string
  }) => Promise<FormikErrors<any>> | FormikErrors<any> | any
  validateOnBlur?: boolean
  validateOnChange?: boolean
  enableReinitialize?: boolean
  onSubmit: (val: any) => void
}
 
const renderIcon = ({
  requiredFields,
  checkValidPanel,
  panelIndex,
  touchedPanels,
  isEdit,
  formikVals,
  formikErrs
}: {
  requiredFields?: string[]
  checkValidPanel?: ({
    formikValues,
    formikErrors
  }: {
    formikValues: { [key: string]: any }
    formikErrors: { [key: string]: any }
  }) => boolean
  panelIndex: number
  touchedPanels: number[]
  isEdit: boolean
  formikVals: { [key: string]: any }
  formikErrs: { [key: string]: any }
}): JSX.Element | undefined => {
  if (!touchedPanels.includes(panelIndex) && !isEdit) return
  let iconName: IconName = 'tick-circle'
  let iconColor = Color.GREEN_500
 
  let showWarningIcon = requiredFields?.some(requiredField => {
    const val = formikVals[requiredField]
    // empty array should not show warning as valid yaml spec
    // set array to undefined to see warning
    if (isUndefined(val) || (typeof val === 'string' && val === '')) {
      return true
    }
  })
 
  if (!showWarningIcon && checkValidPanel && !checkValidPanel({ formikValues: formikVals, formikErrors: formikErrs })) {
    showWarningIcon = true
  }
  if (showWarningIcon) {
    iconName = 'warning-sign'
    iconColor = Color.RED_500
  }
 
  return <Icon size={20} color={iconColor} name={iconName} />
}
 
export const renderTitle = ({
  tabTitle,
  tabTitleComponent,
  requiredFields,
  checkValidPanel,
  panelIndex,
  touchedPanels,
  isEdit,
  selectedTabIndex,
  ref,
  formikVals,
  formikErrs
}: {
  tabTitle?: string
  tabTitleComponent?: JSX.Element
  requiredFields: string[]
  checkValidPanel?: ({
    formikValues,
    formikErrors
  }: {
    formikValues: { [key: string]: any }
    formikErrors: { [key: string]: any }
  }) => boolean
  panelIndex: number
  touchedPanels: number[]
  isEdit: boolean
  selectedTabIndex: number
  formikVals: { [key: string]: any }
  formikErrs: { [key: string]: any }
  ref: RefObject<HTMLSpanElement>
}): JSX.Element => {
  let title: string | JSX.Element = ''
 
  Iif (tabTitleComponent) title = tabTitleComponent
  else Eif (tabTitle) title = tabTitle
  const icon = renderIcon({
    requiredFields,
    panelIndex,
    touchedPanels,
    isEdit,
    formikVals,
    formikErrs,
    checkValidPanel
  })
  return (
    <span ref={ref} className={css.tab}>
      {icon ? (
        icon
      ) : (
        <div className={cx(css.panelIndexCircle, selectedTabIndex === panelIndex && css.activeIndex)}>
          <span className={css.panelIndexNumber}>{panelIndex + 1}</span>
        </div>
      )}
      {title}
    </span>
  )
}
 
export const setNewTouchedPanel = ({
  upcomingTabIndex,
  touchedPanels,
  selectedTabIndex,
  setTouchedPanels,
  includeSkippedIndexes
}: {
  upcomingTabIndex: number
  touchedPanels: number[]
  selectedTabIndex: number
  setTouchedPanels: (panels: number[]) => void
  includeSkippedIndexes?: boolean
}): void => {
  const movingBackwards = selectedTabIndex && upcomingTabIndex < selectedTabIndex
  Iif (touchedPanels.includes(upcomingTabIndex) && !movingBackwards) return /* istanbul ignore else */
  if (includeSkippedIndexes) {
    if (movingBackwards && selectedTabIndex) {
      // going backwards from last
      setTouchedPanels([...touchedPanels, selectedTabIndex])
    } else {
      const additionalTabs = range(0, upcomingTabIndex)
      setTouchedPanels([...touchedPanels, ...additionalTabs])
    }
  } else {
    setTouchedPanels([...touchedPanels, selectedTabIndex])
  }
}
 
export const shouldBlockNavigation = ({
  isSubmitting,
  isValid,
  isYamlView,
  yamlHandler,
  dirty,
  getIsDirtyForm
}: {
  isSubmitting: boolean
  isValid: boolean
  isYamlView: boolean
  yamlHandler?: YamlBuilderHandlerBinding
  dirty: boolean
  getIsDirtyForm: (parsedYaml: any) => boolean
}): boolean => {
  // isValid check for yaml will happen below
  const shouldBlockNav = !(isSubmitting && (isValid || isYamlView))
 
  Iif (isYamlView && yamlHandler) {
    try {
      const parsedYaml = parse(yamlHandler.getLatestYaml())
      if (!parsedYaml) {
        return shouldBlockNav
      }
      const isDirty = getIsDirtyForm(parsedYaml)
      return shouldBlockNav && isDirty
    } catch (e) {
      return shouldBlockNav
    }
  } else {
    return dirty ? shouldBlockNav : false
  }
}
 
export const renderYamlBuilder = ({
  loadingYamlView,
  yamlBuilderReadOnlyModeProps,
  convertFormikValuesToYaml,
  formikProps,
  setYamlHandler,
  invocationMap,
  schema
}: {
  loadingYamlView?: boolean
  yamlBuilderReadOnlyModeProps: YamlBuilderProps
  convertFormikValuesToYaml?: (formikPropsValues: any) => any
  formikProps: FormikProps<any>
  setYamlHandler: Dispatch<SetStateAction<YamlBuilderHandlerBinding | undefined>>
  invocationMap?: Map<RegExp, InvocationMapFunction>
  schema?: Record<string, any>
}): JSX.Element => {
  Iif (loadingYamlView) {
    return (
      <div style={{ position: 'relative', height: 'calc(100vh - 128px)' }}>
        <PageSpinner />
      </div>
    )
  } else {
    return (
      <YAMLBuilder
        {...yamlBuilderReadOnlyModeProps}
        existingJSON={convertFormikValuesToYaml?.(formikProps.values)}
        isReadOnlyMode={false}
        showSnippetSection={false}
        bind={setYamlHandler}
        invocationMap={invocationMap}
        schema={schema}
      />
    )
  }
}