All files / modules/75-ce/common/FixedSchedulesList FixedSchedulesList.tsx

96.97% Statements 64/66
77.98% Branches 85/109
100% Functions 12/12
96.88% Lines 62/64

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              8x 8x 8x 8x 8x   8x           8x 8x 8x                                             8x 8x   8x 11x 11x   11x       11x 11x 11x 11x   11x 11x   11x     11x     11x 11x 11x 11x       11x 65x   11x                                                                         1x 1x                 8x 21x 20x 20x 20x 20x 17x   20x 17x     20x 20x 20x         24x         20x 20x 7x 7x   20x 7x 7x     20x     21x             8x 7x 7x 7x                             11x     1x 1x                   8x  
/*
 * 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 { isEmpty as _isEmpty } from 'lodash-es'
import cx from 'classnames'
import { Container, Icon, Layout, Text } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import type { FixedScheduleClient } from '@ce/components/COCreateGateway/models'
import {
  CE_DATE_FORMAT_INTERNAL,
  FORMAT_12_HOUR,
  get24HourTimeIn12HourFormat,
  getTimePeriodString
} from '@ce/utils/momentUtils'
import { DaysOfWeek } from '@ce/constants'
import { Utils } from '@ce/common/Utils'
import css from './FixedScheduleList.module.scss'
 
interface FixedSchedeulesListProps {
  data: FixedScheduleClient[]
  handleEdit?: (schedule: FixedScheduleClient, index: number) => void
  handleDelete?: (schedule: FixedScheduleClient, index: number) => void
  isEditable?: boolean
  showHeaders?: boolean
  shrinkColumns?: boolean
}
 
interface ScheduleItemProps {
  schedule: FixedScheduleClient
  onEdit: (data: FixedScheduleClient) => void
  onDelete: (data: FixedScheduleClient) => void
  isEditable: boolean
  shrinkColumns?: boolean
}
 
interface ScheduleDescriptionProps {
  schedule: FixedScheduleClient
}
 
const dateFormat = `${CE_DATE_FORMAT_INTERNAL} ${FORMAT_12_HOUR}`
const EMPTY_TEXT = 'N/A'
 
const ScheduleItem: React.FC<ScheduleItemProps> = props => {
  const { getString } = useStrings()
  const { schedule, onEdit, onDelete } = props
 
  Iif (schedule.isDeleted) {
    return null
  }
 
  const gerPeriodString = () => {
    let str = ''
    Eif (!_isEmpty(schedule.beginsOn)) {
      str += getTimePeriodString(schedule.beginsOn as string, dateFormat)
    }
    Eif (str.length && !_isEmpty(schedule.endsOn)) {
      str += ` - ${getTimePeriodString(schedule.endsOn as string, dateFormat)}`
    }
    Iif (_isEmpty(str)) {
      str = EMPTY_TEXT
    }
    return str
  }
 
  const period = gerPeriodString()
  const startTime = !_isEmpty(schedule.startTime) ? `${schedule.startTime?.hour}:${schedule.startTime?.min}` : ''
  const endTime = !_isEmpty(schedule.endTime) ? `${schedule.endTime?.hour}:${schedule.endTime?.min}` : ''
  const duration = schedule.allDay
    ? getString('ce.co.autoStoppingRule.configuration.step4.tabs.schedules.allDay')
    : (startTime ? get24HourTimeIn12HourFormat(startTime) : '') +
      (endTime ? '-' + get24HourTimeIn12HourFormat(endTime) : '')
  const durationText = duration || EMPTY_TEXT
  const repeats = schedule?.repeats?.map(n => DaysOfWeek[n].substring(0, 3)).join(', ') || EMPTY_TEXT
 
  return (
    <Container key={`${schedule.name}`} className={css.scheduleItemContainer}>
      <Layout.Horizontal>
        <div className={cx(css.col1, { [css.shrinkCol]: props.shrinkColumns })}>
          <Layout.Horizontal spacing="small">
            <Text lineClamp={1}>{schedule.name}</Text>
            <span className={Utils.getConditionalResult(schedule.type === 'downtime', css.downTag, css.upTag)}>
              {schedule.type}
            </span>
          </Layout.Horizontal>
        </div>
        <Layout.Vertical className={css.col2}>
          {period !== EMPTY_TEXT && (
            <Layout.Horizontal spacing={'small'}>
              <Icon name="calendar" />
              <Text>{period}</Text>
            </Layout.Horizontal>
          )}
          {durationText !== EMPTY_TEXT && (
            <Layout.Horizontal spacing={'small'}>
              <Icon name="time" />
              <Text>{'Duration: ' + durationText}</Text>
            </Layout.Horizontal>
          )}
          {repeats !== EMPTY_TEXT && (
            <Layout.Horizontal spacing={'small'}>
              <Icon name="main-rerun" />
              <Text>{'Repeats: ' + repeats}</Text>
            </Layout.Horizontal>
          )}
          <Layout.Horizontal spacing={'small'}>
            <Icon name="synced" />
            <Text>{'Timezone: ' + schedule.timezone}</Text>
          </Layout.Horizontal>
        </Layout.Vertical>
        {props.isEditable && (
          <Layout.Horizontal className={css.col3} spacing="medium">
            <Icon name="Edit" onClick={() => onEdit(schedule)} />
            <Icon name="main-trash" onClick={() => onDelete(schedule)} />
          </Layout.Horizontal>
        )}
      </Layout.Horizontal>
      <ScheduleDescription schedule={schedule} />
    </Container>
  )
}
 
export const ScheduleDescription: React.FC<ScheduleDescriptionProps> = props => {
  const descriptionText = React.useMemo(() => {
    const baseStr = 'Resources will be'
    const state = Utils.getConditionalResult(props.schedule.type === 'uptime', 'up', 'down')
    let periodStr = ''
    if (!_isEmpty(props.schedule.beginsOn)) {
      periodStr = `starting from ${getTimePeriodString(props.schedule.beginsOn as string, dateFormat)}`
    }
    if (!_isEmpty(props.schedule.endsOn)) {
      periodStr += ` and ending on ${getTimePeriodString(props.schedule.endsOn as string, dateFormat)}`
    }
 
    let repeatStr = ''
    Eif (!_isEmpty(props.schedule.repeats)) {
      repeatStr = `on every ${
        props.schedule.repeats?.length === 7
          ? 'day'
          : props.schedule.repeats
              ?.sort()
              .map(r => DaysOfWeek[r].substring(0, 3))
              .join(', ')
      }`
    }
 
    let timeStr = props.schedule.allDay ? '(all day)' : ''
    if (_isEmpty(timeStr) && !_isEmpty(props.schedule.startTime)) {
      const sTime = `${props.schedule.startTime?.hour}:${props.schedule.startTime?.min}`
      timeStr = `from ${get24HourTimeIn12HourFormat(sTime)}`
    }
    if (!props.schedule.allDay && !_isEmpty(props.schedule.endTime)) {
      const eTime = `${props.schedule.endTime?.hour}:${props.schedule.endTime?.min}`
      timeStr += ` to ${get24HourTimeIn12HourFormat(eTime)}`
    }
 
    return `${baseStr} ${state} ${repeatStr} ${timeStr} ${periodStr}`
  }, [props.schedule])
 
  return (
    <Text font={{ size: 'small' }} className={css.description}>
      {descriptionText}
    </Text>
  )
}
 
const FixedSchedeulesList: React.FC<FixedSchedeulesListProps> = props => {
  const { getString } = useStrings()
  const { isEditable = true } = props
  return (
    <Layout.Vertical spacing="medium">
      {props.showHeaders && (
        <Layout.Horizontal>
          <div className={css.col1}>
            <Text>{getString('name')}</Text>
          </div>
          <div className={css.col2}>
            <Text>{'Details'}</Text>
          </div>
          <div className={css.col3}></div>
        </Layout.Horizontal>
      )}
      <div>
        {props.data.map((schItem, index) => (
          <ScheduleItem
            key={schItem.name}
            schedule={schItem}
            onDelete={s => props.handleDelete?.(s, index)}
            onEdit={s => props.handleEdit?.(s, index)}
            isEditable={isEditable}
            shrinkColumns={props.shrinkColumns}
          />
        ))}
      </div>
    </Layout.Vertical>
  )
}
 
export default FixedSchedeulesList