11import { gql } from '@apollo/client'
22import { Alert , Button } from 'antd'
33import { useRouter } from 'next/router'
4- import { useEffect , useMemo , useState } from 'react'
4+ import { useEffect , useMemo , useRef , useState } from 'react'
55import { useAccount } from 'wagmi'
66import { Integration } from '../../../graphql'
77import { NotificationTrigger } from '../../../src/constants/notification-triggers'
@@ -10,7 +10,7 @@ import { useGetIntegrationTriggers } from '../../../src/services/IntegrationTrig
1010import { useCreateWorkflowWithOperations } from '../../../src/services/WizardHooks'
1111import { SchemaForm } from '../../common/Forms/schema-form/SchemaForm'
1212import { Loading } from '../../common/RequestStates/Loading'
13- import { TriggerInputsForm } from '../../workflow-nodes/drawer/steps/TriggerInputsForm'
13+ import { TriggerInputsForm , TriggerInputsFormRef } from '../../workflow-nodes/drawer/steps/TriggerInputsForm'
1414import { IntegrationNotificationStep } from './IntegrationNotificationStep'
1515
1616const integrationFragment = gql `
@@ -36,16 +36,17 @@ export function NotificationStep({ notificationTrigger, readonly }: Props) {
3636 const [ inputs , setInputs ] = useState < Record < string , any > > ( { } )
3737 const [ credentialsId , setCredentialsId ] = useState < string > ( )
3838 const [ notificationIntegration , setNotificationIntegration ] = useState < Integration > ( )
39+ const [ loading , setLoading ] = useState ( false )
3940 const [ error , setError ] = useState < string | null > ( null )
4041 const { address } = useAccount ( )
4142 const {
4243 createWorflowWithOperations,
4344 workflow,
4445 workflowActions,
45- loading,
4646 error : createError ,
4747 } = useCreateWorkflowWithOperations ( )
4848 const router = useRouter ( )
49+ const triggerInputsFormRef = useRef < TriggerInputsFormRef > ( null )
4950
5051 // notification triggers without schema need to fetch from the integration trigger
5152 const schemaDefined = ! ! notificationTrigger . schema
@@ -76,6 +77,13 @@ export function NotificationStep({ notificationTrigger, readonly }: Props) {
7677 }
7778 } , [ router , workflow , workflowActions ] )
7879
80+ // stop loading on errors
81+ useEffect ( ( ) => {
82+ if ( createError ) {
83+ setLoading ( false )
84+ }
85+ } , [ createError ] )
86+
7987 const getWorkflowActionData = ( key : string ) => {
8088 const actionData = notificationTrigger . actionData ( inputs )
8189 switch ( key ) {
@@ -141,25 +149,33 @@ export function NotificationStep({ notificationTrigger, readonly }: Props) {
141149 }
142150
143151 const onFormSubmit = async ( ) => {
144- if ( ! inputs ) {
152+ const childInputs = triggerInputsFormRef ?. current ?. getInputs ( )
153+ const workflowInputs = childInputs ?? inputs
154+
155+ if ( ! workflowInputs ) {
145156 return
146157 }
147158
159+ setLoading ( true )
160+ setError ( null )
161+
148162 try {
149163 // throws exception if inputs are invalid
150- notificationTrigger . validateInputs ?.( inputs )
164+ notificationTrigger . validateInputs ?.( workflowInputs )
151165 } catch ( e ) {
166+ setLoading ( false )
152167 setError ( ( e as Error ) . message )
153168 return
154169 }
155170
156171 if ( ! notificationIntegration ) {
172+ setLoading ( false )
157173 setError ( 'Please select an integration' )
158174 return
159175 }
160- setError ( null )
176+
161177 try {
162- const { integrationKey, operationKey } = notificationTrigger . triggerData ( inputs )
178+ const { integrationKey, operationKey } = notificationTrigger . triggerData ( workflowInputs )
163179 await createWorflowWithOperations ( {
164180 workflowName : notificationTrigger . workflowName ,
165181 triggerIntegration : {
@@ -168,7 +184,7 @@ export function NotificationStep({ notificationTrigger, readonly }: Props) {
168184 trigger : {
169185 key : operationKey ,
170186 inputs : {
171- ...inputs ,
187+ ...workflowInputs ,
172188 } ,
173189 ...( notificationTrigger . instantTrigger
174190 ? { }
@@ -182,6 +198,7 @@ export function NotificationStep({ notificationTrigger, readonly }: Props) {
182198 actions : [ getWorkflowActionData ( notificationIntegration . key ) ] ,
183199 } )
184200 } catch ( e : any ) {
201+ setLoading ( false )
185202 setError ( e . message )
186203 }
187204 }
@@ -201,10 +218,10 @@ export function NotificationStep({ notificationTrigger, readonly }: Props) {
201218 </ div >
202219 ) : integrationTrigger ? (
203220 < TriggerInputsForm
221+ ref = { triggerInputsFormRef }
204222 triggerId = { integrationTrigger . id }
205223 accountCredentialId = { undefined }
206224 initialInputs = { inputs }
207- onChange = { setChangedInputs }
208225 onSubmitOperationInputs = { onFormSubmit }
209226 hideSubmit
210227 readonly = { readonly }
0 commit comments