|
1 |
| -import React from 'react'; |
2 |
| -import Worker from 'cql-worker/src/cql.worker.js'; // https://github.com/webpack-contrib/worker-loader |
3 |
| -import { initialzieCqlWorker } from 'cql-worker'; |
4 |
| -import { FhirClientContext } from '../FhirClientContext'; |
5 |
| -import Error from './Error'; |
| 1 | +import React from "react"; |
| 2 | +import Worker from "cql-worker/src/cql.worker.js"; // https://github.com/webpack-contrib/worker-loader |
| 3 | +import { initialzieCqlWorker } from "cql-worker"; |
| 4 | +import { FhirClientContext } from "../FhirClientContext"; |
| 5 | +import Error from "./Error"; |
6 | 6 | import {
|
7 |
| - getFHIRResourcePaths, |
8 |
| - getExpressionLogicLib, |
9 |
| - queryPatientIdKey} from '../util/util'; |
| 7 | + getFHIRResourcePaths, |
| 8 | + getExpressionLogicLib, |
| 9 | + queryPatientIdKey, |
| 10 | +} from "../util/util"; |
10 | 11 |
|
11 |
| -export default function Summary() { |
12 |
| - |
13 |
| - // Define a web worker for evaluating CQL expressions |
14 |
| - const cqlWorker = new Worker(); |
15 |
| - // Initialize the cql-worker |
16 |
| - let [setupExecution, sendPatientBundle, evaluateExpression] = initialzieCqlWorker(cqlWorker); |
| 12 | +let patientBundle = { |
| 13 | + resourceType: "Bundle", |
| 14 | + id: "resource-bundle", |
| 15 | + type: "collection", |
| 16 | + entry: [], |
| 17 | +}; |
| 18 | +// Define a web worker for evaluating CQL expressions |
| 19 | +const cqlWorker = new Worker(); |
| 20 | +// Initialize the cql-worker |
| 21 | +let [setupExecution, sendPatientBundle, evaluateExpression] = |
| 22 | + initialzieCqlWorker(cqlWorker); |
17 | 23 |
|
18 |
| - const contextContent = React.useContext(FhirClientContext); |
19 |
| - const [patient, setPatient] = React.useState(null); |
20 |
| - const [error, setError] = React.useState(null); |
| 24 | +const getCQLEvaluations = () => { |
| 25 | + //get CQL expression lib |
| 26 | + getExpressionLogicLib("Summary").then((data) => { |
| 27 | + // Load CQL ELM JSON, and value set cache which contains evaluated expressions for use by app |
| 28 | + const [elmJson, valueSetJson, namedExpression] = data; |
21 | 29 |
|
22 |
| - let patientBundle = { |
23 |
| - resourceType: 'Bundle', |
24 |
| - id: 'resource-bundle', |
25 |
| - type: 'collection', |
26 |
| - entry: [] |
27 |
| - }; |
| 30 | + // Send the cqlWorker an initial message containing the ELM JSON representation of the CQL expressions |
| 31 | + setupExecution(elmJson, valueSetJson); |
| 32 | + // Send patient info to CQL worker to process |
| 33 | + sendPatientBundle(patientBundle); |
28 | 34 |
|
29 |
| - const getPatient = async () => { |
30 |
| - const client = contextContent.client; |
31 |
| - if (!client) return; |
32 |
| - //this is a workaround for when patient id is not embedded within the JWT token |
33 |
| - let queryPatientId = sessionStorage.getItem(queryPatientIdKey); |
34 |
| - if (queryPatientId) { |
35 |
| - console.log('Using stored patient id ', queryPatientId); |
36 |
| - return client.request('/Patient/'+queryPatientId); |
| 35 | + //named expression here is Summary, look in /src/cql/source/ExpressionLogicLibrary.cql and see how that is defined |
| 36 | + //can use the result(s) from evaluated expressions if desire |
| 37 | + evaluateExpression(namedExpression) |
| 38 | + .then((result) => { |
| 39 | + if (result) { |
| 40 | + console.log("CQL Results ", result); |
37 | 41 | }
|
38 |
| - // Get the Patient resource |
39 |
| - return await client.patient.read().then((pt) => { |
40 |
| - return pt; |
41 |
| - }); |
42 |
| - }; |
43 |
| - |
44 |
| - const getFhirResources = async (id) => { |
45 |
| - const resources = getFHIRResourcePaths(id); |
46 |
| - const requests = resources.map(resource => contextContent.client.request(resource)); |
47 |
| - return Promise.all(requests).then(results => { |
48 |
| - results.forEach(result => { |
49 |
| - if (!result) return true; |
50 |
| - if (result.resourceType === 'Bundle' && result.entry) { |
51 |
| - result.entry.forEach(o => { |
52 |
| - if (o && o.resource) patientBundle.entry.push({resource: o.resource}); |
53 |
| - }); |
54 |
| - } else if (Array.isArray(result)) { |
55 |
| - result.forEach(o => { |
56 |
| - if (o.resourceType) patientBundle.entry.push({resource: o}); |
57 |
| - }); |
58 |
| - } else { |
59 |
| - patientBundle.entry.push({resource: result}); |
60 |
| - } |
61 |
| - }); |
62 |
| - //FHIR resources should be available now via patientBundle.entry |
63 |
| - console.log('FHIR resource bundles ', patientBundle.entry); |
64 |
| - }); |
65 |
| - } |
| 42 | + }) |
| 43 | + .catch((e) => { |
| 44 | + console.log("CQL Expression evaluation error ", e); |
| 45 | + throw new Error(e); |
| 46 | + }); |
| 47 | + }); |
| 48 | +}; |
66 | 49 |
|
67 |
| - const getCQLEvaluations = () => { |
68 |
| - //get CQL expression lib |
69 |
| - getExpressionLogicLib('Summary').then(data => { |
70 |
| - // Load CQL ELM JSON, and value set cache which contains evaluated expressions for use by app |
71 |
| - const [elmJson, valueSetJson, namedExpression] = data; |
| 50 | +export default function Summary() { |
| 51 | + const contextContent = React.useContext(FhirClientContext); |
| 52 | + const [patient, setPatient] = React.useState(null); |
| 53 | + const [error, setError] = React.useState(null); |
72 | 54 |
|
73 |
| - // Send the cqlWorker an initial message containing the ELM JSON representation of the CQL expressions |
74 |
| - setupExecution(elmJson, valueSetJson); |
75 |
| - // Send patient info to CQL worker to process |
76 |
| - sendPatientBundle(patientBundle); |
| 55 | + const getPatient = React.useCallback(async () => { |
| 56 | + const client = contextContent.client; |
| 57 | + if (!client) return; |
| 58 | + //this is a workaround for when patient id is not embedded within the JWT token |
| 59 | + let queryPatientId = sessionStorage.getItem(queryPatientIdKey); |
| 60 | + if (queryPatientId) { |
| 61 | + console.log("Using stored patient id ", queryPatientId); |
| 62 | + return client.request("/Patient/" + queryPatientId); |
| 63 | + } |
| 64 | + // Get the Patient resource |
| 65 | + return await client.patient.read().then((pt) => { |
| 66 | + return pt; |
| 67 | + }); |
| 68 | + }, [contextContent.client]); |
77 | 69 |
|
78 |
| - console.log("patient bundle? ", patientBundle) |
79 |
| - |
80 |
| - //named expression here is Summary, look in /src/cql/source/ExpressionLogicLibrary.cql and see how that is defined |
81 |
| - //can use the result(s) from evaluated expressions if desire |
82 |
| - evaluateExpression(namedExpression).then(result => { |
83 |
| - console.log("CQL expression result ", result) |
84 |
| - if (result) { |
85 |
| - console.log('CQL Results ', result); |
86 |
| - } |
87 |
| - }).catch( e => { |
88 |
| - setError(e); |
89 |
| - console.log("CQL Expression evaluation error ", e); |
| 70 | + const getFhirResources = React.useCallback( |
| 71 | + async (id) => { |
| 72 | + if (!contextContent.client) return; |
| 73 | + const resources = getFHIRResourcePaths(id); |
| 74 | + const requests = resources.map((resource) => |
| 75 | + contextContent.client.request(resource) |
| 76 | + ); |
| 77 | + return Promise.all(requests).then((results) => { |
| 78 | + results.forEach((result) => { |
| 79 | + if (!result) return true; |
| 80 | + if (result.resourceType === "Bundle" && result.entry) { |
| 81 | + result.entry.forEach((o) => { |
| 82 | + if (o && o.resource) |
| 83 | + patientBundle.entry.push({ resource: o.resource }); |
| 84 | + }); |
| 85 | + } else if (Array.isArray(result)) { |
| 86 | + result.forEach((o) => { |
| 87 | + if (o.resourceType) patientBundle.entry.push({ resource: o }); |
90 | 88 | });
|
| 89 | + } else { |
| 90 | + patientBundle.entry.push({ resource: result }); |
| 91 | + } |
91 | 92 | });
|
92 |
| - } |
| 93 | + //FHIR resources should be available now via patientBundle.entry |
| 94 | + console.log("FHIR resource bundles ", patientBundle.entry); |
| 95 | + }); |
| 96 | + }, |
| 97 | + [contextContent.client] |
| 98 | + ); |
93 | 99 |
|
94 |
| - let allLoaded = false; //ensure the async data calls are called once |
95 |
| - React.useEffect(() => { |
96 |
| - if (allLoaded) return; |
97 |
| - getPatient().then(result => { |
98 |
| - setPatient(result); |
99 |
| - console.log("result ", result) |
100 |
| - if (!patientBundle.entry.length) { |
101 |
| - patientBundle.entry.unshift({resource: result}); |
102 |
| - } |
103 |
| - if (result) { |
104 |
| - getFhirResources(result.id); |
105 |
| - getCQLEvaluations(); |
106 |
| - } |
107 |
| - }).catch(e => { |
108 |
| - console.log("Error ", e) |
109 |
| - setError(e); |
110 |
| - }); // eslint-disable-next-line |
111 |
| - return () => allLoaded = true; |
112 |
| - }, []); |
| 100 | + React.useEffect(() => { |
| 101 | + getPatient() |
| 102 | + .then((result) => { |
| 103 | + setPatient(result); |
| 104 | + console.log("patient fhir resource result ", result); |
| 105 | + if (!patientBundle.entry.length) { |
| 106 | + patientBundle.entry.unshift({ resource: result }); |
| 107 | + } |
| 108 | + if (result) { |
| 109 | + getFhirResources(result.id); |
| 110 | + getCQLEvaluations(); |
| 111 | + } |
| 112 | + }) |
| 113 | + .catch((e) => { |
| 114 | + console.log("Error ", e); |
| 115 | + setError(e); |
| 116 | + }); |
| 117 | + }, [getPatient, getFhirResources]); |
113 | 118 |
|
114 |
| - return ( |
115 |
| - <React.Fragment> |
116 |
| - {error && <Error messge={error}></Error>} |
117 |
| - {/* write out patient info */} |
118 |
| - {patient && <div> |
119 |
| - <h2>Hello World SoF App</h2> |
120 |
| - <div>Name: {patient.name[0].given.join(" ") + patient.name[0].family}</div> |
121 |
| - <div>DOB: {patient.birthDate}</div> |
122 |
| - </div>} |
123 |
| - </React.Fragment> |
124 |
| - ); |
| 119 | + return ( |
| 120 | + <React.Fragment> |
| 121 | + {error && <Error messge={error}></Error>} |
| 122 | + {/* write out patient info */} |
| 123 | + {patient && ( |
| 124 | + <div> |
| 125 | + <h2>Hello World SoF App</h2> |
| 126 | + <div> |
| 127 | + Name: {patient.name[0].given.join(" ") + patient.name[0].family} |
| 128 | + </div> |
| 129 | + <div>DOB: {patient.birthDate}</div> |
| 130 | + </div> |
| 131 | + )} |
| 132 | + </React.Fragment> |
| 133 | + ); |
125 | 134 | }
|
0 commit comments