11// Libraries
22import React , { FC , useContext } from 'react'
3+ import { useHistory } from 'react-router-dom'
34import { useDispatch , useSelector } from 'react-redux'
45import {
56 ButtonType ,
@@ -22,6 +23,7 @@ import {checkTaskLimits} from 'src/cloud/actions/limits'
2223import { Button } from '@influxdata/clockface'
2324import { PipeContext } from 'src/flows/context/pipe'
2425import { FlowContext } from 'src/flows/context/flow.current'
26+ import { FlowListContext } from 'src/flows/context/flow.list'
2527
2628// Utils
2729import { event } from 'src/cloud/utils/reporting'
@@ -45,8 +47,10 @@ const ExportTaskButton: FC<Props> = ({
4547 disabled,
4648} ) => {
4749 const { flow} = useContext ( FlowContext )
50+ const { add} = useContext ( FlowListContext )
4851 const { data} = useContext ( PipeContext )
4952 const dispatch = useDispatch ( )
53+ const history = useHistory ( )
5054 const org = useSelector ( getOrg )
5155
5256 const onClick = ( ) => {
@@ -100,24 +104,41 @@ const ExportTaskButton: FC<Props> = ({
100104 }
101105
102106 if ( isFlagEnabled ( 'createWithFlows' ) ) {
103- fetch ( `/api/v2private/notebooks/${ flow . id } /resources` , {
104- method : 'POST' ,
105- headers : {
106- 'Content-Type' : 'application/json' ,
107- } ,
108- body : JSON . stringify ( {
109- panel : data . id ,
110- resource : resp . data . id ,
111- type : 'tasks' ,
112- } ) ,
113- } )
114- }
107+ new Promise ( resolve => {
108+ if ( flow . id ) {
109+ resolve ( flow . id )
110+ return
111+ }
115112
116- dispatch ( notify ( taskCreatedSuccess ( ) ) )
117- dispatch ( checkTaskLimits ( ) )
113+ add ( flow ) . then ( id => resolve ( id ) )
114+ } )
115+ . then ( id => {
116+ return fetch ( `/api/v2private/notebooks/${ id } /resources` , {
117+ method : 'POST' ,
118+ headers : {
119+ 'Content-Type' : 'application/json' ,
120+ } ,
121+ body : JSON . stringify ( {
122+ panel : data . id ,
123+ resource : resp . data . id ,
124+ type : 'tasks' ,
125+ } ) ,
126+ } ) . then ( ( ) => {
127+ return id
128+ } )
129+ } )
130+ . then ( id => {
131+ dispatch ( notify ( taskCreatedSuccess ( ) ) )
132+ dispatch ( checkTaskLimits ( ) )
133+
134+ if ( onCreate ) {
135+ onCreate ( resp . data )
136+ }
118137
119- if ( onCreate ) {
120- onCreate ( resp . data )
138+ if ( id !== flow . id ) {
139+ history . replace ( `/orgs/${ org . id } /notebooks/${ id } ` )
140+ }
141+ } )
121142 }
122143 } )
123144 . catch ( error => {
0 commit comments