Skip to content
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

SAGE-283-allergy-reaction #56

Merged
merged 13 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"command": "npm run dev",
"name": "npm run dev",
"request": "launch",
"type": "node-terminal"
},
{
"type": "firefox",
"request": "launch",
"reAttach": true,
"name": "Launch localhost",
"name": "ffLaunch",
"url": "http://localhost:8083",
"pathMappings": [
{
Expand All @@ -17,5 +23,12 @@
}
]
}
]
],
"compounds": [
{
"name": "Debug",
"configurations": ["npm run dev", "ffLaunch"],
"stopAll": true
}
]
}
2 changes: 1 addition & 1 deletion friendly-names.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
},
{
"SELF": {
"FHIR": "productReference", "FRIENDLY": "Medication Code: "}
"FHIR": "productCodeableConcept", "FRIENDLY": "Medication Code: "}
},
{
"SELF": {
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@types/fhir": "0.0.34",
"@types/jest": "^27.0.2",
"@types/jquery": "^3.5.8",
"@types/lodash": "^4.14.180",
"@types/react": "^16.14.0",
"@types/react-dates": "^21.8.3",
"@types/react-dom": "^16.9.14",
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/schema-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type SageNewResource = {

// type fhirTypeValues = "decimal" | "boolean" | "xhtml" | "base64Binary" | "code" | "uri" | "canonical";

type ProfileDefs = {
export type ProfileDefs = {
'__meta': {
baseDefinition: string,
id: string,
Expand Down
73 changes: 64 additions & 9 deletions src/simplified/cardEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import * as cql from "cql-execution";
import { Library, Dosage, PlanDefinition, PlanDefinitionActionCondition } from "fhir/r4";
import { ExtractTypeOfFN } from "freezer-js";
import { Library, Dosage, PlanDefinition, PlanDefinitionActionCondition, CodeableConcept } from "fhir/r4";
import React, { ElementType, useEffect, useState } from "react";
import { Button, Col, Form, InputGroup, Modal, Row } from 'react-bootstrap';
import hypertensionLibraryJson from "../../public/samples/hypertension-library.json";
import * as SageUtils from "../helpers/sage-utils";
import * as SchemaUtils from "../helpers/schema-utils";
import State, { SageNodeInitializedFreezerNode } from "../state";
import { OuterCardForm, textBoxProps, cardLayout, displayBoxProps } from "./outerCardForm";
Expand All @@ -14,11 +10,9 @@ import { buildEditableStateFromCondition, WizardState } from "./cql-wizard/wizar
import { generateCqlFromConditions, makeCQLtoELMRequest } from "./cql-wizard/cql-generator";
import { buildFredId } from "../helpers/bundle-utils";
import { fhirToFriendly } from '../simplified/nameHelpers';
import CodeableConceptEditor, { CodeableConceptEditorProps } from "./codeableConceptEditor";



const hypertensionLibrary: Library = hypertensionLibraryJson as Library;

interface ExpressionOptionDict {
[expression: string]: ExpressionOption // The key is exactly what's written in the Condition's "expression" element
}
Expand Down Expand Up @@ -50,6 +44,7 @@ export interface ICardForm {
textBoxFields: Map<string, textBoxProps>;
displayBoxFields: Map<string, displayBoxProps>;
dropdownFields: Map<string, string[]>;
codeableConceptFields: Map<string, Partial<CodeableConceptEditorProps>>;
resourceFields: string[];
cardFieldLayout: cardLayout;
pageOne: React.FunctionComponent<pageOneProps> | React.ComponentClass<pageOneProps>;
Expand All @@ -72,6 +67,42 @@ const simpleCardField = (fieldName: string, actNode: SageNodeInitializedFreezerN
return [fieldName, fieldContents, setField, fieldSaveHandler]
}

/**
* CodeableConcept as defined here https://www.hl7.org/fhir/datatypes.html#CodeableConcept
* @param fieldName Name of element in `actNode` that is of type CodeableConcept
* @param parentNode Node of parent element to `fieldName`
* @returns \{ Name of field, Contents of field, (function) Set contents of field, (function) Save contents back into the SAGE node \}
*/
const codeableConceptCardField = (fieldName: string, parentNode: SageNodeInitializedFreezerNode) => {
const fieldNode = SchemaUtils.getChildOfNodePath(parentNode, [fieldName]);

// Normally dangerous
// eslint-disable-next-line react-hooks/rules-of-hooks
const [codeableConcept, setCodeableConcept] = useState<CodeableConcept>(() => {
if (fieldNode) {
// Initialize with existing value
return SchemaUtils.toFhir(fieldNode, false)
}
return {}
});

function codeableConceptSaveHandler(name: string, contents: any, act: any, plan: any) {
if (fieldNode) {
State.emit("load_json_into", fieldNode, codeableConcept);
}
else {
State.emit("load_json_into", parentNode, {
[fieldName]: codeableConcept
});
}
}
return {
fieldName,
codeableConcept,
setCodeableConcept,
codeableConceptSaveHandler
};
}

const createTextBoxElement = (fieldKey: string, friendlyFieldName: string, textProps: textBoxProps, fieldHandlers: any[][], node: SageNodeInitializedFreezerNode): JSX.Element => {
const [fieldName, fieldContents, setField, fieldSaveHandler] = simpleCardField(fieldKey, node);
Expand Down Expand Up @@ -145,6 +176,21 @@ const createDropdownElement = (fieldKey: string, fieldFriendlyName: string, fiel
);
}

const createCodeableConceptElement = (fieldKey: string, fieldFriendlyName: string, codeableConceptEditorPropsOverrides: Partial<CodeableConceptEditorProps>, fieldHandlers: any[][], node: SageNodeInitializedFreezerNode): JSX.Element => {
const { fieldName, codeableConcept, setCodeableConcept, codeableConceptSaveHandler } = codeableConceptCardField(fieldKey, node);
fieldHandlers.push([fieldName, codeableConcept, setCodeableConcept, codeableConceptSaveHandler]);
return (
<Form.Group key={fieldName + "-fromGroup"} as={Col} controlId={fieldKey}>
<Row className="page1-row-element">
<Form.Label key={fieldName + "-label"}>{fieldFriendlyName}</Form.Label>
<Col key={fieldName + "-col"} className = 'page1-input-fields'>
<CodeableConceptEditor {...codeableConceptEditorPropsOverrides} curCodeableConcept={codeableConcept} setCurCodeableConcept={setCodeableConcept} />
</Col>
</Row>
</Form.Group>
);
}

const createDisplayElement = ( displayProps: displayBoxProps,friendlyFields: any,fieldHandlers: any, i: number): JSX.Element => {
let friendly;
for (let j = 0; j < friendlyFields.length; j++) {
Expand Down Expand Up @@ -201,11 +247,20 @@ const createDropdownElementList = (innerCardForm: ICardForm, friendlyFields: Fri
})
}

const createCodeableConceptElementList = (innerCardForm: ICardForm, friendlyFields: FriendlyResourceFormElement[], fieldHandlers: any, node: SageNodeInitializedFreezerNode): JSX.Element[] => {
return friendlyFields
.filter(ff => innerCardForm.codeableConceptFields.has(ff.SELF.FHIR))
.map(ff => {
return createCodeableConceptElement(ff.SELF.FHIR, ff.SELF.FRIENDLY, innerCardForm.codeableConceptFields.get(ff.SELF.FHIR) ?? {}, fieldHandlers, node)
})
}

const fieldElementListForType = (innerCardForm: ICardForm, friendlyFields: FriendlyResourceFormElement[], fieldHandlers: any, node: SageNodeInitializedFreezerNode): JSX.Element[] => {
const flattenFriendlyFields = allFormElems(friendlyFields);
return [
...createTextBoxElementList(innerCardForm, flattenFriendlyFields, fieldHandlers, node),
...createDropdownElementList(innerCardForm, flattenFriendlyFields, fieldHandlers, node)
...createDropdownElementList(innerCardForm, flattenFriendlyFields, fieldHandlers, node),
...createCodeableConceptElementList(innerCardForm, flattenFriendlyFields, fieldHandlers, node),
]
}

Expand Down
49 changes: 49 additions & 0 deletions src/simplified/codeableConceptEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import AsyncSelect from "react-select/async";
import { SageCoding } from "./cql-wizard/wizardLogic";
import * as Bioportal from './cql-wizard/bioportal';
import _ from "lodash"
import { CodeableConcept, Coding } from "fhir/r4";

function loadCodes(inputValue: string, callback: (results: SageCoding[]) => void) {
Bioportal.searchForText(inputValue).then(v => callback(v));
}
const debouncedLoadCodes = _.debounce(loadCodes, 500)

export interface CodeableConceptEditorProps {
curCodeableConcept: CodeableConcept,
setCurCodeableConcept: (newCodeableConcept: CodeableConcept) => void,
codeFilter?: string,
}

const CodeableConceptEditor: React.FC<CodeableConceptEditorProps> = (props: CodeableConceptEditorProps) => {
const selectedCode: Coding | undefined = props.curCodeableConcept.coding?.[0];
function createCodeableConceptFromCode(code: Coding | null): CodeableConcept {
return {
coding: code ? [code] : undefined
}
}

return (
<div>
<AsyncSelect<Coding>
loadOptions={debouncedLoadCodes}
value={selectedCode}
noOptionsMessage={input => input.inputValue !== "" ? `No code found for ${input.inputValue}` : `Please enter a code or the name of a code`}
isClearable={true}
onChange={newCode => props.setCurCodeableConcept(createCodeableConceptFromCode(newCode))}
formatOptionLabel={code => {
return (
<div>
<b>{code.display ?? "Unknown Code Name"}</b>
<br />
{code.code} <i>{(code.system && Bioportal.systemUrlToOntology[code.system]) ?? code.system}</i>
</div>
)
}}
isOptionSelected={option => option.code === selectedCode?.code && option.system === selectedCode?.system && option.version === selectedCode?._version}
/>
</div>
)
}

export default CodeableConceptEditor;
5 changes: 5 additions & 0 deletions src/simplified/cql-wizard/bioportal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from "axios";
import State from "../../state";
import { SageCoding } from "./wizardLogic";

// These ontology names are from Bioportal
export const ontologyToSystemAndVersion: {[key: string]: {system: string, version: string} | undefined} = {
'SNOMEDCT': {
system: "http://snomed.info/sct",
Expand Down Expand Up @@ -29,6 +30,10 @@ export const ontologyToSystemAndVersion: {[key: string]: {system: string, versio
},
};

// Map urls back to their name on Bioportal
export const systemUrlToOntology: {[systemUrl: string]: string | undefined} = {};
Object.entries(ontologyToSystemAndVersion).forEach(v => v[1] ? (systemUrlToOntology[v[1].system] = v[0]) : null)

/**
* Search Bioportal for codes matching the query
* @param text Text to search for
Expand Down
3 changes: 3 additions & 0 deletions src/simplified/cql-wizard/cql-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ export async function generateCqlFromConditions(conditionStates: EditableStateFo

case "unknown":
return null;

default:
return null;
}
}
const clauses = conditionState.curWizState.filters.flatMap(filter => {
Expand Down
Loading