Skip to content

Commit

Permalink
update Summary.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Amy Chen authored and Amy Chen committed Aug 1, 2022
1 parent ac8f93b commit 68ca112
Showing 1 changed file with 120 additions and 111 deletions.
231 changes: 120 additions & 111 deletions src/components/Summary.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,134 @@
import React from 'react';
import Worker from 'cql-worker/src/cql.worker.js'; // https://github.com/webpack-contrib/worker-loader
import { initialzieCqlWorker } from 'cql-worker';
import { FhirClientContext } from '../FhirClientContext';
import Error from './Error';
import React from "react";
import Worker from "cql-worker/src/cql.worker.js"; // https://github.com/webpack-contrib/worker-loader
import { initialzieCqlWorker } from "cql-worker";
import { FhirClientContext } from "../FhirClientContext";
import Error from "./Error";
import {
getFHIRResourcePaths,
getExpressionLogicLib,
queryPatientIdKey} from '../util/util';
getFHIRResourcePaths,
getExpressionLogicLib,
queryPatientIdKey,
} from "../util/util";

export default function Summary() {

// Define a web worker for evaluating CQL expressions
const cqlWorker = new Worker();
// Initialize the cql-worker
let [setupExecution, sendPatientBundle, evaluateExpression] = initialzieCqlWorker(cqlWorker);
let patientBundle = {
resourceType: "Bundle",
id: "resource-bundle",
type: "collection",
entry: [],
};
// Define a web worker for evaluating CQL expressions
const cqlWorker = new Worker();
// Initialize the cql-worker
let [setupExecution, sendPatientBundle, evaluateExpression] =
initialzieCqlWorker(cqlWorker);

const contextContent = React.useContext(FhirClientContext);
const [patient, setPatient] = React.useState(null);
const [error, setError] = React.useState(null);
const getCQLEvaluations = () => {
//get CQL expression lib
getExpressionLogicLib("Summary").then((data) => {
// Load CQL ELM JSON, and value set cache which contains evaluated expressions for use by app
const [elmJson, valueSetJson, namedExpression] = data;

let patientBundle = {
resourceType: 'Bundle',
id: 'resource-bundle',
type: 'collection',
entry: []
};
// Send the cqlWorker an initial message containing the ELM JSON representation of the CQL expressions
setupExecution(elmJson, valueSetJson);
// Send patient info to CQL worker to process
sendPatientBundle(patientBundle);

const getPatient = async () => {
const client = contextContent.client;
if (!client) return;
//this is a workaround for when patient id is not embedded within the JWT token
let queryPatientId = sessionStorage.getItem(queryPatientIdKey);
if (queryPatientId) {
console.log('Using stored patient id ', queryPatientId);
return client.request('/Patient/'+queryPatientId);
//named expression here is Summary, look in /src/cql/source/ExpressionLogicLibrary.cql and see how that is defined
//can use the result(s) from evaluated expressions if desire
evaluateExpression(namedExpression)
.then((result) => {
if (result) {
console.log("CQL Results ", result);
}
// Get the Patient resource
return await client.patient.read().then((pt) => {
return pt;
});
};

const getFhirResources = async (id) => {
const resources = getFHIRResourcePaths(id);
const requests = resources.map(resource => contextContent.client.request(resource));
return Promise.all(requests).then(results => {
results.forEach(result => {
if (!result) return true;
if (result.resourceType === 'Bundle' && result.entry) {
result.entry.forEach(o => {
if (o && o.resource) patientBundle.entry.push({resource: o.resource});
});
} else if (Array.isArray(result)) {
result.forEach(o => {
if (o.resourceType) patientBundle.entry.push({resource: o});
});
} else {
patientBundle.entry.push({resource: result});
}
});
//FHIR resources should be available now via patientBundle.entry
console.log('FHIR resource bundles ', patientBundle.entry);
});
}
})
.catch((e) => {
console.log("CQL Expression evaluation error ", e);
throw new Error(e);
});
});
};

const getCQLEvaluations = () => {
//get CQL expression lib
getExpressionLogicLib('Summary').then(data => {
// Load CQL ELM JSON, and value set cache which contains evaluated expressions for use by app
const [elmJson, valueSetJson, namedExpression] = data;
export default function Summary() {
const contextContent = React.useContext(FhirClientContext);
const [patient, setPatient] = React.useState(null);
const [error, setError] = React.useState(null);

// Send the cqlWorker an initial message containing the ELM JSON representation of the CQL expressions
setupExecution(elmJson, valueSetJson);
// Send patient info to CQL worker to process
sendPatientBundle(patientBundle);
const getPatient = React.useCallback(async () => {
const client = contextContent.client;
if (!client) return;
//this is a workaround for when patient id is not embedded within the JWT token
let queryPatientId = sessionStorage.getItem(queryPatientIdKey);
if (queryPatientId) {
console.log("Using stored patient id ", queryPatientId);
return client.request("/Patient/" + queryPatientId);
}
// Get the Patient resource
return await client.patient.read().then((pt) => {
return pt;
});
}, [contextContent.client]);

console.log("patient bundle? ", patientBundle)

//named expression here is Summary, look in /src/cql/source/ExpressionLogicLibrary.cql and see how that is defined
//can use the result(s) from evaluated expressions if desire
evaluateExpression(namedExpression).then(result => {
console.log("CQL expression result ", result)
if (result) {
console.log('CQL Results ', result);
}
}).catch( e => {
setError(e);
console.log("CQL Expression evaluation error ", e);
const getFhirResources = React.useCallback(
async (id) => {
if (!contextContent.client) return;
const resources = getFHIRResourcePaths(id);
const requests = resources.map((resource) =>
contextContent.client.request(resource)
);
return Promise.all(requests).then((results) => {
results.forEach((result) => {
if (!result) return true;
if (result.resourceType === "Bundle" && result.entry) {
result.entry.forEach((o) => {
if (o && o.resource)
patientBundle.entry.push({ resource: o.resource });
});
} else if (Array.isArray(result)) {
result.forEach((o) => {
if (o.resourceType) patientBundle.entry.push({ resource: o });
});
} else {
patientBundle.entry.push({ resource: result });
}
});
}
//FHIR resources should be available now via patientBundle.entry
console.log("FHIR resource bundles ", patientBundle.entry);
});
},
[contextContent.client]
);

let allLoaded = false; //ensure the async data calls are called once
React.useEffect(() => {
if (allLoaded) return;
getPatient().then(result => {
setPatient(result);
console.log("result ", result)
if (!patientBundle.entry.length) {
patientBundle.entry.unshift({resource: result});
}
if (result) {
getFhirResources(result.id);
getCQLEvaluations();
}
}).catch(e => {
console.log("Error ", e)
setError(e);
}); // eslint-disable-next-line
return () => allLoaded = true;
}, []);
React.useEffect(() => {
getPatient()
.then((result) => {
setPatient(result);
console.log("patient fhir resource result ", result);
if (!patientBundle.entry.length) {
patientBundle.entry.unshift({ resource: result });
}
if (result) {
getFhirResources(result.id);
getCQLEvaluations();
}
})
.catch((e) => {
console.log("Error ", e);
setError(e);
});
}, [getPatient, getFhirResources]);

return (
<React.Fragment>
{error && <Error messge={error}></Error>}
{/* write out patient info */}
{patient && <div>
<h2>Hello World SoF App</h2>
<div>Name: {patient.name[0].given.join(" ") + patient.name[0].family}</div>
<div>DOB: {patient.birthDate}</div>
</div>}
</React.Fragment>
);
return (
<React.Fragment>
{error && <Error messge={error}></Error>}
{/* write out patient info */}
{patient && (
<div>
<h2>Hello World SoF App</h2>
<div>
Name: {patient.name[0].given.join(" ") + patient.name[0].family}
</div>
<div>DOB: {patient.birthDate}</div>
</div>
)}
</React.Fragment>
);
}

0 comments on commit 68ca112

Please sign in to comment.