Skip to content

Commit 1e1f65e

Browse files
committed
fix(ui): delete nested children when disabling a child
1 parent 2475c44 commit 1e1f65e

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

ui/src/singletons/store/appActions.test.ts

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const STANDARD_INBOUND_ADAPTER = "9KWCqlIyy7"
5757
const STANDARD_ROUTER = "LoiC2CFbLP"
5858
const STANDARD_POLLER_CHILD = "mcyTryMPewJ"
5959
const STANDARD_TRANSACTIONAL_CHILD = "V1ls9ri4szs"
60+
const NESTED_CHILD_PARENT_ID = "FL5Tssm8tV"
6061

6162
beforeEach(() => {
6263
act(() => {
@@ -423,6 +424,28 @@ describe("disable child", () => {
423424
)
424425

425426
expect(children).toHaveLength(0)
427+
expect(getEipId(STANDARD_POLLER_CHILD)).toBeUndefined()
428+
})
429+
430+
test("disable child deletes all nested children", () => {
431+
act(() => resetMockStore(nestedChildFlow))
432+
433+
const initChildren = renderAndUnwrapHook(() =>
434+
useGetEnabledChildren(NESTED_CHILD_PARENT_ID)
435+
)
436+
expect(initChildren).toHaveLength(3)
437+
438+
const topChildId = "43FAdk5SdBR"
439+
act(() => disableChild(NESTED_CHILD_PARENT_ID, topChildId))
440+
441+
const updatedChildren = renderAndUnwrapHook(() =>
442+
useGetEnabledChildren(NESTED_CHILD_PARENT_ID)
443+
)
444+
expect(updatedChildren).toHaveLength(initChildren.length - 1)
445+
446+
expect(getEipId(topChildId)).toBeUndefined()
447+
expect(getEipId("9OymVfY4n4p")).toBeUndefined()
448+
expect(getEipId("gCL0YkrFGgW")).toBeUndefined()
426449
})
427450

428451
test("disable child unknown child id -> error", () => {
@@ -455,10 +478,10 @@ describe("reorder enabled child list", () => {
455478
})
456479
})
457480

458-
const PARENT_ID = "FL5Tssm8tV"
459-
460481
test("reorder enabled children success", () => {
461-
const initial = renderAndUnwrapHook(() => useGetEnabledChildren(PARENT_ID))
482+
const initial = renderAndUnwrapHook(() =>
483+
useGetEnabledChildren(NESTED_CHILD_PARENT_ID)
484+
)
462485

463486
const updated = [...initial]
464487

@@ -467,26 +490,38 @@ describe("reorder enabled child list", () => {
467490
updated[2] = updated[0]
468491
updated[0] = temp
469492

470-
act(() => reorderEnabledChildren(PARENT_ID, updated))
493+
act(() => reorderEnabledChildren(NESTED_CHILD_PARENT_ID, updated))
471494

472-
const children = renderAndUnwrapHook(() => useGetEnabledChildren(PARENT_ID))
495+
const children = renderAndUnwrapHook(() =>
496+
useGetEnabledChildren(NESTED_CHILD_PARENT_ID)
497+
)
473498
expect(children).toEqual(updated)
474499
})
475500

476501
test("adding child -> error ", () => {
477-
const initial = renderAndUnwrapHook(() => useGetEnabledChildren(PARENT_ID))
502+
const initial = renderAndUnwrapHook(() =>
503+
useGetEnabledChildren(NESTED_CHILD_PARENT_ID)
504+
)
478505
const updated = [...initial, "extra"]
479-
expect(() => reorderEnabledChildren(PARENT_ID, updated)).toThrowError()
506+
expect(() =>
507+
reorderEnabledChildren(NESTED_CHILD_PARENT_ID, updated)
508+
).toThrowError()
480509
})
481510

482511
test("removing child -> error ", () => {
483-
const initial = renderAndUnwrapHook(() => useGetEnabledChildren(PARENT_ID))
512+
const initial = renderAndUnwrapHook(() =>
513+
useGetEnabledChildren(NESTED_CHILD_PARENT_ID)
514+
)
484515
const updated = initial.slice(1)
485-
expect(() => reorderEnabledChildren(PARENT_ID, updated)).toThrowError()
516+
expect(() =>
517+
reorderEnabledChildren(NESTED_CHILD_PARENT_ID, updated)
518+
).toThrowError()
486519
})
487520

488521
test("changing child list -> error ", () => {
489-
expect(() => reorderEnabledChildren(PARENT_ID, ["c1", "c2"])).toThrowError()
522+
expect(() =>
523+
reorderEnabledChildren(NESTED_CHILD_PARENT_ID, ["c1", "c2"])
524+
).toThrowError()
490525
})
491526
})
492527

ui/src/singletons/store/appActions.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { EipId } from "../../api/generated/eipFlow"
1414
import { newFlowLayout } from "../../components/layout/layouting"
1515
import { AppStore, EipConfig, SerializedFlow } from "./api"
1616
import { useAppStore } from "./appStore"
17+
import { childrenDepthTraversal } from "./storeViews"
1718

1819
export const createDroppedNode = (eipId: EipId, position: XYPosition) =>
1920
useAppStore.setState((state) => {
@@ -127,20 +128,30 @@ export const enableChild = (parentId: string, childEipId: EipId) =>
127128

128129
export const disableChild = (parentId: string, childId: string) =>
129130
useAppStore.setState((state) => {
130-
const idx = state.eipConfigs[parentId].children.findIndex(
131-
(id) => id === childId
132-
)
131+
const parentConfig = state.eipConfigs[parentId]
132+
const idx = parentConfig.children.findIndex((id) => id === childId)
133133

134134
if (idx === -1) {
135135
throw new Error(
136136
`Node (id: ${parentId}) did not have child with id: ${childId}`
137137
)
138138
}
139139

140-
return produce(state, (draft: AppStore) => {
141-
draft.eipConfigs[parentId].children.splice(idx, 1)
142-
delete draft.eipConfigs[childId]
143-
})
140+
// remove target child from parent's children list
141+
const updatedConfigs = {
142+
...state.eipConfigs,
143+
[parentId]: {
144+
...parentConfig,
145+
children: parentConfig.children.filter((c) => c !== childId),
146+
},
147+
}
148+
149+
// delete target child and its nested children
150+
for (const child of childrenDepthTraversal(childId)) {
151+
delete updatedConfigs[child.id]
152+
}
153+
154+
return { eipConfigs: updatedConfigs }
144155
})
145156

146157
export const reorderEnabledChildren = (parentId: string, children: string[]) =>

0 commit comments

Comments
 (0)