UNPKG

@medplum/dosespot-react

Version:

Medplum DoseSpot React SDK

3 lines (2 loc) 7.23 kB
"use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0})},__copyProps=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames(from))!__hasOwnProp.call(to,key)&&key!==except&&__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable});return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:!0}),mod);var index_exports={};__export(index_exports,{DOSESPOT_ADD_FAVORITE_MEDICATION_BOT:()=>DOSESPOT_ADD_FAVORITE_MEDICATION_BOT,DOSESPOT_CLINIC_FAVORITE_ID_SYSTEM:()=>DOSESPOT_CLINIC_FAVORITE_ID_SYSTEM,DOSESPOT_DISPENSABLE_DRUG_ID_SYSTEM:()=>DOSESPOT_DISPENSABLE_DRUG_ID_SYSTEM,DOSESPOT_GET_FAVORITE_MEDICATIONS_BOT:()=>DOSESPOT_GET_FAVORITE_MEDICATIONS_BOT,DOSESPOT_IFRAME_BOT:()=>DOSESPOT_IFRAME_BOT,DOSESPOT_MEDICATION_HISTORY_BOT:()=>DOSESPOT_MEDICATION_HISTORY_BOT,DOSESPOT_NOTIFICATION_COUNTS_BOT:()=>DOSESPOT_NOTIFICATION_COUNTS_BOT,DOSESPOT_PATIENT_ID_SYSTEM:()=>DOSESPOT_PATIENT_ID_SYSTEM,DOSESPOT_PATIENT_SYNC_BOT:()=>DOSESPOT_PATIENT_SYNC_BOT,DOSESPOT_PRESCRIPTIONS_SYNC_BOT:()=>DOSESPOT_PRESCRIPTIONS_SYNC_BOT,DOSESPOT_SEARCH_MEDICATIONS_BOT:()=>DOSESPOT_SEARCH_MEDICATIONS_BOT,MEDPLUM_BOT_SYSTEM:()=>MEDPLUM_BOT_SYSTEM,getMedicationName:()=>getMedicationName,useDoseSpotClinicFormulary:()=>useDoseSpotClinicFormulary,useDoseSpotIFrame:()=>useDoseSpotIFrame,useDoseSpotNotifications:()=>useDoseSpotNotifications});module.exports=__toCommonJS(index_exports);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"};var import_core=require("@medplum/core"),import_react_hooks=require("@medplum/react-hooks"),import_react=require("react");function useDoseSpotClinicFormulary(){let[directions,privateSetDirections]=(0,import_react.useState)(void 0),[selectedMedication,privateSetSelectedMedication]=(0,import_react.useState)(void 0),medplum=(0,import_react_hooks.useMedplum)(),state={selectedMedication,directions},saveFavoriteMedication=(0,import_react.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=(0,import_react.useCallback)(async searchTerm=>await medplum.executeBot(DOSESPOT_SEARCH_MEDICATIONS_BOT,{name:searchTerm}),[medplum]);return{state,searchMedications,setSelectedMedication:medication=>{let medicationToSet;(0,import_core.isCodeableConcept)(medication)?medicationToSet={...medication}:(0,import_core.isCoding)(medication)&&(medicationToSet={text:medication.display||"",coding:[medication]}),privateSetSelectedMedication(medicationToSet)},setSelectedMedicationDirections:directions2=>{privateSetDirections(directions2)},saveFavoriteMedication,clear:()=>{privateSetSelectedMedication(void 0),privateSetDirections(void 0)}}}var import_react_hooks2=require("@medplum/react-hooks"),import_react2=require("react");function useDoseSpotIFrame(options){let medplum=(0,import_react_hooks2.useMedplum)(),{patientId,onPatientSyncSuccess,onIframeSuccess,onError}=options,initializingRef=(0,import_react2.useRef)(!1),[iframeUrl,setIframeUrl]=(0,import_react2.useState)(void 0),onPatientSyncSuccessRef=(0,import_react2.useRef)(onPatientSyncSuccess);onPatientSyncSuccessRef.current=onPatientSyncSuccess;let onIframeSuccessRef=(0,import_react2.useRef)(onIframeSuccess);onIframeSuccessRef.current=onIframeSuccess;let onErrorRef=(0,import_react2.useRef)(onError);onErrorRef.current=onError;let initPage=(0,import_react2.useCallback)(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(0,import_react2.useEffect)(()=>{initPage().catch(console.error)},[initPage]),iframeUrl}var import_react_hooks3=require("@medplum/react-hooks"),import_react3=require("react");var DEFAULT_REFRESH_INTERVAL_MILLISECONDS=1e4;function useDoseSpotNotifications(options){let medplum=(0,import_react_hooks3.useMedplum)(),{onChange,onError}=options??{},hasDoseSpot=medplum.getProjectMembership()?.identifier?.some(i=>i.system?.includes("dosespot")),refreshInterval=options?.refreshIntervalMilliseconds??DEFAULT_REFRESH_INTERVAL_MILLISECONDS,timerRef=(0,import_react3.useRef)(void 0),[unreadCount,setUnreadCount]=(0,import_react3.useState)(void 0),stopTimer=(0,import_react3.useCallback)(()=>{let timerId=timerRef.current;timerId&&clearInterval(timerId)},[]),updateCount=(0,import_react3.useCallback)(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=(0,import_react3.useCallback)(()=>{timerRef.current=setInterval(()=>{updateCount().catch(console.error)},refreshInterval)},[updateCount,refreshInterval]);return(0,import_react3.useEffect)(()=>(hasDoseSpot&&startTimer(),stopTimer),[hasDoseSpot,startTimer,stopTimer]),unreadCount}var getMedicationName=medication=>medication?.code?.text||""; //# sourceMappingURL=index.cjs.map