import * as React from 'react'; import { NumberInput, ReactSelectInput } from '@spinnaker/core'; import type { IScalingPolicyAlarm, IStepAdjustment } from '../../../../../domain'; import './StepPolicyAction.less'; export type Operator = 'Add' | 'Remove' | 'Set to'; export type AdjustmentTypeView = 'instances' | 'percent of group'; export interface IStepPolicyActionProps { adjustmentType: AdjustmentTypeView; adjustmentTypeChanged: (action: Operator, type: AdjustmentTypeView) => void; alarm: IScalingPolicyAlarm; isMin: boolean; operator: Operator; step: { stepAdjustments: IStepAdjustment[] }; stepAdjustments?: IStepAdjustment[]; stepsChanged: (steps: IStepAdjustment[]) => void; } export const StepPolicyAction = ({ adjustmentType, adjustmentTypeChanged, alarm, isMin, operator, step, stepAdjustments, stepsChanged, }: IStepPolicyActionProps) => { const hasEqualTo = alarm?.comparisonOperator.includes('Equal'); const availableActions = ['Add', 'Remove', 'Set to']; const adjustmentTypeOptions = operator === 'Set to' ? ['instances'] : ['instances', 'percent of group']; const onActionChange = (val: Operator) => { adjustmentTypeChanged(val, adjustmentType); }; const onAdjustmentTypeChange = (type: AdjustmentTypeView) => { adjustmentTypeChanged(operator, type); }; const steps = step?.stepAdjustments || stepAdjustments; const addStep = () => { const newStep = { scalingAdjustment: 1 } as IStepAdjustment; const newSteps = [...steps, newStep]; stepsChanged(newSteps); }; const removeStep = (index: number) => { const newSteps = steps.filter((_s, i) => i !== index); stepsChanged(newSteps); }; const updateStep = (updatedStep: IStepAdjustment, index: number) => { const newSteps = [...steps]; newSteps[index] = updatedStep; stepsChanged(newSteps); }; return (
{steps?.map((step: IStepAdjustment, index: number) => (
{Boolean(index) ? ( {operator} ) : ( onActionChange(e.target.value)} clearable={false} inputClassName="action-input sp-margin-xs-right" /> )} updateStep({ ...step, scalingAdjustment: Number.parseInt(e.target.value) }, index)} inputClassName="action-input" /> {Boolean(index) ? ( {adjustmentType} ) : ( onAdjustmentTypeChange(e.target.value)} clearable={false} inputClassName="adjustment-type-input sp-margin-xs-left" /> )} {' '} when {alarm?.metricName} is{' '} {index === steps.length - 1 && ( {` ${isMin ? 'less' : 'greater'} than${!Boolean(index) || hasEqualTo ? ' or equal to' : ''} ${ isMin ? step.metricIntervalUpperBound || '' : step.metricIntervalLowerBound || '' } `} )} {index < steps.length - 1 && ( <> between {isMin ? ( updateStep({ ...step, metricIntervalLowerBound: Number.parseFloat(e.target.value) }, index) } inputClassName="action-input" /> ) : ( {step.metricIntervalLowerBound} )} and {isMin ? ( {step.metricIntervalUpperBound} ) : ( updateStep({ ...step, metricIntervalUpperBound: Number.parseFloat(e.target.value) }, index) } inputClassName="action-input" /> )} )} {Boolean(index) && ( removeStep(index)} /> )}
))}
Documentation
); };