All files / modules/72-templates-library/components/TemplateStudio/TemplateContext TemplateReducer.ts

100% Statements 17/17
78.26% Branches 36/46
100% Functions 1/1
100% Lines 16/16

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              46x       46x             46x                                                                         46x                                               46x 10x 10x   1x         1x         1x         1x           1x             1x           1x               2x   1x      
/*
 * 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 { clone } from 'lodash-es'
import type { IDrawerProps } from '@blueprintjs/core'
import type { GetDataError } from 'restful-react'
import type { EntityGitDetails, EntityValidityDetails, NGTemplateInfoConfig } from 'services/template-ng'
import {
  ActionReturnType,
  DrawerTypes,
  TemplateActions
} from '@templates-library/components/TemplateStudio/TemplateContext/TemplateActions'
import type { Failure, StepElementConfig } from 'services/cd-ng'
import type { YamlBuilderHandlerBinding } from '@common/interfaces/YAMLBuilderProps'
import { DefaultNewTemplateId, DefaultNewVersionLabel, DefaultTemplate } from 'framework/Templates/templates'
 
export interface DrawerData extends Omit<IDrawerProps, 'isOpen'> {
  type: DrawerTypes
  data?: {
    paletteData?: {
      onSelection?: (stepOrGroup: StepElementConfig) => void
    }
  }
}
 
export interface TemplateViewData {
  isYamlEditable: boolean
  isDrawerOpened: boolean
  drawerData: DrawerData
}
 
export interface TemplateReducerState {
  template: NGTemplateInfoConfig
  yamlHandler?: YamlBuilderHandlerBinding
  originalTemplate: NGTemplateInfoConfig
  stableVersion: string
  lastPublishedVersion: string
  versions: string[]
  templateView: TemplateViewData
  templateIdentifier: string
  isDBInitialized: boolean
  isLoading: boolean
  isInitialized: boolean
  isBETemplateUpdated: boolean
  isUpdated: boolean
  gitDetails: EntityGitDetails
  entityValidityDetails: EntityValidityDetails
  templateYaml: string
  templateError?: GetDataError<Failure | Error> | null
}
 
export const initialState: TemplateReducerState = {
  template: { ...DefaultTemplate },
  originalTemplate: { ...DefaultTemplate },
  stableVersion: DefaultNewVersionLabel,
  versions: [DefaultNewVersionLabel],
  lastPublishedVersion: '',
  templateIdentifier: DefaultNewTemplateId,
  templateView: {
    isDrawerOpened: false,
    isYamlEditable: false,
    drawerData: {
      type: DrawerTypes.AddStep
    }
  },
  isLoading: false,
  isBETemplateUpdated: false,
  isDBInitialized: false,
  isUpdated: false,
  isInitialized: false,
  gitDetails: {},
  entityValidityDetails: {},
  templateYaml: ''
}
 
export const TemplateReducer = (state: TemplateReducerState, data: ActionReturnType): TemplateReducerState => {
  const { type, response } = data
  switch (type) {
    case TemplateActions.Initialize:
      return {
        ...state,
        isInitialized: true
      }
    case TemplateActions.DBInitialize:
      return {
        ...state,
        isDBInitialized: true
      }
    case TemplateActions.SetYamlHandler:
      return {
        ...state,
        yamlHandler: data.response?.yamlHandler
      }
    case TemplateActions.Loading: {
      return {
        ...state,
        isLoading: !!response?.isLoading
      }
    }
    case TemplateActions.UpdateTemplateView:
      return {
        ...state,
        templateView: response?.templateView
          ? clone({ ...state.templateView, ...response?.templateView })
          : state.templateView
      }
    case TemplateActions.UpdateTemplate:
      return {
        ...state,
        isUpdated: response?.isUpdated ?? true,
        template: response?.template ? clone(response?.template) : state.template
      }
    case TemplateActions.Fetching:
      return {
        ...state,
        isLoading: true,
        isBETemplateUpdated: false,
        isUpdated: false
      }
    case TemplateActions.Success:
    case TemplateActions.Error:
      return { ...state, isLoading: false, ...response }
    default:
      return state
  }
}