All files / modules/72-triggers/pages/triggers/views/subviews ExpressionBreakdown.tsx

97.22% Statements 35/36
98.31% Branches 58/59
100% Functions 3/3
97.14% Lines 34/35

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              5x 5x 5x 5x 5x   5x 5x             5x             5x                           5x   295x   295x   295x 295x 59x 236x 59x 36x 4x   32x 177x 59x     118x 59x     59x 59x         5x 295x                       5x         59x 59x 59x 59x                                                                                                     5x  
/*
 * 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 { Layout, Container, Text, HarnessDocTooltip } from '@wings-software/uicore'
import { isUndefined } from 'lodash-es'
import cx from 'classnames'
import { useStrings } from 'framework/strings'
import type { StringKeys } from 'framework/strings'
import { EXP_BREAKDOWN_INPUTS, scheduleTabsId, getMilitaryHours, getSlashValue, getDayOfWeekStr } from './ScheduleUtils'
import css from './ExpressionBreakdown.module.scss'
 
interface ExpressionBreakdownPropsInterface {
  formikValues?: any
  activeInputs: EXP_BREAKDOWN_INPUTS[]
}
 
export const ActiveInputs = {
  MINUTES: EXP_BREAKDOWN_INPUTS.MINUTES,
  HOURS: EXP_BREAKDOWN_INPUTS.HOURS,
  DAY_OF_MONTH: EXP_BREAKDOWN_INPUTS.DAY_OF_MONTH,
  MONTH: EXP_BREAKDOWN_INPUTS.MONTH,
  DAY_OF_WEEK: EXP_BREAKDOWN_INPUTS.DAY_OF_WEEK
}
const ColumnWidth = {
  SMALL: 100,
  MEDIUM: 125,
  LARGE: 250
}
interface ColumnInterface {
  width: number
  inactive: boolean
  label: string
  value?: string
  getString: (key: StringKeys) => string
  hideValuesRow: boolean
}
 
const getColumnValue = (formikValues: any, column: EXP_BREAKDOWN_INPUTS): string | undefined => {
  const { minutes, hours, amPm, dayOfMonth, month, startMonth, dayOfWeek, selectedScheduleTab, expression } =
    formikValues
 
  const isCustomTab = selectedScheduleTab === scheduleTabsId.CUSTOM
 
  const expressionArr = expression?.trim() && isCustomTab ? expression.trim().split(' ') : undefined
  if (column === EXP_BREAKDOWN_INPUTS.MINUTES) {
    return isCustomTab ? expressionArr?.[0] : getSlashValue({ selectedScheduleTab, id: 'minutes', value: minutes })
  } else if (column === EXP_BREAKDOWN_INPUTS.HOURS) {
    if (isCustomTab) return expressionArr?.[1]
    else if (selectedScheduleTab === scheduleTabsId.HOURLY) {
      return getSlashValue({ selectedScheduleTab, id: 'hours', value: hours })
    }
    return getMilitaryHours({ hours, amPm })
  } else if (column === EXP_BREAKDOWN_INPUTS.DAY_OF_MONTH) {
    return isCustomTab
      ? expressionArr?.[2]
      : getSlashValue({ selectedScheduleTab, id: 'dayOfMonth', value: dayOfMonth })
  } else if (column === EXP_BREAKDOWN_INPUTS.MONTH) {
    return isCustomTab
      ? expressionArr?.[3]
      : getSlashValue({ selectedScheduleTab, id: 'month', startMonth, value: month })
  } else Eif (column === EXP_BREAKDOWN_INPUTS.DAY_OF_WEEK) {
    return isCustomTab ? expressionArr?.[4] : getDayOfWeekStr(dayOfWeek)
  }
  return undefined
}
 
const Column = ({ width, inactive, label, value, getString, hideValuesRow }: ColumnInterface): JSX.Element => (
  <Layout.Vertical width={width} data-name="column" className={css.column}>
    <Container className={css.label}>
      <Text>{label}</Text>
    </Container>
    {!hideValuesRow && (
      <Container className={cx(css.value, inactive && css.inactive)}>
        <Text>{(!isUndefined(value) && value) || getString('invalidText')}</Text>
      </Container>
    )}
  </Layout.Vertical>
)
 
const ExpressionBreakdown: React.FC<ExpressionBreakdownPropsInterface> = ({
  formikValues,
  formikValues: { selectedScheduleTab },
  activeInputs
}): JSX.Element => {
  const { getString } = useStrings()
  const minutesValue = getColumnValue(formikValues, EXP_BREAKDOWN_INPUTS.MINUTES)
  const hideValuesRow = selectedScheduleTab === scheduleTabsId.CUSTOM && isUndefined(minutesValue)
  return (
    <Container data-name="expressionBreakdown" className={css.expressionBreakdown}>
      <Text className={css.title} data-tooltip-id="expressionBreakdown">
        {getString('triggers.schedulePanel.expressionBreakdown')}
        <HarnessDocTooltip tooltipId="expressionBreakdown" useStandAlone={true} />
      </Text>
      <Layout.Horizontal>
        <Column
          width={ColumnWidth.SMALL}
          label={getString('triggers.schedulePanel.minutesLabel')}
          value={minutesValue}
          inactive={!activeInputs.includes(ActiveInputs.MINUTES)}
          getString={getString}
          hideValuesRow={hideValuesRow}
        />
        <Column
          width={ColumnWidth.SMALL}
          label={getString('triggers.schedulePanel.hoursLabel')}
          value={getColumnValue(formikValues, EXP_BREAKDOWN_INPUTS.HOURS)}
          inactive={!activeInputs.includes(ActiveInputs.HOURS)}
          getString={getString}
          hideValuesRow={hideValuesRow}
        />
        <Column
          width={ColumnWidth.MEDIUM}
          label={getString('triggers.schedulePanel.dayOfMonthLabel')}
          value={getColumnValue(formikValues, EXP_BREAKDOWN_INPUTS.DAY_OF_MONTH)}
          inactive={!activeInputs.includes(ActiveInputs.DAY_OF_MONTH)}
          getString={getString}
          hideValuesRow={hideValuesRow}
        />
        <Column
          width={ColumnWidth.SMALL}
          label={getString('triggers.schedulePanel.monthLabel')}
          value={getColumnValue(formikValues, EXP_BREAKDOWN_INPUTS.MONTH)}
          inactive={!activeInputs.includes(ActiveInputs.MONTH)}
          getString={getString}
          hideValuesRow={hideValuesRow}
        />
        <Column
          width={ColumnWidth.LARGE}
          label={getString('triggers.schedulePanel.dayOfWeekLabel')}
          value={getColumnValue(formikValues, EXP_BREAKDOWN_INPUTS.DAY_OF_WEEK)}
          inactive={!activeInputs.includes(ActiveInputs.DAY_OF_WEEK)}
          getString={getString}
          hideValuesRow={hideValuesRow}
        />
      </Layout.Horizontal>
    </Container>
  )
}
export default ExpressionBreakdown