Skip to content
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
33 changes: 9 additions & 24 deletions src/components/RequestBox/RequestBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import MuiAlert from '@mui/material/Alert';
import Snackbar from '@mui/material/Snackbar';
import { shortNameMap } from '../../util/data.js';
import { getAge, createMedicationDispenseFromMedicationRequest } from '../../util/fhir.js';
import { retrieveLaunchContext } from '../../util/util.js';
import { retrieveLaunchContext, prepPrefetch } from '../../util/util.js';
import './request.css';

const RequestBox = props => {
const [state, setState] = useState({
gatherCount: 0,
response: {},
submittedRx: false
submittedRx: false,
prefetchCompleted: false
});

const {
Expand All @@ -29,52 +30,36 @@ const RequestBox = props => {
smartAppUrl,
client,
pimsUrl,
prefetchCompleted,
getRemsAdminUrl
} = props;
const emptyField = <span className="empty-field">empty</span>;

const prepPrefetch = () => {
const preppedResources = new Map();
Object.keys(prefetchedResources).forEach(resourceKey => {
let resourceList = [];
if (Array.isArray(prefetchedResources[resourceKey])) {
resourceList = prefetchedResources[resourceKey].map(resource => {
return resource;
});
} else {
resourceList = prefetchedResources[resourceKey];
}

preppedResources.set(resourceKey, resourceList);
});
return preppedResources;
};

const submitPatientView = () => {
submitInfo(prepPrefetch(), null, patient, 'patient-view');
submitInfo(prepPrefetch(prefetchedResources), null, patient, 'patient-view');
};

const _submitOrderSelect = () => {
if (!_.isEmpty(request)) {
submitInfo(prepPrefetch(), request, patient, 'order-select');
submitInfo(prepPrefetch(prefetchedResources), request, patient, 'order-select');
}
};

const submitOrderSign = request => {
if (!_.isEmpty(request)) {
submitInfo(prepPrefetch(), request, patient, 'order-sign');
submitInfo(prepPrefetch(prefetchedResources), request, patient, 'order-sign');
}
};

useEffect(() => {
// if prefetch completed
if (state.prefetchCompleted) {
if (props.prefetchCompleted) {
// if the prefetch contains a medicationRequests bundle
if (prefetchedResources.medicationRequests) {
submitPatientView();
}
}
}, [state.prefetchCompleted]);
}, [props.prefetchCompleted]);

const renderPatientInfo = () => {
if (Object.keys(patient).length === 0) {
Expand Down
1 change: 1 addition & 0 deletions src/components/SMARTBox/PatientBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ const PatientBox = props => {
response: ''
}));
callback('response', '');
clearCallback();
// update prefetch request for the medication
const request = JSON.parse(data);
if (
Expand Down
56 changes: 53 additions & 3 deletions src/containers/RequestBuilder.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import RequestBox from '../components/RequestBox/RequestBox.jsx';
import buildRequest from '../util/buildRequest.js';
import { types } from '../util/data.js';
import { createJwt } from '../util/auth.js';
import { getMedicationSpecificRemsAdminUrl } from '../util/util.js';
import { getMedicationSpecificRemsAdminUrl, prepPrefetch } from '../util/util.js';

import Accordion from '@mui/material/Accordion';
import AccordionSummary from '@mui/material/AccordionSummary';
Expand Down Expand Up @@ -40,7 +40,9 @@ const RequestBuilder = props => {
token: null,
client: client,
medicationDispense: null,
lastCheckedMedicationTime: null
lastCheckedMedicationTime: null,
prefetchCompleted: false,
medicationRequests: {}
});
const displayRequestBox = !!globalState.patient?.id;

Expand Down Expand Up @@ -101,9 +103,56 @@ const RequestBuilder = props => {
}
};

const getMedicationRequests = patientId => {
client
.request(`MedicationRequest?subject=Patient/${patientId}`, {
resolveReferences: ['subject', 'performer', 'medicationReference'],
graph: false,
flat: true
})
.then(result => {
setState(prevState => ({ ...prevState, medicationRequests: result }));
});
};


useEffect(() => {
const hook = 'patient-view';

let remsAdminUrls = [];
// get all the remsAdminUrl for each MedicationRequest
state.medicationRequests?.data?.forEach(request => {
const code = request?.medicationCodeableConcept?.coding[0]?.code;
const remsAdminUrl = getMedicationSpecificRemsAdminUrl(request, globalState, hook);
if (remsAdminUrl) {
remsAdminUrls.push(remsAdminUrl);
}
//sendHook(prefetch, request, patient, hook, remsAdminUrl);
});
const uniqueUrls = [... new Set(remsAdminUrls.map(item => item))];

uniqueUrls?.forEach(url => {
sendHook(prepPrefetch(state.prefetchedResources), null, globalState.patient, hook, url);
//TODO: still need to handle multiple sends and multiple cards coming back
});

}, [state.medicationRequests]);

const submitInfo = (prefetch, request, patient, hook) => {
console.log('Initiating form submission ', types.info);
const remsAdminUrl = getMedicationSpecificRemsAdminUrl(request, globalState, hook);
let remsAdminUrl = null;
if (request) {
remsAdminUrl = getMedicationSpecificRemsAdminUrl(request, globalState, hook);
sendHook(prefetch, request, patient, hook, remsAdminUrl);
} else {
// grab all of the REMS admins for the medications for this patient

// get all MedicationRequests for the patient, then continue
getMedicationRequests(patient.id);
}
};

const sendHook = (prefetch, request, patient, hook, remsAdminUrl) => {

setState(prevState => ({
...prevState,
Expand Down Expand Up @@ -292,6 +341,7 @@ const RequestBuilder = props => {
defaultUser={globalState.defaultUser}
loading={state.loading}
patientFhirQuery={globalState.patientFhirQuery}
prefetchCompleted={state.prefetchCompleted}
getRemsAdminUrl={(request, hook) =>
getMedicationSpecificRemsAdminUrl(request, globalState, hook)
}
Expand Down
23 changes: 22 additions & 1 deletion src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ function standardsBasedGetEtasu(etasuUrl, body, responseCallback) {
}

const getMedicationSpecificRemsAdminUrl = (request, globalState, hook) => {
// if empty request, just return
if (Object.keys(request).length === 0) {
return undefined;
}
const display = request.medicationCodeableConcept?.coding?.[0]?.display;
const rxnorm = request.medicationCodeableConcept?.coding?.[0]?.code;

Expand All @@ -117,4 +121,21 @@ const getMedicationSpecificRemsAdminUrl = (request, globalState, hook) => {
return cdsUrl;
};

export { retrieveLaunchContext, standardsBasedGetEtasu, getMedicationSpecificRemsAdminUrl };
const prepPrefetch = (prefetchedResources) => {
const preppedResources = new Map();
Object.keys(prefetchedResources).forEach(resourceKey => {
let resourceList = [];
if (Array.isArray(prefetchedResources[resourceKey])) {
resourceList = prefetchedResources[resourceKey].map(resource => {
return resource;
});
} else {
resourceList = prefetchedResources[resourceKey];
}

preppedResources.set(resourceKey, resourceList);
});
return preppedResources;
};

export { retrieveLaunchContext, standardsBasedGetEtasu, getMedicationSpecificRemsAdminUrl, prepPrefetch };