All files / modules/75-ce/components/COGatewayConfig COGatewayConfig.tsx

71.7% Statements 38/53
53.57% Branches 45/84
46.15% Functions 6/13
71.7% Lines 38/53

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              4x 4x 4x 4x   4x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x                     4x 19x 17x 17x 17x 17x       17x 17x   17x                             17x 11x     17x   11x 11x 11x     11x 11x       17x                         11x                                       17x 11x                           17x                                                                                                                                             4x  
/*
 * 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 React, { useState, useEffect, useRef, useLayoutEffect } from 'react'
import { debounce as _debounce, isEmpty as _isEmpty, defaultTo as _defaultTo } from 'lodash-es'
import { Drawer } from '@blueprintjs/core'
import { Container, Layout, Button } from '@wings-software/uicore'
import type { ASRuleCreationActiveStep, GatewayDetails } from '@ce/components/COCreateGateway/models'
import COHelpSidebar from '@ce/components/COHelpSidebar/COHelpSidebar'
import type { Service } from 'services/lw'
import { CONFIG_IDLE_TIME_CONSTRAINTS, CONFIG_STEP_IDS, CONFIG_TOTAL_STEP_COUNTS, RESOURCES } from '@ce/constants'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { USER_JOURNEY_EVENTS } from '@ce/TrackingEventsConstants'
import { Utils } from '@ce/common/Utils'
import DefineRule from './steps/DefineRule'
import ManageResources from './steps/ManageResources/ManageResources'
import ResourceFulfilment from './steps/ResourceFulfilment'
import AdvancedConfiguration from './steps/AdvancedConfiguration/AdvancedConfiguration'
import { getSelectedResourceFromGatewayDetails, isActiveStep } from './helper'
import css from './COGatewayConfig.module.scss'
 
interface COGatewayConfigProps {
  gatewayDetails: GatewayDetails
  setGatewayDetails: (gwDetails: GatewayDetails) => void
  valid: boolean
  setValidity: (tab: boolean) => void
  activeStepDetails?: ASRuleCreationActiveStep | null
  allServices: Service[]
}
 
const COGatewayConfig: React.FC<COGatewayConfigProps> = props => {
  const { trackEvent } = useTelemetry()
  const isGcpProvider = Utils.isProviderGcp(props.gatewayDetails.provider)
  const [drawerOpen, setDrawerOpen] = useState<boolean>(false)
  const [totalStepsCount, setTotalStepsCount] = useState<number>(CONFIG_TOTAL_STEP_COUNTS.DEFAULT)
  const [selectedResource, setSelectedResource] = useState<RESOURCES | null>(
    getSelectedResourceFromGatewayDetails(props.gatewayDetails)
  )
 
  const configContEl = useRef<HTMLDivElement>(null)
  const [activeDrawerIds, setActiveDrawerIds] = useState<string[]>([CONFIG_STEP_IDS[0], CONFIG_STEP_IDS[1]])
 
  const observeScrollHandler = _debounce(() => {
    const configStepsContainers: HTMLElement[] = []
    CONFIG_STEP_IDS.forEach((_id: string) => {
      const element = document.getElementById(_id)
      element && configStepsContainers.push(element)
    })
    const newActiveIds: string[] = []
    configStepsContainers.forEach((stepEl, _i) => {
      if (isActiveStep(stepEl, configContEl.current as HTMLDivElement)) {
        newActiveIds.push(stepEl.id)
      }
    })
    setActiveDrawerIds(newActiveIds)
  }, 500)
 
  useEffect(() => {
    trackEvent(USER_JOURNEY_EVENTS.RULE_CREATION_STEP_1, {})
  }, [])
 
  useLayoutEffect(() => {
    {
      ;(configContEl.current as HTMLDivElement).addEventListener('scroll', observeScrollHandler)
      const el = document.getElementById(`configStep${props.activeStepDetails?.count}`)
      el?.scrollIntoView()
    }
 
    return () => {
      ;(configContEl.current as HTMLDivElement).removeEventListener('scroll', observeScrollHandler)
    }
  }, [])
 
  const groupsValidation = (): boolean => {
    if (isGcpProvider) {
      return (props.gatewayDetails.routing.instance.scale_group?.desired as number) > 0
    } else {
      return (
        (props.gatewayDetails.routing.instance.scale_group?.on_demand as number) <=
          (props.gatewayDetails.routing.instance.scale_group?.max as number) &&
        (props.gatewayDetails.routing.instance.scale_group?.spot as number) >= 0
      )
    }
  }
 
  function isValid(): boolean {
    return (
      (props.gatewayDetails.selectedInstances.length > 0 ||
        !_isEmpty(props.gatewayDetails.routing?.instance?.scale_group) ||
        !_isEmpty(props.gatewayDetails.metadata.kubernetes_connector_id) ||
        !_isEmpty(props.gatewayDetails.routing.container_svc) ||
        !_isEmpty(props.gatewayDetails.routing.database)) &&
      props.gatewayDetails.name !== '' &&
      props.gatewayDetails.idleTimeMins >= CONFIG_IDLE_TIME_CONSTRAINTS.MIN &&
      props.gatewayDetails.idleTimeMins <= CONFIG_IDLE_TIME_CONSTRAINTS.MAX &&
      (selectedResource === RESOURCES.INSTANCES && !isGcpProvider ? props.gatewayDetails.fullfilment !== '' : true) &&
      (!_isEmpty(props.gatewayDetails.deps)
        ? props.gatewayDetails.deps.every(_dep => !isNaN(_dep.dep_id as number) && !isNaN(_dep.delay_secs as number))
        : true) &&
      (!_isEmpty(props.gatewayDetails.routing.instance.scale_group) ? groupsValidation() : true) &&
      (selectedResource === RESOURCES.ECS
        ? _defaultTo(props.gatewayDetails.routing.container_svc?.task_count, 0) > -1
        : true)
    )
  }
 
  useEffect(() => {
    props.setValidity(isValid())
  }, [
    props.gatewayDetails.selectedInstances,
    props.gatewayDetails.name,
    props.gatewayDetails.idleTimeMins,
    props.gatewayDetails.fullfilment,
    props.gatewayDetails.deps,
    selectedResource,
    props.gatewayDetails.metadata.kubernetes_connector_id,
    props.gatewayDetails.routing?.instance?.scale_group,
    props.gatewayDetails.routing?.container_svc,
    props.gatewayDetails.routing?.database
  ])
 
  return (
    <Layout.Vertical ref={configContEl} className={css.page}>
      {/* {drawerOpen && (
        <COFixedDrawer
          topMargin={85}
          content={<COHelpSidebar pageName="configuration" activeSectionNames={activeDrawerIds} />}
          onClose={() => setDrawerOpen(false)}
        />
      )} */}
      <Drawer
        autoFocus={true}
        enforceFocus={true}
        hasBackdrop={true}
        usePortal={true}
        canOutsideClickClose={true}
        canEscapeKeyClose={true}
        isOpen={drawerOpen}
        onClose={() => {
          setDrawerOpen(false)
        }}
        size="392px"
        style={{
          // top: '85px',
          boxShadow: 'rgb(40 41 61 / 4%) 0px 2px 8px, rgb(96 97 112 / 16%) 0px 16px 24px',
          height: '100vh',
          overflowY: 'scroll'
        }}
      >
        <Container style={{ textAlign: 'right' }}>
          <Button icon="cross" minimal onClick={_ => setDrawerOpen(false)} />
        </Container>
        <COHelpSidebar pageName="configuration" activeSectionNames={activeDrawerIds} />
      </Drawer>
      <Container style={{ paddingTop: 10 }}>
        <Layout.Vertical spacing="large" padding="large">
          <DefineRule
            gatewayDetails={props.gatewayDetails}
            setGatewayDetails={props.setGatewayDetails}
            setDrawerOpen={setDrawerOpen}
            totalStepsCount={totalStepsCount}
          />
          <ManageResources
            gatewayDetails={props.gatewayDetails}
            setGatewayDetails={props.setGatewayDetails}
            totalStepsCount={totalStepsCount}
            setTotalStepsCount={setTotalStepsCount}
            setDrawerOpen={setDrawerOpen}
            selectedResource={selectedResource}
            setSelectedResource={setSelectedResource}
          />
          <ResourceFulfilment
            gatewayDetails={props.gatewayDetails}
            setGatewayDetails={props.setGatewayDetails}
            setDrawerOpen={setDrawerOpen}
            totalStepsCount={totalStepsCount}
            selectedResource={selectedResource}
          />
          <AdvancedConfiguration
            gatewayDetails={props.gatewayDetails}
            setGatewayDetails={props.setGatewayDetails}
            totalStepsCount={totalStepsCount}
            allServices={props.allServices}
            selectedResource={selectedResource as RESOURCES}
            activeStepDetails={props.activeStepDetails}
          />
        </Layout.Vertical>
      </Container>
    </Layout.Vertical>
  )
}
 
export default COGatewayConfig