UNPKG

@medplum/dosespot-react

Version:

Medplum DoseSpot React SDK

3 lines (2 loc) 5.73 kB
var MEDPLUM_BOT_SYSTEM="https://www.medplum.com/bots",DOSESPOT_PATIENT_ID_SYSTEM="https://dosespot.com/patient-id",DOSESPOT_CLINIC_FAVORITE_ID_SYSTEM="https://dosespot.com/clinic-favorite-medication-id",DOSESPOT_DISPENSABLE_DRUG_ID_SYSTEM="https://dosespot.com/dispensable-drug-id",DOSESPOT_PATIENT_SYNC_BOT={system:MEDPLUM_BOT_SYSTEM,value:"dosespot-patient-sync-bot"},DOSESPOT_IFRAME_BOT={system:MEDPLUM_BOT_SYSTEM,value:"dosespot-iframe-bot"},DOSESPOT_ADD_FAVORITE_MEDICATION_BOT={system:MEDPLUM_BOT_SYSTEM,value:"dosespot-add-favorite-medication-bot"},DOSESPOT_GET_FAVORITE_MEDICATIONS_BOT={system:MEDPLUM_BOT_SYSTEM,value:"dosespot-get-favorite-medications-bot"},DOSESPOT_SEARCH_MEDICATIONS_BOT={system:MEDPLUM_BOT_SYSTEM,value:"dosespot-search-medication-bot"},DOSESPOT_MEDICATION_HISTORY_BOT={system:MEDPLUM_BOT_SYSTEM,value:"dosespot-medication-history-bot"},DOSESPOT_PRESCRIPTIONS_SYNC_BOT={system:MEDPLUM_BOT_SYSTEM,value:"dosespot-prescriptions-sync-bot"},DOSESPOT_NOTIFICATION_COUNTS_BOT={system:MEDPLUM_BOT_SYSTEM,value:"dosespot-notification-counts-bot"};import{isCodeableConcept,isCoding}from"@medplum/core";import{useMedplum}from"@medplum/react-hooks";import{useCallback,useState}from"react";function useDoseSpotClinicFormulary(){let[directions,privateSetDirections]=useState(void 0),[selectedMedication,privateSetSelectedMedication]=useState(void 0),medplum=useMedplum(),state={selectedMedication,directions},saveFavoriteMedication=useCallback(async()=>{if(!selectedMedication)throw new Error("Must select a medication before adding a favorite medication");let medicationKnowledgeWithDirections={resourceType:"MedicationKnowledge",code:{...selectedMedication},administrationGuidelines:[{dosage:[{dosage:[{patientInstruction:directions||""}],type:{coding:[{system:"https://dosespot.com/patient-instructions"}]}}]}]};return medplum.executeBot(DOSESPOT_ADD_FAVORITE_MEDICATION_BOT,medicationKnowledgeWithDirections)},[selectedMedication,directions,medplum]),searchMedications=useCallback(async searchTerm=>await medplum.executeBot(DOSESPOT_SEARCH_MEDICATIONS_BOT,{name:searchTerm}),[medplum]);return{state,searchMedications,setSelectedMedication:medication=>{let medicationToSet;isCodeableConcept(medication)?medicationToSet={...medication}:isCoding(medication)&&(medicationToSet={text:medication.display||"",coding:[medication]}),privateSetSelectedMedication(medicationToSet)},setSelectedMedicationDirections:directions2=>{privateSetDirections(directions2)},saveFavoriteMedication,clear:()=>{privateSetSelectedMedication(void 0),privateSetDirections(void 0)}}}import{useMedplum as useMedplum2}from"@medplum/react-hooks";import{useCallback as useCallback2,useEffect,useRef,useState as useState2}from"react";function useDoseSpotIFrame(options){let medplum=useMedplum2(),{patientId,onPatientSyncSuccess,onIframeSuccess,onError}=options,initializingRef=useRef(!1),[iframeUrl,setIframeUrl]=useState2(void 0),onPatientSyncSuccessRef=useRef(onPatientSyncSuccess);onPatientSyncSuccessRef.current=onPatientSyncSuccess;let onIframeSuccessRef=useRef(onIframeSuccess);onIframeSuccessRef.current=onIframeSuccess;let onErrorRef=useRef(onError);onErrorRef.current=onError;let initPage=useCallback2(async()=>{if(!initializingRef.current){initializingRef.current=!0;try{patientId&&(await medplum.executeBot(DOSESPOT_PATIENT_SYNC_BOT,{patientId}),onPatientSyncSuccessRef.current?.());let result=await medplum.executeBot(DOSESPOT_IFRAME_BOT,{patientId});result.url&&(setIframeUrl(result.url),onIframeSuccessRef.current?.(result.url))}catch(err){onErrorRef.current?.(err)}}},[medplum,patientId]);return useEffect(()=>{initPage().catch(console.error)},[initPage]),iframeUrl}import{useMedplum as useMedplum3}from"@medplum/react-hooks";import{useCallback as useCallback3,useEffect as useEffect2,useRef as useRef2,useState as useState3}from"react";var DEFAULT_REFRESH_INTERVAL_MILLISECONDS=1e4;function useDoseSpotNotifications(options){let medplum=useMedplum3(),{onChange,onError}=options??{},hasDoseSpot=medplum.getProjectMembership()?.identifier?.some(i=>i.system?.includes("dosespot")),refreshInterval=options?.refreshIntervalMilliseconds??DEFAULT_REFRESH_INTERVAL_MILLISECONDS,timerRef=useRef2(void 0),[unreadCount,setUnreadCount]=useState3(void 0),stopTimer=useCallback3(()=>{let timerId=timerRef.current;timerId&&clearInterval(timerId)},[]),updateCount=useCallback3(async()=>{try{let result=await medplum.executeBot(DOSESPOT_NOTIFICATION_COUNTS_BOT,{}),newCount=0;result.PendingPrescriptionsCount&&(newCount+=result.PendingPrescriptionsCount),result.PendingRxChangeCount&&(newCount+=result.PendingRxChangeCount),result.RefillRequestsCount&&(newCount+=result.RefillRequestsCount),result.TransactionErrorsCount&&(newCount+=result.TransactionErrorsCount),newCount!==unreadCount&&(setUnreadCount(newCount),onChange?.(newCount))}catch(err){onError?.(err),stopTimer()}},[medplum,unreadCount,onChange,onError,stopTimer]),startTimer=useCallback3(()=>{timerRef.current=setInterval(()=>{updateCount().catch(console.error)},refreshInterval)},[updateCount,refreshInterval]);return useEffect2(()=>(hasDoseSpot&&startTimer(),stopTimer),[hasDoseSpot,startTimer,stopTimer]),unreadCount}var getMedicationName=medication=>medication?.code?.text||"";export{DOSESPOT_ADD_FAVORITE_MEDICATION_BOT,DOSESPOT_CLINIC_FAVORITE_ID_SYSTEM,DOSESPOT_DISPENSABLE_DRUG_ID_SYSTEM,DOSESPOT_GET_FAVORITE_MEDICATIONS_BOT,DOSESPOT_IFRAME_BOT,DOSESPOT_MEDICATION_HISTORY_BOT,DOSESPOT_NOTIFICATION_COUNTS_BOT,DOSESPOT_PATIENT_ID_SYSTEM,DOSESPOT_PATIENT_SYNC_BOT,DOSESPOT_PRESCRIPTIONS_SYNC_BOT,DOSESPOT_SEARCH_MEDICATIONS_BOT,MEDPLUM_BOT_SYSTEM,getMedicationName,useDoseSpotClinicFormulary,useDoseSpotIFrame,useDoseSpotNotifications}; //# sourceMappingURL=index.mjs.map