Skip to content

Commit 86299bf

Browse files
committed
refactor(frontend): fetch flowId directly from backend, remove manual cache manipulation for createStep and duplicate step
1 parent 9cc1d84 commit 86299bf

File tree

9 files changed

+23
-73
lines changed

9 files changed

+23
-73
lines changed

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,7 @@ export default function Editor(props: EditorProps): React.ReactElement {
3030
useContext(EditorContext)
3131

3232
const { handleReorderUpdate } = useReorderSteps(flow.id)
33-
const rawSteps = flow.steps
34-
const steps = useMemo(
35-
// Populate each step's flowId so that IStep isn't LYING about flowId being
36-
// non-undefined. We do it here instead of fetching in GraphQL since all
37-
// steps have same pipe, so a bit wasteful to repeat this data over the wire.
38-
() =>
39-
rawSteps.map((step) => ({
40-
...step,
41-
flow,
42-
flowId: flow.id,
43-
})),
44-
[flow, rawSteps],
45-
)
33+
const steps = flow.steps
4634

4735
const currentStep = useMemo(() => {
4836
return steps.find((step) => step.id === currentStepId)

packages/frontend/src/components/FlowStep/components/DuplicateStepButton.tsx

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,13 @@ interface DuplicateStepButtonProps {
1717
step: IStep
1818
}
1919

20-
/**
21-
* Helper function to update the flow in the cache
22-
*/
23-
function updateHandlerFactory(flowId: string, previousStepId: string) {
24-
return function createStepUpdateHandler(cache: any, mutationResult: any) {
25-
const { data } = mutationResult
26-
const { createStep: createdStep } = data
27-
const { getFlow: flow } = cache.readQuery({
28-
query: GET_FLOW,
29-
variables: { id: flowId },
30-
})
31-
32-
// getFlow requires certain attributes to be returned
33-
const completeCreatedStep = {
34-
...createdStep,
35-
iconUrl: null,
36-
webhookUrl: null,
37-
config: {
38-
stepName: createdStep?.config?.stepName || null,
39-
templateConfig: {
40-
appEventKey: null,
41-
},
42-
approval: {
43-
branch: createdStep?.config?.approval?.branch ?? null,
44-
stepId: createdStep?.config?.approval?.stepId ?? null,
45-
},
46-
},
47-
createdAt: new Date().toISOString(),
48-
}
49-
50-
const steps = flow.steps.reduce((steps: any[], currentStep: any) => {
51-
if (currentStep.id === previousStepId) {
52-
return [...steps, currentStep, completeCreatedStep]
53-
}
54-
55-
return [...steps, currentStep]
56-
}, [])
57-
58-
cache.writeQuery({
59-
query: GET_FLOW,
60-
variables: { id: flowId },
61-
data: { getFlow: { ...flow, steps } },
62-
})
63-
}
64-
}
65-
6620
export default function DuplicateStepButton(props: DuplicateStepButtonProps) {
6721
const { isNested, step } = props
6822

6923
const { flow, isMobile, onDrawerOpen, setCurrentStepId } =
7024
useContext(EditorContext)
7125

72-
const [createStep] = useMutation(CREATE_STEP)
26+
const [createStep] = useMutation(CREATE_STEP, { refetchQueries: [GET_FLOW] })
7327
const onDuplicateStep = useCallback(async () => {
7428
const duplicateConfig = {
7529
...(step.config?.approval && {
@@ -97,7 +51,6 @@ export default function DuplicateStepButton(props: DuplicateStepButtonProps) {
9751

9852
const createdStep = await createStep({
9953
variables: { input: mutationInput },
100-
update: updateHandlerFactory(flow.id, step.id),
10154
})
10255

10356
const newStep = createdStep.data.createStep

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export default function ChooseAndAddConnection(
139139
selectedApp.key,
140140
selectedEvent.key,
141141
connectionId,
142-
{ approval: approvalConfig },
142+
approvalConfig && { approval: approvalConfig },
143143
)
144144
newStepId = createdStep.id
145145
} else if (step) {

packages/frontend/src/components/FlowStepConfigurationModal/ChooseAppAndEvent/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { useQuery } from '@apollo/client'
55

66
import { EditorContext } from '@/contexts/Editor'
77
import { MrfContext } from '@/contexts/MrfContext'
8+
import client from '@/graphql/client'
89
import { GET_APP_CONNECTIONS } from '@/graphql/queries/get-app-connections'
10+
import { GET_FLOW } from '@/graphql/queries/get-flow'
911
import { getMrfApprovalConfig } from '@/helpers/formsg'
1012
import {
1113
TOOLBOX_ACTIONS,
@@ -120,10 +122,11 @@ export default function ChooseAppAndEvent(props: ChooseAppAndEventProps) {
120122
app.key,
121123
triggerOrAction.key,
122124
excelConnection?.id || undefined,
123-
{ approval: approvalConfig },
125+
approvalConfig && { approval: approvalConfig },
124126
)
125127
newStepId = createdStep.id
126128
} else if (step) {
129+
// This part of the code happens when updating an empty step
127130
// account for the if-then edge case
128131
if (
129132
app.key === TOOLBOX_APP_KEY &&
@@ -142,6 +145,8 @@ export default function ChooseAppAndEvent(props: ChooseAppAndEventProps) {
142145
})
143146
newStepId = updatedStep.id
144147
}
148+
// we refetch GET_FLOW after everything is completed
149+
await client.refetchQueries({ include: [GET_FLOW] })
145150
}
146151
onClose()
147152
onDrawerOpen()

packages/frontend/src/graphql/link.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ApolloLink } from '@apollo/client'
22
import { from, HttpLink } from '@apollo/client'
33
import { onError } from '@apollo/client/link/error'
4+
import { removeTypenameFromVariables } from '@apollo/client/link/remove-typename'
45

56
import { INVALID_TILE_VIEW_KEY, NOT_AUTHORISED } from '@/config/errors'
67
import * as URLS from '@/config/urls'
@@ -62,7 +63,13 @@ const createLink = (options: CreateLinkOptions): ApolloLink => {
6263

6364
const httpOptions = { uri, token }
6465

65-
return from([createErrorLink(onError), createHttpLink(httpOptions)])
66+
return from([
67+
// this removes the __typename from variables before sending to the server,
68+
// which prevents validation errors especially when duplicating steps
69+
removeTypenameFromVariables(),
70+
createErrorLink(onError),
71+
createHttpLink(httpOptions),
72+
])
6673
}
6774

6875
export default createLink

packages/frontend/src/graphql/mutations/create-step.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const CREATE_STEP = gql`
1111
position
1212
status
1313
flowId
14+
createdAt
1415
connection {
1516
id
1617
}

packages/frontend/src/graphql/mutations/update-step.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const UPDATE_STEP = graphql(`
1212
status
1313
position
1414
flowId
15+
createdAt
1516
connection {
1617
id
1718
}

packages/frontend/src/helpers/formsg.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ export function getMrfApprovalConfig({
2929
previousStep.config?.approval?.branch &&
3030
previousStep.config?.approval?.stepId
3131
) {
32-
return {
33-
branch: previousStep.config?.approval?.branch,
34-
stepId: previousStep.config?.approval?.stepId,
35-
}
32+
return previousStep.config?.approval
3633
}
3734

3835
/**

packages/frontend/src/helpers/toolbox.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import { useCallback, useContext, useState } from 'react'
44
import { useMutation } from '@apollo/client'
55

66
import { BranchContext } from '@/components/FlowStepGroup/Content/IfThen/BranchContext'
7-
import client from '@/graphql/client'
87
import { CREATE_STEP } from '@/graphql/mutations/create-step'
98
import { UPDATE_STEP } from '@/graphql/mutations/update-step'
10-
import { GET_FLOW } from '@/graphql/queries/get-flow'
119

1210
export const TOOLBOX_APP_KEY = 'toolbox'
1311

@@ -241,10 +239,10 @@ export function useIfThenInitializer(): [
241239
},
242240
})
243241

244-
// Refetch only after completion of all initialization steps.
245-
await client.refetchQueries({ include: [GET_FLOW] })
246-
247242
setIsInitializing(false)
243+
244+
// we dont refetch GET_FLOW here but leave it to the caller to refetch
245+
248246
return currStep
249247
},
250248
[createStep, depth, updateStep],

0 commit comments

Comments
 (0)