11// Libraries
22import React , { FC , useContext } from 'react'
3+ import { useDispatch , useSelector } from 'react-redux'
34import {
45 ButtonType ,
56 ComponentColor ,
67 ComponentStatus ,
78 IconFont ,
89} from '@influxdata/clockface'
910
11+ import {
12+ resourceLimitReached ,
13+ taskCreatedSuccess ,
14+ taskNotCreated ,
15+ } from 'src/shared/copy/notifications'
16+ import { ASSET_LIMIT_ERROR_STATUS } from 'src/cloud/constants/index'
17+ import { postTask , patchTask } from 'src/client'
18+ import { notify } from 'src/shared/actions/notifications'
19+ import { checkTaskLimits } from 'src/cloud/actions/limits'
20+
1021// Components
1122import { Button } from '@influxdata/clockface'
1223import { PipeContext } from 'src/flows/context/pipe'
@@ -15,33 +26,86 @@ import ExportTaskOverlay from 'src/flows/pipes/Schedule/ExportTaskOverlay'
1526
1627// Utils
1728import { event } from 'src/cloud/utils/reporting'
29+ import { getErrorMessage } from 'src/utils/api'
30+ import { getOrg } from 'src/organizations/selectors'
31+ import { isFlagEnabled } from 'src/shared/utils/featureFlag'
1832
1933interface Props {
2034 text : string
2135 type : string
2236 generate ?: ( ) => string
37+ onCreate ?: ( task : any ) => void
38+ disabled ?: boolean
2339}
2440
25- const ExportTaskButton : FC < Props > = ( { text, type, generate} ) => {
41+ const ExportTaskButton : FC < Props > = ( {
42+ text,
43+ type,
44+ generate,
45+ onCreate,
46+ disabled,
47+ } ) => {
2648 const { data, range} = useContext ( PipeContext )
2749 const { launch} = useContext ( PopupContext )
50+ const dispatch = useDispatch ( )
51+ const org = useSelector ( getOrg )
2852
2953 const onClick = ( ) => {
54+ const query = generate ? generate ( ) : data . query
55+
56+ if ( isFlagEnabled ( 'removeExportModal' ) ) {
57+ event ( 'Export Task Modal Skipped' , { from : type } )
58+
59+ // we can soft delete the previously connected task by marking the old one inactive
60+ if ( data ?. task ?. id ) {
61+ patchTask ( {
62+ taskID : data . task . id ,
63+ data : {
64+ status : 'inactive' ,
65+ } ,
66+ } )
67+ }
68+
69+ postTask ( { data : { orgID : org . id , flux : query } } )
70+ . then ( resp => {
71+ if ( resp . status !== 201 ) {
72+ throw new Error ( resp . data . message )
73+ }
74+
75+ dispatch ( notify ( taskCreatedSuccess ( ) ) )
76+ dispatch ( checkTaskLimits ( ) )
77+
78+ if ( onCreate ) {
79+ onCreate ( resp . data )
80+ }
81+ } )
82+ . catch ( error => {
83+ if ( error ?. response ?. status === ASSET_LIMIT_ERROR_STATUS ) {
84+ dispatch ( notify ( resourceLimitReached ( 'tasks' ) ) )
85+ } else {
86+ const message = getErrorMessage ( error )
87+ dispatch ( notify ( taskNotCreated ( message ) ) )
88+ }
89+ } )
90+
91+ return
92+ }
93+
3094 event ( 'Export Task Clicked' , { from : type } )
3195 launch ( < ExportTaskOverlay text = { text } type = { type } /> , {
3296 bucket : data . bucket ,
33- query : generate ? generate ( ) : data . query ,
97+ query,
3498 range,
3599 } )
36100 }
37101
38102 return (
39103 < Button
40104 text = { text }
41- color = { ComponentColor . Success }
105+ color = { disabled ? ComponentColor . Default : ComponentColor . Success }
42106 type = { ButtonType . Submit }
43107 onClick = { onClick }
44- status = { ComponentStatus . Default }
108+ status = { disabled ? ComponentStatus . Disabled : ComponentStatus . Default }
45109 testID = "task-form-save"
46110 style = { { opacity : 1 } }
47111 icon = { IconFont . Export_New }
0 commit comments