Skip to content

Commit 947f1a6

Browse files
committed
refactor register connection to use hook
1 parent 0371522 commit 947f1a6

File tree

9 files changed

+239
-155
lines changed

9 files changed

+239
-155
lines changed

packages/frontend/src/components/ChooseConnectionSubstep/index.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,10 @@ function ChooseConnectionSubstep(
6363
connectionId: connection?.id,
6464
flowId: supportsConnectionRegistration ? step.flowId : undefined,
6565
},
66-
// cache-first to prevent the test connection from being called multiple times
67-
fetchPolicy: 'cache-first',
6866
skip: !connection?.id,
6967
})
7068

7169
const isTestStepValid = useMemo(() => {
72-
if (application.key === APP_ALLOWING_EMPTY_CONNECTION) {
73-
return true
74-
}
75-
7670
if (testResultLoading || !testConnectionData?.testConnection) {
7771
return null
7872
}
@@ -83,13 +77,20 @@ function ChooseConnectionSubstep(
8377
return false
8478
}
8579
return true
86-
}, [application.key, testConnectionData, testResultLoading])
80+
}, [testConnectionData, testResultLoading])
8781

8882
const connectionStatus: ConnectionStatus = useMemo(() => {
8983
if (!connection) {
90-
return {
91-
text: 'Not connected',
92-
color: 'yellow.200',
84+
if (application.key === APP_ALLOWING_EMPTY_CONNECTION) {
85+
return {
86+
text: 'No connection',
87+
color: 'green.500',
88+
}
89+
} else {
90+
return {
91+
text: 'Not connected',
92+
color: 'yellow.200',
93+
}
9394
}
9495
} else if (testResultLoading) {
9596
return {

packages/frontend/src/components/FlowStepConfigurationModal/ChooseAndAddConnection/AddConnection.tsx

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,29 @@ import Form from '../../Form'
2020
import { infoboxMdComponents } from '../../MarkdownRenderer/CustomMarkdownComponents'
2121
import { DEFAULT_ADD_CONNECTION_LABEL } from '../constants'
2222
import { FlowStepConfigurationContext } from '../FlowStepConfigurationContext'
23+
import { useConnectionVerification } from '../hooks/useConnectionRegistration'
2324
import InvalidModalScreen from '../InvalidModalScreen'
2425

2526
import ConnectionHeader from './ConnectionHeader'
2627

2728
type AddConnectionProps = {
28-
onSubmit: (response: Record<string, unknown>) => void
29-
onBack: () => void
29+
handleConnectionChange: (
30+
connectionId: string,
31+
shouldRefetch: boolean,
32+
) => Promise<void>
33+
onCreateOrUpdateStep: (connectionId: string) => Promise<void>
3034
}
3135

3236
type Response = {
3337
[key: string]: any
3438
}
3539

3640
export default function AddConnection(props: AddConnectionProps): JSX.Element {
37-
const { onSubmit, onBack } = props
38-
const {
39-
modalState: { selectedApp },
40-
} = useContext(FlowStepConfigurationContext)
41+
const { handleConnectionChange, onCreateOrUpdateStep } = props
42+
const { modalState, patchModalState } = useContext(
43+
FlowStepConfigurationContext,
44+
)
45+
const { selectedApp } = modalState
4146
const { name, authDocUrl, key, auth } = selectedApp || {}
4247

4348
const [error, setError] = useState<IJSONObject | null>(null)
@@ -61,6 +66,57 @@ export default function AddConnection(props: AddConnectionProps): JSX.Element {
6166
}
6267
}, [])
6368

69+
const supportsConnectionRegistration =
70+
!!selectedApp?.auth?.connectionRegistrationType
71+
72+
const { onRegisterConnection, testConnection } = useConnectionVerification({
73+
supportsConnectionRegistration,
74+
})
75+
76+
/**
77+
* Test connection first for apps that support connection registration
78+
* If connection is not verified, redirect to choose-connection screen to connect
79+
* If connection is verified, register connection and create step
80+
*
81+
* For apps without connection registration, create step directly
82+
*/
83+
const handleAddConnection = useCallback(
84+
async (response: Record<string, any>) => {
85+
const newConnectionId = response?.createConnection?.id as
86+
| string
87+
| undefined
88+
89+
if (newConnectionId) {
90+
patchModalState({ isLoading: true })
91+
if (supportsConnectionRegistration) {
92+
const testResult = await testConnection(newConnectionId)
93+
// If connection is not verified and has an error message
94+
if (!testResult?.registrationVerified && !!testResult?.message) {
95+
await handleConnectionChange(newConnectionId, true)
96+
patchModalState({
97+
selectedConnectionId: newConnectionId,
98+
currentScreen: 'choose-connection',
99+
isLoading: false,
100+
})
101+
return
102+
} else {
103+
await onRegisterConnection(newConnectionId)
104+
}
105+
}
106+
// Create or update step at the end
107+
await onCreateOrUpdateStep(newConnectionId)
108+
}
109+
},
110+
[
111+
handleConnectionChange,
112+
onCreateOrUpdateStep,
113+
onRegisterConnection,
114+
patchModalState,
115+
supportsConnectionRegistration,
116+
testConnection,
117+
],
118+
)
119+
64120
const submitHandler: SubmitHandler<FieldValues> = useCallback(
65121
async (data) => {
66122
if (!steps) {
@@ -95,15 +151,17 @@ export default function AddConnection(props: AddConnectionProps): JSX.Element {
95151
stepIndex++
96152

97153
if (stepIndex === steps.length) {
98-
onSubmit(response)
154+
await handleAddConnection(response)
99155
}
100156
}
101157

102158
setInProgress(false)
103159
},
104-
[key, steps, onSubmit],
160+
[key, steps, handleAddConnection],
105161
)
106162

163+
const onBack = () => patchModalState({ currentScreen: 'choose-connection' })
164+
107165
if (!selectedApp) {
108166
return <InvalidModalScreen />
109167
}
@@ -129,7 +187,7 @@ export default function AddConnection(props: AddConnectionProps): JSX.Element {
129187
DEFAULT_ADD_CONNECTION_LABEL
130188
}
131189
/>
132-
<ModalCloseButton mt={6} size="xs" />
190+
<ModalCloseButton mt={2} size="xs" />
133191

134192
<ModalBody mt={2}>
135193
{auth?.connectionType !== 'user-added' ? (

packages/frontend/src/components/FlowStepConfigurationModal/ChooseAndAddConnection/ChooseConnection.tsx

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import type { ITestConnectionOutput } from '@plumber/types'
2-
3-
import { useCallback, useContext } from 'react'
1+
import { useContext } from 'react'
42
import { BiChevronLeft } from 'react-icons/bi'
5-
import { useMutation, useQuery } from '@apollo/client'
63
import { Flex, ModalBody, ModalHeader } from '@chakra-ui/react'
74
import { Button, ModalCloseButton } from '@opengovsg/design-system-react'
85

96
import { EditorContext } from '@/contexts/Editor'
10-
import { REGISTER_CONNECTION } from '@/graphql/mutations/register-connection'
11-
import { TEST_CONNECTION } from '@/graphql/queries/test-connection'
127

138
import { DEFAULT_CHOOSE_CONNECTION_LABEL } from '../constants'
149
import { FlowStepConfigurationContext } from '../FlowStepConfigurationContext'
@@ -22,8 +17,11 @@ import { ConnectionDropdownOption } from '.'
2217
interface ChooseConnectionProps {
2318
appConnectionsLoading: boolean
2419
connectionOptions: ConnectionDropdownOption[]
25-
handleConnectionChange: (value: string, shouldRefetch: boolean) => void
26-
onCreateOrUpdateStep: (connectionId: string) => void
20+
handleConnectionChange: (
21+
value: string,
22+
shouldRefetch: boolean,
23+
) => Promise<void>
24+
onCreateOrUpdateStep: (connectionId: string) => Promise<void>
2725
}
2826

2927
export default function ChooseConnection(
@@ -35,55 +33,14 @@ export default function ChooseConnection(
3533
handleConnectionChange,
3634
onCreateOrUpdateStep,
3735
} = props
38-
const { readOnly, flowId } = useContext(EditorContext)
36+
const { readOnly } = useContext(EditorContext)
3937

4038
const { modalState, patchModalState, step } = useContext(
4139
FlowStepConfigurationContext,
4240
)
4341
const { selectedApp, selectedConnectionId } = modalState
44-
45-
const supportsConnectionRegistration =
46-
!!selectedApp?.auth?.connectionRegistrationType
47-
4842
const connectionModalLabel = selectedApp?.auth?.connectionModalLabel
4943

50-
const {
51-
loading: testResultLoading,
52-
refetch: retestConnection,
53-
data: testConnectionData,
54-
} = useQuery<{
55-
testConnection: ITestConnectionOutput
56-
}>(TEST_CONNECTION, {
57-
variables: {
58-
connectionId: selectedConnectionId,
59-
flowId: supportsConnectionRegistration ? flowId : undefined,
60-
},
61-
skip: !selectedConnectionId,
62-
})
63-
64-
const [registerConnection, { loading: registerConnectionLoading }] =
65-
useMutation(REGISTER_CONNECTION)
66-
67-
const onRegisterConnection = useCallback(async () => {
68-
if (selectedConnectionId && supportsConnectionRegistration) {
69-
await registerConnection({
70-
variables: {
71-
input: {
72-
connectionId: selectedConnectionId,
73-
flowId,
74-
},
75-
},
76-
})
77-
await retestConnection()
78-
}
79-
}, [
80-
selectedConnectionId,
81-
supportsConnectionRegistration,
82-
registerConnection,
83-
flowId,
84-
retestConnection,
85-
])
86-
8744
const onBack = () => {
8845
if (!selectedApp) {
8946
return
@@ -133,7 +90,7 @@ export default function ChooseConnection(
13390
}
13491
/>
13592
</ModalHeader>
136-
<ModalCloseButton mt={6} size="xs" />
93+
<ModalCloseButton mt={2} size="xs" />
13794

13895
<ModalBody>
13996
<Flex flexDir="column" alignItems="center" gap={6}>
@@ -149,13 +106,10 @@ export default function ChooseConnection(
149106
}
150107
/>
151108
<SetConnectionButton
152-
onNextStep={() => onCreateOrUpdateStep(selectedConnectionId)}
153-
onRegisterConnection={onRegisterConnection}
109+
onNextStep={async () =>
110+
await onCreateOrUpdateStep(selectedConnectionId)
111+
}
154112
readOnly={readOnly}
155-
supportsConnectionRegistration={supportsConnectionRegistration}
156-
testResult={testConnectionData?.testConnection}
157-
testResultLoading={testResultLoading}
158-
registerConnectionLoading={registerConnectionLoading}
159113
isNewStep={!step?.appKey || !step?.key}
160114
/>
161115
</Flex>

0 commit comments

Comments
 (0)