All files / modules/10-common/pages/welcome SelectModuleList.tsx

89.66% Statements 52/58
32.43% Branches 12/37
88.89% Functions 8/9
89.47% Lines 51/57

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              9x 9x   9x 9x 9x 9x 9x 9x 9x   9x 9x                             9x 5x   5x 5x 5x 5x 5x       5x 5x   5x 1x 1x     5x   5x 25x           25x   1x 1x 1x           1x                   5x 25x   25x                         5x 2x 2x   2x 2x 2x   2x     5x 5x 5x 5x 5x   5x 5x 5x           5x                     9x  
/*
 * 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, { useLayoutEffect, useRef, useState } from 'react'
import { useHistory, useParams } from 'react-router-dom'
import type { IconName } from '@wings-software/uicore'
import { useTelemetry } from '@common/hooks/useTelemetry'
import routes from '@common/RouteDefinitions'
import { useUpdateAccountDefaultExperienceNG } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import { Experiences } from '@common/constants/Utils'
import { useToaster } from '@common/components'
import { Category, PurposeActions } from '@common/constants/TrackingConstants'
import type { Module, AccountPathProps } from '@common/interfaces/RouteInterfaces'
import ModuleCard from './ModuleCard'
import css from './WelcomePage.module.scss'
 
interface ModuleProps {
  enabled: boolean
  titleIcon: IconName
  bodyIcon: IconName
  module: Module
  description: string
}
 
interface SelectModuleListProps {
  onModuleClick: (module?: Module) => void
  moduleList: ModuleProps[]
}
 
const SelectModuleList: React.FC<SelectModuleListProps> = ({ onModuleClick, moduleList }) => {
  const [selected, setSelected] = useState<Module>()
 
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const { trackEvent } = useTelemetry()
  const { showError } = useToaster()
  const { mutate: updateDefaultExperience, loading: updatingDefaultExperience } = useUpdateAccountDefaultExperienceNG({
    accountIdentifier: accountId
  })
 
  const [dimensions, setDimensions] = useState({ width: window.innerWidth, height: innerHeight })
  const ref = useRef<HTMLDivElement>(null)
 
  const handleModuleSelection = (module: Module): void => {
    setSelected(module)
    onModuleClick(module)
  }
 
  const history = useHistory()
 
  const getButtonProps = (buttonType: string): { clickHandle?: () => void; disabled?: boolean } => {
    switch (buttonType) {
      case 'cd':
      case 'ci':
      case 'ce':
      case 'cv':
      case 'cf':
        return {
          clickHandle: () => {
            trackEvent(PurposeActions.ModuleContinue, { category: Category.SIGNUP, module: buttonType })
            try {
              updateDefaultExperience({
                defaultExperience: Experiences.NG
              }).then(() => {
                history.push(routes.toModuleHome({ accountId, module: buttonType, source: 'purpose' }))
              })
            } catch (error) {
              showError(error.data?.message || getString('somethingWentWrong'))
            }
          },
          disabled: updatingDefaultExperience
        }
      default:
        return {}
    }
  }
 
  const moduleListElements = moduleList.map(option => {
    const buttonProp: { clickHandle?: () => void; disabled?: boolean } = getButtonProps(option.module)
 
    return (
      <ModuleCard
        key={option.module}
        option={option}
        onClick={handleModuleSelection}
        selected={selected === option.module}
        buttonText={getString('continue')}
        buttonDisabled={buttonProp.disabled}
        handleButtonClick={buttonProp.clickHandle}
      />
    )
  })
 
  useLayoutEffect(() => {
    const resize = () => {
      setDimensions({ width: window.innerWidth, height: window.innerHeight })
    }
    window.addEventListener('resize', resize)
    resize()
    const removeListner = () => window.removeEventListener('resize', resize)
 
    return removeListner
  }, [])
 
  const wWI = dimensions.width
  const wHI = dimensions.height
  const scaleW = Math.max(wWI / 1920, 1)
  const scaleH = Math.max(wHI / 1080, 1)
  const scale = Math.min(scaleH, scaleW)
 
  let topMarginAdd = 0
  let leftMarginAdd = 0
  Iif (scale > 1 && ref !== null && ref !== undefined) {
    const height = ref?.current?.clientHeight
    const width = ref?.current?.clientWidth
    topMarginAdd = height ? (height * (scale - 1)) / 3 : 0
    leftMarginAdd = width ? (width * (scale - 1)) / 3 : 0
  }
  return (
    <div
      ref={ref}
      className={css.moduleList}
      style={{ transform: `scale(${scale}) translate(${leftMarginAdd}px,${topMarginAdd}px)` }}
    >
      {moduleListElements}
    </div>
  )
}
 
export default SelectModuleList