1- import { useCallback , useEffect , useState } from 'react'
1+ import { useCallback , useEffect , useRef , useState } from 'react'
22import CreatePipelineStage from './CreatePipelineStage'
33import Breadcrumb from 'components/Breadcrumb'
44import { Button } from 'components/base/forms/Button'
@@ -64,7 +64,8 @@ function CreateReleasePipeline() {
6464 } ,
6565 ] = useUpdateReleasePipelineMutation ( )
6666
67- const [ publishReleasePipeline ] = usePublishReleasePipelineMutation ( )
67+ const [ publishReleasePipeline , { isLoading : isPublishingPipeline } ] =
68+ usePublishReleasePipelineMutation ( )
6869
6970 const [ pipelineData , setPipelineData ] = useState < ReleasePipelineRequest > ( {
7071 name : '' ,
@@ -75,19 +76,22 @@ function CreateReleasePipeline() {
7576 const [ isEditingName , setIsEditingName ] = useState (
7677 ! pipelineData ?. name ?. length ,
7778 )
79+ const skipNextCreateToast = useRef ( false )
7880
7981 const handleSuccess = useCallback (
80- ( updated = false ) => {
82+ ( updated = false , skipToast = false ) => {
8183 history . push ( `/project/${ projectId } /release-pipelines` )
82- toast ( `Release pipeline ${ updated ? 'updated' : 'created' } successfully` )
84+ if ( ! skipToast ) {
85+ toast (
86+ `Release pipeline ${ updated ? 'updated' : 'created' } successfully` ,
87+ )
88+ }
8389 } ,
8490 [ history , projectId ] ,
8591 )
8692
87- const [ isSavingAndPublishing , setIsSavingAndPublishing ] = useState ( false )
88-
8993 useEffect ( ( ) => {
90- if ( isSavingAndPublishing ) {
94+ if ( skipNextCreateToast . current ) {
9195 return
9296 }
9397
@@ -98,15 +102,10 @@ function CreateReleasePipeline() {
98102 if ( isUpdatingPipelineSuccess ) {
99103 return handleSuccess ( true )
100104 }
101- } , [
102- isCreatingPipelineSuccess ,
103- isUpdatingPipelineSuccess ,
104- handleSuccess ,
105- isSavingAndPublishing ,
106- ] )
105+ } , [ isCreatingPipelineSuccess , isUpdatingPipelineSuccess , handleSuccess ] )
107106
108107 useEffect ( ( ) => {
109- if ( isSavingAndPublishing ) {
108+ if ( skipNextCreateToast . current ) {
110109 return
111110 }
112111
@@ -125,7 +124,6 @@ function CreateReleasePipeline() {
125124 createPipelineError ,
126125 isUpdatingPipelineError ,
127126 updatePipelineError ,
128- isSavingAndPublishing ,
129127 ] )
130128
131129 useEffect ( ( ) => {
@@ -248,20 +246,20 @@ function CreateReleasePipeline() {
248246
249247 const handleSubmitAndPublish = async ( id ?: number ) => {
250248 try {
251- setIsSavingAndPublishing ( true )
249+ skipNextCreateToast . current = true
252250 const response = await handleSubmit ( id )
253251 await publishReleasePipeline ( {
254252 pipelineId : response . id ,
255253 projectId : Number ( projectId ) ,
256- } )
257- toast ( `Release pipeline published successfully` )
254+ } ) . unwrap ( )
255+ toast ( 'Release pipeline published successfully' )
256+ handleSuccess ( true , true )
257+ skipNextCreateToast . current = false
258258 } catch ( error ) {
259259 toast (
260260 `Error ${ id ? 'updating' : 'creating' } and publishing pipeline` ,
261261 'danger' ,
262262 )
263- } finally {
264- setIsSavingAndPublishing ( false )
265263 }
266264 }
267265
@@ -271,7 +269,10 @@ function CreateReleasePipeline() {
271269 }
272270
273271 const isSaveDisabled =
274- ! validatePipelineData ( ) || isCreatingPipeline || isUpdatingPipeline
272+ ! validatePipelineData ( ) ||
273+ isCreatingPipeline ||
274+ isUpdatingPipeline ||
275+ isPublishingPipeline
275276
276277 const saveButtonText = existingPipeline ?. id ? 'Update' : 'Save'
277278
0 commit comments