UNPKG

@medplum/dosespot-react

Version:

Medplum DoseSpot React SDK

3 lines (2 loc) 4.58 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_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,MEDPLUM_BOT_SYSTEM:()=>MEDPLUM_BOT_SYSTEM,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_PATIENT_SYNC_BOT={system:MEDPLUM_BOT_SYSTEM,value:"dosespot-patient-sync-bot"},DOSESPOT_IFRAME_BOT={system:MEDPLUM_BOT_SYSTEM,value:"dosespot-iframe-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_react_hooks=require("@medplum/react-hooks"),import_react=require("react");function useDoseSpotIFrame(options){let medplum=(0,import_react_hooks.useMedplum)(),{patientId,onPatientSyncSuccess,onIframeSuccess,onError}=options,initializingRef=(0,import_react.useRef)(!1),[iframeUrl,setIframeUrl]=(0,import_react.useState)(void 0),onPatientSyncSuccessRef=(0,import_react.useRef)(onPatientSyncSuccess);onPatientSyncSuccessRef.current=onPatientSyncSuccess;let onIframeSuccessRef=(0,import_react.useRef)(onIframeSuccess);onIframeSuccessRef.current=onIframeSuccess;let onErrorRef=(0,import_react.useRef)(onError);onErrorRef.current=onError;let initPage=(0,import_react.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_react.useEffect)(()=>{initPage().catch(console.error)},[initPage]),iframeUrl}var import_react_hooks2=require("@medplum/react-hooks"),import_react2=require("react");var DEFAULT_REFRESH_INTERVAL_MILLISECONDS=1e4;function useDoseSpotNotifications(options){let medplum=(0,import_react_hooks2.useMedplum)(),{onChange,onError}=options??{},hasDoseSpot=medplum.getProjectMembership()?.identifier?.some(i=>i.system?.includes("dosespot")),refreshInterval=options?.refreshIntervalMilliseconds??DEFAULT_REFRESH_INTERVAL_MILLISECONDS,timerRef=(0,import_react2.useRef)(void 0),[unreadCount,setUnreadCount]=(0,import_react2.useState)(void 0),stopTimer=(0,import_react2.useCallback)(()=>{let timerId=timerRef.current;timerId&&clearInterval(timerId)},[]),updateCount=(0,import_react2.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_react2.useCallback)(()=>{timerRef.current=setInterval(()=>{updateCount().catch(console.error)},refreshInterval)},[updateCount,refreshInterval]);return(0,import_react2.useEffect)(()=>(hasDoseSpot&&startTimer(),stopTimer),[hasDoseSpot,startTimer,stopTimer]),unreadCount} //# sourceMappingURL=index.cjs.map