-
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
feat: Data value search #3646
Merged
Merged
feat: Data value search #3646
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5e02b80
chore: Update folder structure
DafyddLlyr 39fb9c2
feat: Use a map data structure to hold dispay data for search cards, …
DafyddLlyr 20f4572
test: Setup todos
DafyddLlyr 790d9ef
test: Coverage for all exceptional node types
DafyddLlyr 391b4b8
chore: Drop filter which hid excpetional nodes
DafyddLlyr 30ea8e8
chore: Slightly better types
DafyddLlyr 7234ca8
fix: Add temporary type guard
DafyddLlyr 78be419
chore: Drop type guard after TS bump
DafyddLlyr 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
126 changes: 126 additions & 0 deletions
126
...uk/src/pages/FlowEditor/components/Sidebar/Search/SearchResultCard/DataDisplayMap.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,126 @@ | ||
import { ComponentType } from "@opensystemslab/planx-core/types"; | ||
import { useStore } from "pages/FlowEditor/lib/store"; | ||
|
||
import { | ||
mockAnswerResult, | ||
mockCalculateFormulaResult, | ||
mockCalculateRootResult, | ||
mockFileUploadAndLabelResult, | ||
mockFlow, | ||
mockListAnswerResult, | ||
mockListDataResult, | ||
mockListRootResult, | ||
mockQuestionResult, | ||
} from "../mocks/DataDisplayMap"; | ||
import { getDisplayDetailsForResult } from "./DataDisplayMap"; | ||
|
||
type Output = ReturnType<typeof getDisplayDetailsForResult>; | ||
|
||
// Setup flow so that it can be referenced by SearchResults (e.g. getting parent nodes) | ||
beforeAll(() => useStore.setState({ flow: mockFlow })); | ||
|
||
describe("Question component", () => { | ||
it("returns the expected display values", () => { | ||
const output = getDisplayDetailsForResult(mockQuestionResult); | ||
|
||
expect(output).toStrictEqual<Output>({ | ||
key: "Data", | ||
iconKey: ComponentType.Question, | ||
componentType: "Question", | ||
title: "This is a question component", | ||
headline: "colour", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("Answer component", () => { | ||
it("returns the expected display values", () => { | ||
const output = getDisplayDetailsForResult(mockAnswerResult); | ||
|
||
expect(output).toStrictEqual<Output>({ | ||
key: "Option (data)", | ||
iconKey: ComponentType.Question, | ||
componentType: "Question", | ||
title: "This is a question component", | ||
headline: "red", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("List component", () => { | ||
it("handles the root data value", () => { | ||
const output = getDisplayDetailsForResult(mockListRootResult); | ||
|
||
expect(output).toStrictEqual<Output>({ | ||
componentType: "List", | ||
headline: "listRoot", | ||
iconKey: ComponentType.List, | ||
key: "Data", | ||
title: "This is a list component", | ||
}); | ||
}); | ||
|
||
it("handles nested data variables", () => { | ||
const output = getDisplayDetailsForResult(mockListDataResult); | ||
|
||
expect(output).toStrictEqual<Output>({ | ||
componentType: "List", | ||
headline: "tenure", | ||
iconKey: ComponentType.List, | ||
key: "Data", | ||
title: "This is a list component", | ||
}); | ||
}); | ||
|
||
it("handles nested data variables in Answers", () => { | ||
const output = getDisplayDetailsForResult(mockListAnswerResult); | ||
|
||
expect(output).toStrictEqual<Output>({ | ||
componentType: "List", | ||
headline: "selfCustomBuild", | ||
iconKey: ComponentType.List, | ||
key: "Option (data)", | ||
title: "This is a list component", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("Calculate component", () => { | ||
it("handles the output data variables", () => { | ||
const output = getDisplayDetailsForResult(mockCalculateRootResult); | ||
|
||
expect(output).toStrictEqual<Output>({ | ||
componentType: "Calculate", | ||
headline: "calculateOutput", | ||
iconKey: ComponentType.Calculate, | ||
key: "Output (data)", | ||
title: "This is a calculate component", | ||
}); | ||
}); | ||
|
||
it("handles the formula data variables", () => { | ||
const output = getDisplayDetailsForResult(mockCalculateFormulaResult); | ||
|
||
expect(output).toStrictEqual<Output>({ | ||
componentType: "Calculate", | ||
headline: "formulaOne + formulaTwo", | ||
iconKey: ComponentType.Calculate, | ||
key: "Formula", | ||
title: "This is a calculate component", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("FileUploadAndLabel component", () => { | ||
it("handles the data variables nested in FileTypes", () => { | ||
const output = getDisplayDetailsForResult(mockFileUploadAndLabelResult); | ||
|
||
expect(output).toStrictEqual<Output>({ | ||
componentType: "File upload and label", | ||
headline: "floorplan", | ||
iconKey: ComponentType.FileUploadAndLabel, | ||
key: "File type (data)", | ||
title: "This is a FileUploadAndLabel component", | ||
}); | ||
}); | ||
}); |
116 changes: 116 additions & 0 deletions
116
...anx.uk/src/pages/FlowEditor/components/Sidebar/Search/SearchResultCard/DataDisplayMap.tsx
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,116 @@ | ||
import { ComponentType, IndexedNode } from "@opensystemslab/planx-core/types"; | ||
import { Calculate } from "@planx/components/Calculate/model"; | ||
import { FileUploadAndLabel } from "@planx/components/FileUploadAndLabel/model"; | ||
import { List } from "@planx/components/List/model"; | ||
import { SearchResult } from "hooks/useSearch"; | ||
import { capitalize, get } from "lodash"; | ||
import { SLUGS } from "pages/FlowEditor/data/types"; | ||
import { useStore } from "pages/FlowEditor/lib/store"; | ||
|
||
interface DataDisplayValues { | ||
displayKey: string; | ||
getIconKey: (result: SearchResult<IndexedNode>) => ComponentType; | ||
getTitle: (result: SearchResult<IndexedNode>) => string; | ||
getHeadline: (result: SearchResult<IndexedNode>) => string; | ||
getComponentType: (result: SearchResult<IndexedNode>) => string; | ||
} | ||
|
||
/** | ||
* Map of data keys to their associated display values | ||
* Uses Partial<DataDisplayValues> as not all values are unique, we later apply defaults | ||
*/ | ||
type DataKeyMap = Record<string, Partial<DataDisplayValues>>; | ||
|
||
/** | ||
* Map of ComponentTypes to their associated data keys | ||
*/ | ||
type ComponentMap = Record<ComponentType, DataKeyMap>; | ||
|
||
/** | ||
* Map of ComponentTypes which need specific overrides in order to display their data values | ||
*/ | ||
const DISPLAY_DATA: Partial<ComponentMap> = { | ||
// Answers are mapped to their parent questions | ||
[ComponentType.Answer]: { | ||
default: { | ||
getIconKey: () => ComponentType.Question, | ||
displayKey: "Option (data)", | ||
getTitle: ({ item }) => { | ||
const parentNode = useStore.getState().flow[item.parentId]; | ||
return parentNode.data.text; | ||
}, | ||
getHeadline: ({ item, key }) => get(item, key)?.toString(), | ||
}, | ||
}, | ||
// FileUploadAndLabel has data values nested in FileTypes | ||
[ComponentType.FileUploadAndLabel]: { | ||
default: { | ||
displayKey: "File type (data)", | ||
getHeadline: ({ item, refIndex }) => | ||
(item["data"] as unknown as FileUploadAndLabel)["fileTypes"][refIndex][ | ||
"fn" | ||
], | ||
}, | ||
}, | ||
// Calculate contains both input and output data values | ||
[ComponentType.Calculate]: { | ||
formula: { | ||
displayKey: "Formula", | ||
getHeadline: ({ item }) => (item.data as unknown as Calculate).formula, | ||
}, | ||
"data.output": { | ||
displayKey: "Output (data)", | ||
getHeadline: ({ item }) => (item.data as unknown as Calculate).output, | ||
}, | ||
}, | ||
// List contains data variables nested within its schema | ||
[ComponentType.List]: { | ||
"data.schema.fields.data.fn": { | ||
getHeadline: ({ item, refIndex }) => | ||
(item.data as unknown as List).schema.fields[refIndex].data.fn, | ||
}, | ||
"data.schema.fields.data.options.data.val": { | ||
displayKey: "Option (data)", | ||
getHeadline: ({ item, refIndex }) => { | ||
// Fuse.js flattens deeply nested arrays when using refIndex | ||
const options = (item.data as unknown as List).schema.fields | ||
.filter((field) => field.type === "question") | ||
.flatMap((field) => field.data.options); | ||
return options[refIndex].data.val || ""; | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
/** | ||
* Default values for all ComponentTypes not listed in DISPLAY_DATA | ||
*/ | ||
const DEFAULT_DISPLAY_DATA: DataDisplayValues = { | ||
displayKey: "Data", | ||
getIconKey: ({ item }) => item.type, | ||
getTitle: ({ item }) => | ||
(item.data?.title as string) || (item.data?.text as string) || "", | ||
getHeadline: ({ item, key }) => get(item, key)?.toString() || "", | ||
getComponentType: ({ item }) => | ||
capitalize(SLUGS[item.type].replaceAll("-", " ")), | ||
}; | ||
|
||
export const getDisplayDetailsForResult = ( | ||
result: SearchResult<IndexedNode>, | ||
) => { | ||
const componentMap = DISPLAY_DATA[result.item.type]; | ||
const keyMap = componentMap?.[result.key] || componentMap?.default || {}; | ||
|
||
const data: DataDisplayValues = { | ||
...DEFAULT_DISPLAY_DATA, | ||
...keyMap, | ||
}; | ||
|
||
return { | ||
iconKey: data.getIconKey(result), | ||
componentType: data.getComponentType(result), | ||
title: data.getTitle(result), | ||
key: data.displayKey, | ||
headline: data.getHeadline(result), | ||
}; | ||
}; |
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.
There's a fair amount of type casting in this data structure as
IndexedNode
(and our flow as a whole) are not strongly typed.We can't use a type guard as the types of
IndexedNode["data"]
and various components have no overlap - we hit the"A type predicate's type must be assignable to its parameter's type"
error.This means that casting is unfortunately necessary, but it's better than using
any
or just optional chaining the whole way down.