All files / modules/70-pipeline/components/TrialModalTemplate trialModalUtils.tsx

88% Statements 44/50
80% Branches 28/35
75% Functions 6/8
87.76% Lines 43/49

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                                                                                       8x                     16x 16x   2x   2x         2x         8x 18x 18x                                         21x   12x           9x                               8x           21x 21x 21x 21x 21x   21x 21x   21x 21x   21x   6x     6x     7x         1x                       7x 7x     8x                       21x    
/*
 * 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 { useHistory, useParams } from 'react-router-dom'
import routes from '@common/RouteDefinitions'
import { useStrings } from 'framework/strings'
 
import { CreateOrSelectAProjectTemplate } from '@projects-orgs/components/CreateOrSelectAProjectTemplate/CreateOrSelectAProjectTemplate'
import { CreatePipelineForm } from '@pipeline/components/CreatePipelineForm/CreatePipelineForm'
import { SelectOrCreatePipelineForm } from '@pipeline/components/SelectOrCreatePipelineForm/SelectOrCreatePipelineForm'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
 
import type { Project, PipelineInfoConfig } from 'services/cd-ng'
import type { ModuleLicenseType } from '@common/constants/SubscriptionTypes'
import type { Module } from '@common/interfaces/RouteInterfaces'
import type { StringsMap } from 'stringTypes'
 
export enum TrialType {
  CREATE_OR_SELECT_PIPELINE,
  CREATE_OR_SELECT_PROJECT,
  SET_UP_PIPELINE
}
 
export interface TrialModalProps {
  selectedProject?: Project
  openProjectModal: (project?: Project) => void
  pushToPipelineStudio: (pipelinId: string, projectData: Project, search?: string) => void
}
export type onCreatePipeline = (values: PipelineInfoConfig) => void
export type onSelectPipeline = (value: string) => void
export type CreateOrSelectProjectProps = {
  onCreateProject: () => void
}
export type PipelineProps = {
  onSuccess: onCreatePipeline | onSelectPipeline
}
export interface UseTrialModalProps {
  actionProps: PipelineProps | CreateOrSelectProjectProps
  trialType: TrialType
  onCloseModal?: () => void
}
 
export interface UseTrialModalReturns {
  openTrialModal: () => void
  closeTrialModal: () => void
}
 
export interface FormPropsReturn {
  child: React.ReactElement
  description: string
}
 
export interface OpenModalProps {
  modal?: ModuleLicenseType
  gettingProjects: boolean
  gettingPipelines: boolean
  selectedProject?: Project
  pipelinesExist: boolean
  projectsExist: boolean
  openProjectModal: (project?: Project) => void
  openTrialModal: () => void
  pushToPipelineStudio: (pipelinId: string, projectData: Project, search?: string) => void
}
 
export function openModal(props: OpenModalProps): void {
  const {
    modal,
    gettingProjects,
    gettingPipelines,
    selectedProject,
    pipelinesExist,
    projectsExist,
    openProjectModal,
    openTrialModal,
    pushToPipelineStudio
  } = props
  if (modal && !gettingProjects && !gettingPipelines) {
    // selectedProject exists and no pipelines, forward to create pipeline
    Iif (selectedProject && !pipelinesExist) {
      pushToPipelineStudio('-1', selectedProject, `?modal=${modal}`)
    } else Iif (!selectedProject && !projectsExist) {
      // selectedProject doesnot exist and projects donot exist, open project modal
      openProjectModal()
    } else {
      // otherwise, just open trial modal
      openTrialModal()
    }
  }
}
 
export const getTrialModalProps = (props: TrialModalProps): Omit<UseTrialModalProps, 'onCloseModal'> => {
  const { selectedProject, openProjectModal, pushToPipelineStudio } = props
  return selectedProject
    ? {
        actionProps: {
          onSuccess: (pipelineId: string) => pushToPipelineStudio(pipelineId, selectedProject)
        },
        trialType: TrialType.CREATE_OR_SELECT_PIPELINE
      }
    : {
        actionProps: {
          onCreateProject: openProjectModal
        },
        trialType: TrialType.CREATE_OR_SELECT_PROJECT
      }
}
 
interface FormByModulePropsReturn {
  description: string
  moduleDescription: string
}
 
function getFormPropsByModule(module: string): FormByModulePropsReturn {
  switch (module) {
    case 'cd': {
      return {
        description: 'cd.cdTrialHomePage.startTrial.description',
        moduleDescription: 'cd.continuous'
      }
    }
    case 'ci': {
      return {
        description: 'ci.ciTrialHomePage.startTrial.description',
        moduleDescription: 'ci.continuous'
      }
    }
    case 'cf': {
      return {
        description: 'cf.cfTrialHomePage.startTrial.description',
        moduleDescription: 'cf.continuous'
      }
    }
  }
 
  return { description: '', moduleDescription: '' }
}
 
export function useGetFormPropsByTrialType({
  trialType,
  actionProps,
  module,
  onCloseModal
}: UseTrialModalProps & { module: Module }): FormPropsReturn {
  const { getString } = useStrings()
  const history = useHistory()
  const { accountId } = useParams<{ accountId: string }>()
  const { selectedProject } = useAppStore()
  let child = <></>
 
  const pipelineProps = actionProps as PipelineProps
  const projectProps = actionProps as CreateOrSelectProjectProps
 
  const { description, moduleDescription } = getFormPropsByModule(module)
  let formDescription = getString(description as keyof StringsMap)
 
  switch (trialType) {
    case TrialType.SET_UP_PIPELINE: {
      child = (
        <CreatePipelineForm handleSubmit={pipelineProps.onSuccess as onCreatePipeline} closeModal={onCloseModal} />
      )
      break
    }
    case TrialType.CREATE_OR_SELECT_PIPELINE: {
      child = (
        <SelectOrCreatePipelineForm
          handleSubmit={pipelineProps.onSuccess as onSelectPipeline}
          closeModal={onCloseModal}
          openCreatPipeLineModal={() => {
            history.push(
              routes.toPipelineStudio({
                orgIdentifier: selectedProject?.orgIdentifier || '',
                projectIdentifier: selectedProject?.identifier || '',
                pipelineIdentifier: '-1',
                accountId,
                module
              })
            )
          }}
        />
      )
      formDescription = getString('pipeline.selectOrCreateForm.description')
      break
    }
    case TrialType.CREATE_OR_SELECT_PROJECT: {
      child = (
        <CreateOrSelectAProjectTemplate
          onCreateProject={() => {
            projectProps.onCreateProject()
          }}
          closeModal={onCloseModal}
          moduleDescription={getString(moduleDescription as keyof StringsMap)}
        />
      )
    }
  }
 
  return { child, description: formDescription }
}