-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Handle multiple external portals as siblings #2217
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,28 +20,44 @@ const getFlowData = async (id: string) => { | |
return data.flows_by_pk; | ||
}; | ||
|
||
export const dataMerged = async ( | ||
id: string, | ||
ob: Record<string, any> = {}, | ||
): Promise<Record<string, any>> => { | ||
const { slug, data }: { slug: string; data: Record<string, any> } = | ||
await getFlowData(id); | ||
// Flatten a flow's data to include main content & portals in a single JSON representation | ||
// XXX: getFlowData & dataMerged are currently repeated in api.planx.uk/helpers.ts | ||
// in order to load frontend /preview routes for flows that are not published | ||
export const dataMerged = async (id: string, ob: Record<string, any> = {}) => { | ||
// get the primary flow data | ||
const { slug, data }: { slug: string; data: Record<string, any> } = await getFlowData(id); | ||
|
||
// recursively get and flatten internal portals & external portals | ||
for (const [nodeId, node] of Object.entries(data)) { | ||
if (nodeId === "_root" && Object.keys(ob).length > 0) { | ||
const isExternalPortalRoot = nodeId === "_root" && Object.keys(ob).length > 0; | ||
const isExternalPortal = node.type === TYPES.ExternalPortal; | ||
const isMerged = ob[node.data?.flowId]; | ||
|
||
// Merge portal root as a new node in the graph | ||
if (isExternalPortalRoot) { | ||
ob[id] = { | ||
...node, | ||
type: TYPES.InternalPortal, | ||
data: { text: slug }, | ||
}; | ||
} else if (node.type === TYPES.ExternalPortal && !ob[node.data.flowId]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a little rearranging / renaming here but breaking up this condition is what's fixed the bug. As is, this flattens the first external portal it hits, but skips subsequent ones. |
||
await dataMerged(node.data.flowId, ob); | ||
} | ||
|
||
// Merge as internal portal, with reference to flowId | ||
else if (isExternalPortal) { | ||
ob[nodeId] = { | ||
type: TYPES.InternalPortal, | ||
edges: [node.data.flowId], | ||
edges: [node.data?.flowId], | ||
}; | ||
} else { | ||
ob[nodeId] = node; | ||
|
||
// Recursively merge flow | ||
if (!isMerged) { | ||
await dataMerged(node.data?.flowId, ob); | ||
} | ||
} | ||
|
||
// Merge all other nodes | ||
else ob[nodeId] = node; | ||
} | ||
|
||
return ob; | ||
}; |
63 changes: 63 additions & 0 deletions
63
editor.planx.uk/src/pages/FlowEditor/lib/__tests__/externalPortals.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { FullStore, vanillaStore } from "../store"; | ||
import multipleExternalPortals from "./mocks/multipleExternalPortals.json"; | ||
import singleExternalPortal from "./mocks/singleExternalPortal.json"; | ||
|
||
const { getState, setState } = vanillaStore; | ||
const { upcomingCardIds, record } = getState(); | ||
|
||
let initialState: FullStore; | ||
|
||
beforeEach(() => { | ||
initialState = getState(); | ||
}); | ||
|
||
describe("A flow with a single external portal can be navigated as expected", () => { | ||
beforeEach(() => setState({ flow: singleExternalPortal })); | ||
afterEach(() => setState(initialState)); | ||
|
||
it("without entering the portal", () => { | ||
expect(upcomingCardIds()[0]).toEqual("firstNode"); | ||
// Navigate down branch avoiding external portal | ||
record("firstNode", { answers: ["option2"] }); | ||
expect(upcomingCardIds()[0]).toEqual("finalNode"); | ||
}); | ||
|
||
it("via the portal", () => { | ||
expect(upcomingCardIds()[0]).toEqual("firstNode"); | ||
// Navigate down branch via external portal | ||
record("firstNode", { answers: ["option1"] }); | ||
expect(upcomingCardIds()[0]).toEqual("withinExternalPortal"); | ||
record("withinExternalPortal", { answers: [] }); | ||
expect(upcomingCardIds()[0]).toEqual("finalNode"); | ||
}); | ||
}); | ||
|
||
describe("A flow with repeated external portals can be navigated as expected", () => { | ||
beforeEach(() => setState({ flow: multipleExternalPortals })); | ||
afterEach(() => setState(initialState)); | ||
|
||
it("without entering the portal", () => { | ||
expect(upcomingCardIds()[0]).toEqual("firstNode"); | ||
// Navigate down branch avoiding external portal | ||
record("firstNode", { answers: ["option3"] }); | ||
expect(upcomingCardIds()[0]).toEqual("finalNode"); | ||
}); | ||
|
||
it("via the first portal", () => { | ||
expect(upcomingCardIds()[0]).toEqual("firstNode"); | ||
// Navigate down branch via first external portal | ||
record("firstNode", { answers: ["option1"] }); | ||
expect(upcomingCardIds()[0]).toEqual("withinExternalPortal"); | ||
record("withinExternalPortal", { answers: [] }); | ||
expect(upcomingCardIds()[0]).toEqual("finalNode"); | ||
}); | ||
|
||
it("via the second portal", () => { | ||
expect(upcomingCardIds()[0]).toEqual("firstNode"); | ||
// Navigate down branch via second external portal | ||
record("firstNode", { answers: ["option2"] }); | ||
expect(upcomingCardIds()[0]).toEqual("withinExternalPortal"); | ||
record("withinExternalPortal", { answers: [] }); | ||
expect(upcomingCardIds()[0]).toEqual("finalNode"); | ||
}); | ||
}); |
81 changes: 81 additions & 0 deletions
81
editor.planx.uk/src/pages/FlowEditor/lib/__tests__/mocks/multipleExternalPortals.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
{ | ||
"_root": { | ||
"edges": [ | ||
"firstNode", | ||
"finalNode" | ||
] | ||
}, | ||
"externalPortal1": { | ||
"type": 300, | ||
"edges": [ | ||
"externalFlowId" | ||
] | ||
}, | ||
"option3": { | ||
"data": { | ||
"text": "Option 3" | ||
}, | ||
"type": 200 | ||
}, | ||
"externalPortal2": { | ||
"type": 300, | ||
"edges": [ | ||
"externalFlowId" | ||
] | ||
}, | ||
"firstNode": { | ||
"data": { | ||
"text": "This is a question" | ||
}, | ||
"type": 100, | ||
"edges": [ | ||
"option1", | ||
"option2", | ||
"option3" | ||
] | ||
}, | ||
"option2": { | ||
"data": { | ||
"text": "Option 2" | ||
}, | ||
"type": 200, | ||
"edges": [ | ||
"externalPortal2" | ||
] | ||
}, | ||
"finalNode": { | ||
"data": { | ||
"color": "#EFEFEF", | ||
"title": "This is the end", | ||
"resetButton": false | ||
}, | ||
"type": 8 | ||
}, | ||
"withinExternalPortal": { | ||
"data": { | ||
"color": "#EFEFEF", | ||
"title": "This is inside the portal", | ||
"description": "<p>Hello there 👋</p>", | ||
"resetButton": false | ||
}, | ||
"type": 8 | ||
}, | ||
"option1": { | ||
"data": { | ||
"text": "Option 1" | ||
}, | ||
"type": 200, | ||
"edges": [ | ||
"externalPortal1" | ||
] | ||
}, | ||
"externalFlowId": { | ||
"data": { | ||
"text": "daf-external-portal-test" | ||
}, | ||
"type": 300, | ||
"edges": [ | ||
"withinExternalPortal" | ||
] | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: is
isNotMerged
a more clear name here sincenode.data?.flowId
indicates it's still an external portal reference, not flattened representation of individual nodes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So here
ob
is the returned item (the flattened flow). Ifob[node.data?.flowId] == true
then the flow has already been flattened andob[example-uuid-of-portal]
exists. Does that make sense?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH I think some of the language here is a little fuzzy around merging / flattening