UNPKG

@medplum/dosespot-react

Version:

Medplum DoseSpot React SDK

8 lines (7 loc) 14.7 kB
{ "version": 3, "sources": ["../../src/index.ts", "../../src/common.ts", "../../src/useDoseSpotClinicFormulary.ts", "../../src/useDoseSpotIFrame.ts", "../../src/useDoseSpotNotifications.ts", "../../src/utils.ts"], "sourcesContent": ["// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors\n// SPDX-License-Identifier: Apache-2.0\nexport * from './common';\nexport * from './useDoseSpotClinicFormulary';\nexport * from './useDoseSpotIFrame';\nexport * from './useDoseSpotNotifications';\nexport * from './utils';\n", "// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors\n// SPDX-License-Identifier: Apache-2.0\nimport type { Identifier } from '@medplum/fhirtypes';\n\nexport const MEDPLUM_BOT_SYSTEM = 'https://www.medplum.com/bots';\n\nexport const DOSESPOT_PATIENT_ID_SYSTEM = 'https://dosespot.com/patient-id';\n\nexport const DOSESPOT_CLINIC_FAVORITE_ID_SYSTEM = 'https://dosespot.com/clinic-favorite-medication-id';\n\nexport const DOSESPOT_DISPENSABLE_DRUG_ID_SYSTEM = 'https://dosespot.com/dispensable-drug-id';\n\nexport const DOSESPOT_PATIENT_SYNC_BOT: Identifier = { system: MEDPLUM_BOT_SYSTEM, value: 'dosespot-patient-sync-bot' };\n\nexport const DOSESPOT_IFRAME_BOT: Identifier = { system: MEDPLUM_BOT_SYSTEM, value: 'dosespot-iframe-bot' };\n\nexport const DOSESPOT_ADD_FAVORITE_MEDICATION_BOT: Identifier = {\n system: MEDPLUM_BOT_SYSTEM,\n value: 'dosespot-add-favorite-medication-bot',\n};\n\nexport const DOSESPOT_GET_FAVORITE_MEDICATIONS_BOT: Identifier = {\n system: MEDPLUM_BOT_SYSTEM,\n value: 'dosespot-get-favorite-medications-bot',\n};\n\nexport const DOSESPOT_SEARCH_MEDICATIONS_BOT: Identifier = {\n system: MEDPLUM_BOT_SYSTEM,\n value: 'dosespot-search-medication-bot',\n};\n\nexport const DOSESPOT_MEDICATION_HISTORY_BOT: Identifier = {\n system: MEDPLUM_BOT_SYSTEM,\n value: 'dosespot-medication-history-bot',\n};\n\nexport const DOSESPOT_PRESCRIPTIONS_SYNC_BOT: Identifier = {\n system: MEDPLUM_BOT_SYSTEM,\n value: 'dosespot-prescriptions-sync-bot',\n};\n\nexport const DOSESPOT_NOTIFICATION_COUNTS_BOT: Identifier = {\n system: MEDPLUM_BOT_SYSTEM,\n value: 'dosespot-notification-counts-bot',\n};\n\nexport interface DoseSpotNotificationCountsResponse {\n PendingPrescriptionsCount: number;\n PendingRxChangeCount: number;\n RefillRequestsCount: number;\n TransactionErrorsCount: number;\n}\n", "// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors\n// SPDX-License-Identifier: Apache-2.0\nimport { isCodeableConcept, isCoding } from '@medplum/core';\nimport type { CodeableConcept, Coding, MedicationKnowledge } from '@medplum/fhirtypes';\nimport { useMedplum } from '@medplum/react-hooks';\nimport { useCallback, useState } from 'react';\nimport { DOSESPOT_ADD_FAVORITE_MEDICATION_BOT, DOSESPOT_SEARCH_MEDICATIONS_BOT } from './common';\n\nexport interface DoseSpotClinicFormularyReturn {\n state: DoseSpotClinicFormularyState;\n /**\n * Search for DoseSpot Medications and returns array of temporary MedicationKnowledge objects that are not yet saved to the FHIR server\n */\n readonly searchMedications: (searchTerm: string) => Promise<CodeableConcept[]>;\n /**\n * Set the currently selected medication. Can be set as a CodeableConcept or a Coding, but state is always stored as a CodeableConcept\n */\n readonly setSelectedMedication: (medication: CodeableConcept | Coding | undefined) => void;\n /**\n * Set the directions for the currently selected medication\n */\n readonly setSelectedMedicationDirections: (directions: string | undefined) => void;\n /**\n * Save a DoseSpot Medication to the Clinic's favorites and returns the MedicationKnowledge object that was saved\n */\n readonly saveFavoriteMedication: () => Promise<MedicationKnowledge>;\n /**\n * Clear the state\n */\n readonly clear: () => void;\n}\n\nexport interface DoseSpotClinicFormularyState {\n selectedMedication: CodeableConcept | undefined;\n directions: string | undefined;\n}\n\nexport function useDoseSpotClinicFormulary(): DoseSpotClinicFormularyReturn {\n const [directions, privateSetDirections] = useState<string | undefined>(undefined);\n const [selectedMedication, privateSetSelectedMedication] = useState<CodeableConcept | undefined>(undefined);\n const medplum = useMedplum();\n\n const state: DoseSpotClinicFormularyState = { selectedMedication, directions };\n\n const saveFavoriteMedication = useCallback(async (): Promise<MedicationKnowledge> => {\n if (!selectedMedication) {\n throw new Error('Must select a medication before adding a favorite medication');\n }\n\n //Add the directions to the medicationKnowledge object\n const medicationKnowledgeWithDirections = {\n resourceType: 'MedicationKnowledge',\n code: { ...selectedMedication },\n administrationGuidelines: [\n {\n dosage: [\n {\n dosage: [\n {\n patientInstruction: directions || '',\n },\n ],\n type: {\n coding: [\n {\n system: 'https://dosespot.com/patient-instructions',\n },\n ],\n },\n },\n ],\n },\n ],\n };\n\n return medplum.executeBot(DOSESPOT_ADD_FAVORITE_MEDICATION_BOT, medicationKnowledgeWithDirections);\n }, [selectedMedication, directions, medplum]);\n\n const searchMedications = useCallback(\n async (searchTerm: string): Promise<CodeableConcept[]> => {\n return (await medplum.executeBot(DOSESPOT_SEARCH_MEDICATIONS_BOT, { name: searchTerm })) as CodeableConcept[];\n },\n [medplum]\n );\n\n const setSelectedMedicationDirections = (directions: string | undefined): void => {\n privateSetDirections(directions);\n };\n\n const setSelectedMedication = (medication: CodeableConcept | Coding | undefined): void => {\n let medicationToSet: CodeableConcept | undefined;\n if (isCodeableConcept(medication)) {\n medicationToSet = { ...medication };\n } else if (isCoding(medication)) {\n medicationToSet = {\n text: medication.display || '',\n coding: [medication],\n };\n }\n privateSetSelectedMedication(medicationToSet);\n };\n\n const clear = (): void => {\n privateSetSelectedMedication(undefined);\n privateSetDirections(undefined);\n };\n\n return {\n state,\n searchMedications,\n setSelectedMedication,\n setSelectedMedicationDirections,\n saveFavoriteMedication,\n clear,\n };\n}\n", "// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors\n// SPDX-License-Identifier: Apache-2.0\nimport { useMedplum } from '@medplum/react-hooks';\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport { DOSESPOT_IFRAME_BOT, DOSESPOT_PATIENT_SYNC_BOT } from './common';\n\nexport interface DoseSpotIFrameOptions {\n readonly patientId?: string;\n readonly onPatientSyncSuccess?: () => void;\n readonly onIframeSuccess?: (url: string) => void;\n readonly onError?: (err: unknown) => void;\n}\n\nexport function useDoseSpotIFrame(options: DoseSpotIFrameOptions): string | undefined {\n const medplum = useMedplum();\n const { patientId, onPatientSyncSuccess, onIframeSuccess, onError } = options;\n const initializingRef = useRef<boolean>(false);\n const [iframeUrl, setIframeUrl] = useState<string | undefined>(undefined);\n\n const onPatientSyncSuccessRef = useRef(onPatientSyncSuccess);\n onPatientSyncSuccessRef.current = onPatientSyncSuccess;\n\n const onIframeSuccessRef = useRef(onIframeSuccess);\n onIframeSuccessRef.current = onIframeSuccess;\n\n const onErrorRef = useRef(onError);\n onErrorRef.current = onError;\n\n const initPage = useCallback(async () => {\n if (initializingRef.current) {\n return;\n }\n\n initializingRef.current = true;\n try {\n if (patientId) {\n await medplum.executeBot(DOSESPOT_PATIENT_SYNC_BOT, { patientId });\n onPatientSyncSuccessRef.current?.();\n }\n const result = await medplum.executeBot(DOSESPOT_IFRAME_BOT, { patientId });\n if (result.url) {\n setIframeUrl(result.url);\n onIframeSuccessRef.current?.(result.url);\n }\n } catch (err: unknown) {\n onErrorRef.current?.(err);\n }\n }, [medplum, patientId]);\n\n useEffect(() => {\n initPage().catch(console.error);\n }, [initPage]);\n\n return iframeUrl;\n}\n", "// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors\n// SPDX-License-Identifier: Apache-2.0\nimport { useMedplum } from '@medplum/react-hooks';\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport type { DoseSpotNotificationCountsResponse } from './common';\nimport { DOSESPOT_NOTIFICATION_COUNTS_BOT } from './common';\n\nexport interface DoseSpotNotificationsOptions {\n readonly refreshIntervalMilliseconds?: number;\n readonly onChange?: (count: number) => void;\n readonly onError?: (err: unknown) => void;\n}\n\nconst DEFAULT_REFRESH_INTERVAL_MILLISECONDS = 10000;\n\nexport function useDoseSpotNotifications(options?: DoseSpotNotificationsOptions): number | undefined {\n const medplum = useMedplum();\n const { onChange, onError } = options ?? {};\n const hasDoseSpot = medplum.getProjectMembership()?.identifier?.some((i) => i.system?.includes('dosespot'));\n const refreshInterval = options?.refreshIntervalMilliseconds ?? DEFAULT_REFRESH_INTERVAL_MILLISECONDS;\n const timerRef = useRef<NodeJS.Timeout | undefined>(undefined);\n const [unreadCount, setUnreadCount] = useState<number | undefined>(undefined);\n\n const stopTimer = useCallback(() => {\n const timerId = timerRef.current;\n if (timerId) {\n clearInterval(timerId);\n }\n }, []);\n\n const updateCount = useCallback(async () => {\n try {\n const result = (await medplum.executeBot(\n DOSESPOT_NOTIFICATION_COUNTS_BOT,\n {}\n )) as DoseSpotNotificationCountsResponse;\n\n let newCount = 0;\n if (result.PendingPrescriptionsCount) {\n newCount += result.PendingPrescriptionsCount;\n }\n if (result.PendingRxChangeCount) {\n newCount += result.PendingRxChangeCount;\n }\n if (result.RefillRequestsCount) {\n newCount += result.RefillRequestsCount;\n }\n if (result.TransactionErrorsCount) {\n newCount += result.TransactionErrorsCount;\n }\n if (newCount !== unreadCount) {\n setUnreadCount(newCount);\n onChange?.(newCount);\n }\n } catch (err: unknown) {\n onError?.(err);\n stopTimer();\n }\n }, [medplum, unreadCount, onChange, onError, stopTimer]);\n\n const startTimer = useCallback(() => {\n timerRef.current = setInterval(() => {\n updateCount().catch(console.error);\n }, refreshInterval);\n }, [updateCount, refreshInterval]);\n\n useEffect(() => {\n // Start an interval timer to update the count every 5 seconds\n if (hasDoseSpot) {\n startTimer();\n }\n\n // Clear the interval timer when the component is unmounted\n return stopTimer;\n }, [hasDoseSpot, startTimer, stopTimer]);\n\n return unreadCount;\n}\n", "// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors\n// SPDX-License-Identifier: Apache-2.0\nimport type { MedicationKnowledge } from '@medplum/fhirtypes';\n\nexport const getMedicationName = (medication: MedicationKnowledge | undefined): string => {\n return medication?.code?.text || '';\n};\n"], "mappings": "wpBAAA,wiCCIO,IAAM,mBAAqB,+BAErB,2BAA6B,kCAE7B,mCAAqC,qDAErC,oCAAsC,2CAEtC,0BAAwC,CAAE,OAAQ,mBAAoB,MAAO,2BAA4B,EAEzG,oBAAkC,CAAE,OAAQ,mBAAoB,MAAO,qBAAsB,EAE7F,qCAAmD,CAC9D,OAAQ,mBACR,MAAO,sCACT,EAEa,sCAAoD,CAC/D,OAAQ,mBACR,MAAO,uCACT,EAEa,gCAA8C,CACzD,OAAQ,mBACR,MAAO,gCACT,EAEa,gCAA8C,CACzD,OAAQ,mBACR,MAAO,iCACT,EAEa,gCAA8C,CACzD,OAAQ,mBACR,MAAO,iCACT,EAEa,iCAA+C,CAC1D,OAAQ,mBACR,MAAO,kCACT,EC1CA,gBAA4C,yBAE5C,mBAA2B,gCAC3B,aAAsC,iBAgC/B,SAAS,4BAA4D,CAC1E,GAAM,CAAC,WAAY,oBAAoB,KAAI,uBAA6B,MAAS,EAC3E,CAAC,mBAAoB,4BAA4B,KAAI,uBAAsC,MAAS,EACpG,WAAU,+BAAW,EAErB,MAAsC,CAAE,mBAAoB,UAAW,EAEvE,0BAAyB,0BAAY,SAA0C,CACnF,GAAI,CAAC,mBACH,MAAM,IAAI,MAAM,8DAA8D,EAIhF,IAAM,kCAAoC,CACxC,aAAc,sBACd,KAAM,CAAE,GAAG,kBAAmB,EAC9B,yBAA0B,CACxB,CACE,OAAQ,CACN,CACE,OAAQ,CACN,CACE,mBAAoB,YAAc,EACpC,CACF,EACA,KAAM,CACJ,OAAQ,CACN,CACE,OAAQ,2CACV,CACF,CACF,CACF,CACF,CACF,CACF,CACF,EAEA,OAAO,QAAQ,WAAW,qCAAsC,iCAAiC,CACnG,EAAG,CAAC,mBAAoB,WAAY,OAAO,CAA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