@@ -9,14 +9,18 @@ import {
99} from 'common/types/responses'
1010import Button from 'components/base/forms/Button'
1111import Icon from 'components/Icon'
12- import { TIME_UNIT_OPTIONS , TimeUnit , TRIGGER_OPTIONS } from './constants'
13- import { StageAction } from 'common/types/responses'
12+ import {
13+ NEW_PIPELINE_STAGE ,
14+ NEW_PIPELINE_STAGE_ACTION ,
15+ TIME_UNIT_OPTIONS ,
16+ TimeUnit ,
17+ TRIGGER_OPTIONS ,
18+ } from './constants'
1419import moment from 'moment'
1520import Input from 'components/base/forms/Input'
16- import { PipelineStageRequest } from 'common/types/requests'
21+ import { PipelineStageRequest , StageActionRequest } from 'common/types/requests'
1722import PipelineStageActions from './PipelineStageActions'
1823
19- type DraftStageType = PipelineStageRequest
2024type TimeUnitType = ( typeof TimeUnit ) [ keyof typeof TimeUnit ]
2125
2226const CreatePipelineStage = ( {
@@ -26,8 +30,8 @@ const CreatePipelineStage = ({
2630 showRemoveButton,
2731 stageData,
2832} : {
29- stageData : DraftStageType
30- onChange : ( stageData : DraftStageType ) => void
33+ stageData : PipelineStageRequest
34+ onChange : ( stageData : PipelineStageRequest ) => void
3135 projectId : number
3236 showRemoveButton ?: boolean
3337 onRemove ?: ( ) => void
@@ -73,8 +77,8 @@ const CreatePipelineStage = ({
7377 } , [ environmentsData ] )
7478
7579 const handleOnChange = (
76- fieldName : keyof DraftStageType ,
77- value : string | number | StageTrigger | Omit < StageAction , 'id' > [ ] ,
80+ fieldName : keyof PipelineStageRequest ,
81+ value : string | number | StageTrigger | StageActionRequest [ ] ,
7882 ) => {
7983 onChange ( { ...stageData , [ fieldName ] : value } )
8084 }
@@ -104,25 +108,36 @@ const CreatePipelineStage = ({
104108 // eslint-disable-next-line react-hooks/exhaustive-deps
105109 } , [ environmentWithFeatureVersioningOptions , stageData ] )
106110
107- const handleSegmentChange = ( option : { value : number ; label : string } ) => {
108- if ( option ?. value ) {
109- const actions = stageData . actions . map ( ( action ) => {
110- if ( action . action_type === StageActionType . TOGGLE_FEATURE_FOR_SEGMENT ) {
111- return {
112- ...action ,
113- action_body : { ...action . action_body , segment_id : option ?. value } ,
114- }
111+ const handleSegmentChange = (
112+ option : { value : number ; label : string } ,
113+ actionIndex : number ,
114+ ) => {
115+ if ( ! option ?. value ) {
116+ return
117+ }
118+
119+ const actions = stageData . actions . map ( ( action , idx ) => {
120+ if (
121+ action . action_type === StageActionType . TOGGLE_FEATURE_FOR_SEGMENT &&
122+ idx === actionIndex
123+ ) {
124+ return {
125+ ...action ,
126+ action_body : { ...action . action_body , segment_id : option ?. value } ,
115127 }
116- return action
117- } )
128+ }
129+ return action
130+ } )
118131
119- handleOnChange ( 'actions' , actions )
120- }
132+ handleOnChange ( 'actions' , actions )
121133 }
122134
123- const handleActionChange = ( option : { value : string ; label : string } ) => {
135+ const handleActionChange = (
136+ option : { value : string ; label : string } ,
137+ actionIndex : number ,
138+ ) => {
124139 if ( option . value === '' ) {
125- return handleOnChange ( 'actions' , [ ] )
140+ return
126141 }
127142
128143 const isSegment = option . value . includes ( 'FOR_SEGMENT' )
@@ -134,7 +149,23 @@ const CreatePipelineStage = ({
134149
135150 const action_body = { enabled }
136151
137- handleOnChange ( 'actions' , [ { action_body, action_type } ] )
152+ const actions = stageData . actions . map ( ( action , idx ) => {
153+ if ( idx === actionIndex ) {
154+ return { action_body, action_type }
155+ }
156+ return action
157+ } )
158+ handleOnChange ( 'actions' , actions )
159+ }
160+
161+ const handleAddAction = ( ) => {
162+ const defaultDraftAction = NEW_PIPELINE_STAGE_ACTION
163+ handleOnChange ( 'actions' , [ ...stageData . actions , defaultDraftAction ] )
164+ }
165+
166+ const handleRemoveAction = ( index : number ) => {
167+ const actions = stageData . actions . filter ( ( _ , i ) => i !== index )
168+ handleOnChange ( 'actions' , actions )
138169 }
139170
140171 const setWaitForTrigger = ( time : number , unit : TimeUnitType ) => {
@@ -281,10 +312,11 @@ const CreatePipelineStage = ({
281312 projectId = { projectId }
282313 onActionChange = { handleActionChange }
283314 onSegmentChange = { handleSegmentChange }
315+ onAddAction = { handleAddAction }
316+ onRemoveAction = { handleRemoveAction }
284317 />
285318 </ div >
286319 )
287320}
288321
289- export type { DraftStageType }
290322export default CreatePipelineStage
0 commit comments