Skip to content

Commit

Permalink
Updated more files with typescript.
Browse files Browse the repository at this point in the history
  • Loading branch information
senorpatricio committed Oct 8, 2020
1 parent 086bd32 commit e21f8e3
Show file tree
Hide file tree
Showing 28 changed files with 127 additions and 149 deletions.
4 changes: 2 additions & 2 deletions .rescriptsrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ const addLaunch = function (config) {
return edit(
function (entries) {
if (!Array.isArray(entries) || entries.filter(function (e) {
return e.endsWith(path.sep + 'index.js');
return e.endsWith(path.sep + 'index.tsx');
}).length !== 1) {
console.error('Cannot add launch.js to entry. Unexpected starting value for entry:', entries);
return entries;
}
return {
main: entries,
launch: entries.map(function (e) {
return e.replace(/[\/\\]index.js$/, path.sep + 'launch.js');
return e.replace(/[\/\\]index.tsx$/, path.sep + 'launch.ts');
})
};
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/enzyme-adapter-react-16": "^1.0.6",
"@types/jest": "^26.0.14",
"@types/node": "^14.11.5",
"@types/react": "^16.9.51",
Expand Down
4 changes: 3 additions & 1 deletion src/@types/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
declare module 'react-tooltip';
declare module 'react-modal';
declare module 'react-modal';
declare module 'cql-execution';
declare module 'cql-exec-fhir';
2 changes: 1 addition & 1 deletion src/components/Landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Landing extends Component<any, any> {
}

componentDidMount() {
executeElm(this.state.collector).then((result) => {
executeElm(this.state.collector).then((result: any) => {
this.setState({ loading: false });
const { sectionFlags, flaggedCount } = this.processSummary(result.Summary);
this.setState({ result, sectionFlags, flaggedCount });
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/helpers/formatit.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export function arrayFlatten(result, input, property, propertyAria, showAria) {
if (input == null) return '';
return input.map((question, i) => {
let ariaLabel = '';
if (showAria) ariaLabel = question[property] + ' - Question Score: ' + question[propertyAria];
return <span key={i} aria-hidden={!showAria} aria-label={ariaLabel}>{question[property]}<br /></span>;
if (showAria) {ariaLabel = question[property] + ' - Question Score: ' + question[propertyAria]};
return (<span key={i} aria-hidden={!showAria} aria-label={ariaLabel}>{question[property]}<br /></span>);
});
}

Expand Down
52 changes: 0 additions & 52 deletions src/helpers/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,3 @@ import 'core-js/es/map';
import 'core-js/es/weak-map';
import 'core-js/es/set';
import 'core-js/es/array';

// Object.assign polyfill
// if (typeof Object.assign !== 'function') {
// // Must be writable: true, enumerable: false, configurable: true
// Object.defineProperty(Object, "assign", {
// value: function assign(target, varArgs) { // .length of function is 2
// if (target === null || target === undefined) {
// throw new TypeError('Cannot convert undefined or null to object');
// }

// var to = Object(target);

// for (var index = 1; index < arguments.length; index++) {
// var nextSource = arguments[index];

// if (nextSource !== null && nextSource !== undefined) {
// for (var nextKey in nextSource) {
// // Avoid bugs when hasOwnProperty is shadowed
// if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
// to[nextKey] = nextSource[nextKey];
// }
// }
// }
// }
// return to;
// },
// writable: true,
// configurable: true
// });
// }

// if (!Object.setPrototypeOf) {
// // Only works in Chrome and FireFox, does not work in IE:
// // eslint-disable-next-line
// Object.prototype.setPrototypeOf = function(obj, proto) {
// if(obj.__proto__) {
// obj.__proto__ = proto;
// return obj;
// } else {
// // If you want to return prototype of Object.create(null):
// var Fn = function() {
// for (var key in obj) {
// Object.defineProperty(this, key, {
// value: obj[key],
// });
// }
// };
// Fn.prototype = proto;
// return new Fn();
// }
// }
// }
5 changes: 3 additions & 2 deletions src/helpers/sortit.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ export function quantityCompare(a, b) {
} else if (aM == null && bM == null) {
return 0;
}
const aUnit = aM.length === 5 && aM[4] != null ? aM[4] : '';
const bUnit = bM.length === 5 && bM[4] != null ? bM[4] : '';
const aUnit = aM?.length === 5 && aM[4] != null ? aM[4] : '';
const bUnit = bM?.length === 5 && bM[4] != null ? bM[4] : '';
const unitCmp = stringCompare(aUnit, bUnit);
if (unitCmp !== 0) {
return unitCmp;
}
// @ts-ignore
return numberCompare(aM[1], bM[1]);
}

Expand Down
File renamed without changes.
35 changes: 0 additions & 35 deletions src/launch.js

This file was deleted.

35 changes: 35 additions & 0 deletions src/launch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// import FHIR from 'fhirclient';
import { oauth2 as SMART } from 'fhirclient';
import './helpers/polyfill';
require('fetch-everywhere');

// fetch(process.env.PUBLIC_URL + '/launch-context.json')
// .then(function (response) {
// return response.json()
// }).then(function (launchContext) {
// // const context = JSON.stringify(launchContext);
// // console.log('launchContext: ', launchContext);
// return FHIR.oauth2.authorize(launchContext);
// })
// .catch(function (error) {
// console.log('bad error: ', error)
// }
// )
SMART.init({
iss: "https://launcher-develop.sandbox.alphora.com/v/r4/fhir",
clientId: "6c12dff4-24e7-4475-a742-b08972c4ea27",
scope: "patient/*.read launch launch/patient",
completeInTarget: true
}).then(client => {
console.log('client:', client)
return Promise.all([
client.patient.read(),
client.request(`/MedicationRequest?patient=${client.patient.id}`, {
resolveReferences: "medicationReference",
pageLimit: 0,
flat: true
}).then(response => {
console.log('med response: ', response);
})
])
})
2 changes: 1 addition & 1 deletion src/serviceWorker.js → src/serviceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function register(config) {
}

window.addEventListener('load', function () {
const swUrl = process.env.PUBLIC_URL + '/service-worker.js';
const swUrl = process.env.PUBLIC_URL + '/service-worker.ts';

if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
Expand Down
3 changes: 2 additions & 1 deletion src/setupTests.js → src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import 'jest-enzyme';
configure({ adapter: new Adapter() });

// mock local storage
global.window = {};
// global.window = {};
(window as any);
// see: https://github.com/letsrock-today/mock-local-storage/issues/17
Object.defineProperty(window, 'localStorage', {
value: global.localStorage,
Expand Down
21 changes: 11 additions & 10 deletions src/utils/executeELM.js → src/utils/executeELM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import valueSetDB from '../cql/valueset-db.json';
import medicationReferenceSolver from "./medicationReferenceSolver";
var Promise = require('es6-promise').Promise;

function executeELM(collector) {
let client, release, library;
return new Promise((resolve) => {
let cqlFhirModule: any = cqlfhir;
function executeELM(collector: any) {
let client: any, release: any, library: any;
return new Promise((resolve: any) => {
// First get our authorized client and send the FHIR release to the next step
const results = FHIR.oauth2.ready().then((clientArg) => {
client = clientArg;
Expand All @@ -33,7 +34,7 @@ function executeELM(collector) {
return client.patient.read();
})
// then gather all the patient's relevant resource instances and send them in a bundle to the next step
.then((pt) => {
.then((pt: any) => {
collector.push({ data: pt, url: 'Patient/' + pt.id});
let isFromOpiodRec = false;
const requests = extractResourcesFromELM(library,isFromOpiodRec).map((name) => {
Expand All @@ -43,9 +44,9 @@ function executeELM(collector) {
return doSearch(client, release, name, collector);
});
// Don't return until all the requests have been resolved
return Promise.all(requests).then((requestResults) => {
const resources = [];
requestResults.forEach(result => resources.push(...result));
return Promise.all(requests).then((requestResults: any) => {
const resources: any[] = [];
requestResults.forEach((result) => {resources.push(...result)});
return {
resourceType: "Bundle",
entry: resources.map(r => ({ resource: r }))
Expand Down Expand Up @@ -88,9 +89,9 @@ function getLibrary(release) {
function getPatientSource(release) {
switch(release) {
case 2:
return cqlfhir.PatientSource.FHIRv102();
case 4:
return cqlfhir.PatientSource.FHIRv400();
return cqlFhirModule.PatientSource.FHIRv102();
case 4:
return cqlFhirModule.PatientSource.FHIRv400();
default:
throw new Error('Only FHIR DSTU2 and FHIR R4 servers are supported');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ async function getValues(collector, opioidLibraryNumber) {
let returnText = '';
await createPrefetch(collector, opioidLibraryNumber)
.then((prefetchData) => {
return fetch(url, {
method: 'POST', // use GET for cds-services discovery call
body: prefetchData, // string or object !!put back in for POST operation
let params: RequestInit = {
method: 'POST',
body: JSON.stringify(prefetchData),
headers: {
'Content-Type': 'application/json'
}
})
}
let fetchUrl = JSON.stringify(url);
return fetch(fetchUrl, params)
.then(response => {return response.text();})
.then(response => {
console.log(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export default function executeInternalCDSCall(recommendationNumber, collector)
return doSearch(client, release, name, collector);
});
// Don't return until all the requests have been resolved
return Promise.all(requests).then(function (requestResults) {
const resources = [];
requestResults.forEach(function (result) {
return Promise.all(requests).then((requestResults) => {
const resources: any[] = [];
requestResults.forEach((result: any) => {
return resources.push(...result);
});
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
function extractResourcesFromELM(elm, isFromOpiodRec) {
const resources = new Set();
const resources: any = new Set();
if (elm && elm.source && elm.source.library && elm.source.library.statements && elm.source.library.statements.def) {
for (const expDef of Object.values(elm.source.library.statements.def)) {
extractResourcesFromExpression(resources, expDef.expression);
let expDef: any;
for (expDef of Object.values(elm.source.library.statements.def)) {
extractResourcesFromExpression(resources, expDef.expression!);
}
}
if(isFromOpiodRec){
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 17 additions & 13 deletions src/utils/prefetchCreator.js → src/utils/prefetchCreator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export default function createPrefetch(collector, opioidLibraryNumber) {
async function getPrefetch(collector, opioidLibraryNumber) {
let hookid;
let newPrefetchStr;
if(opioidLibraryNumber === 10){
if (opioidLibraryNumber === 10) {
hookid = 'opioidcds-10-patient-view';
}else if(opioidLibraryNumber === 11){
} else if (opioidLibraryNumber === 11) {
hookid = 'opioidcds-11-patient-view';

}
Expand All @@ -21,10 +21,10 @@ async function getPrefetch(collector, opioidLibraryNumber) {

function createPrefetchObject(collector, itemsList) {
let finalPrefetch = getHeader(collector[0]);
if (itemsList){
if (itemsList) {
let index = 0;
for (let [key, value] of Object.entries(itemsList)) {
if(index > 0){
if (index > 0) {
finalPrefetch = finalPrefetch + ',';
}
index = index + 1;
Expand All @@ -38,7 +38,8 @@ function createPrefetchObject(collector, itemsList) {

function createItemList(collector, hookId) {
let items = {};
let url = process.env.REACT_APP_CDS_URL;
let url = JSON.stringify(process.env.REACT_APP_CDS_URL);

return fetch(url, {
method: 'GET', // use GET for cds-services discovery call
headers: {
Expand All @@ -56,10 +57,11 @@ function createItemList(collector, hookId) {
if (!resourceType.includes('Patient') && entry.data.entry) {
for (let [key, value] of Object.entries(prefetch)) {
if (key !== 'item1') { //handled in header
if (value.includes(resourceType)) {
let inhValue: any = value;
if (inhValue.includes(resourceType)) {
entry.data.entry.forEach((dataEntry) => {
if ((dataEntry.resource.code && value.includes(dataEntry.resource.code.coding[0].code + ',')) ||
(dataEntry.resource.medicationCodeableConcept && value.includes(dataEntry.resource.medicationCodeableConcept.coding[0].code + ','))) {
if ((dataEntry.resource.code && inhValue.includes(dataEntry.resource.code.coding[0].code + ',')) ||
(dataEntry.resource.medicationCodeableConcept && inhValue.includes(dataEntry.resource.medicationCodeableConcept.coding[0].code + ','))) {
if (items[key]) {
items[key] = items[key] + ',' + JSON.stringify(dataEntry.resource);
} else {
Expand All @@ -80,14 +82,16 @@ function createItemList(collector, hookId) {

function getHeader(pt) {
let uuid = uuidv4();
let fhirServer = process.env.REACT_APP_CDS_URL
.substring(0, process.env.REACT_APP_CDS_URL.lastIndexOf('/')) + '/fhir';
let prefetchStr = '{"hookInstance": "' + uuid + '","fhirServer": "' + fhirServer +'","hook": "patient-view","applyCql": true,"context": {"userId": "Practitioner/PainManager","patientId": "';
return prefetchStr + pt.url + '"}, "prefetch": {"item1":' + JSON.stringify(pt.data) + ",";
if (process.env.REACT_APP_CDS_URL) {
let fhirServer = process.env.REACT_APP_CDS_URL
.substring(0, process.env.REACT_APP_CDS_URL.lastIndexOf('/')) + '/fhir';
let prefetchStr = '{"hookInstance": "' + uuid + '","fhirServer": "' + fhirServer + '","hook": "patient-view","applyCql": true,"context": {"userId": "Practitioner/PainManager","patientId": "';
return prefetchStr + pt.url + '"}, "prefetch": {"item1":' + JSON.stringify(pt.data) + ",";
}
}

function uuidv4() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
return JSON.stringify(1e7 + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c:any) =>
// eslint-disable-next-line
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit e21f8e3

Please sign in to comment.