import { useState } from "react"; import { useNavigate } from "react-router-dom"; import { DeviceRequest } from "fhir/r4"; import { FhirDeviceRequestDisplay } from "../../../../components"; import { useFhirDeviceRequestBroadcast, useFhirDeviceRequests, useFhirDeviceRequestTargetedBroadcast, } from "../../../../hooks"; import { useAuthServiceContext, } from "../../../../services"; import { Button } from "../../../../components/ui/button.tsx"; import { ChevronRight, Cpu, Edit, Plus, Radio } from "lucide-react"; import { Skeleton } from "../../../../components/ui/skeleton.tsx"; import { Sheet, SheetContent } from "../../../../components/ui/sheet.tsx"; import { mayEditRecord } from '../../../../utils/recordEditability'; import { useSeatStanding } from '../../../../utils/seatStanding'; const DeviceRequests = () => { const navigate = useNavigate(); const { userRole, USER_ROLES, practitionerRoles } = useAuthServiceContext(); const seatStanding = useSeatStanding(); // The nav row that opens this page says one thing and the page said another. A clinician reading // the same page through the chart is a different audience and keeps their own wording. const isPatientOrProxy = [USER_ROLES.PATIENT, USER_ROLES.RELATED_PERSON].includes(userRole as string); const { loading, error, deviceRequestsList } = useFhirDeviceRequests(); const [selectedDeviceRequest, setSelectedDeviceRequest] = useState(null); const [showDisplayModal, setShowDisplayModal] = useState(false); useFhirDeviceRequestBroadcast(); useFhirDeviceRequestTargetedBroadcast({ deviceRequest: selectedDeviceRequest || undefined, isOpen: showDisplayModal, onUpdate: (updatedDeviceRequest) => { setSelectedDeviceRequest(updatedDeviceRequest); }, onDelete: () => { setShowDisplayModal(false); setSelectedDeviceRequest(null); }, }); const viewDeviceRequest = (deviceRequest: DeviceRequest) => { setSelectedDeviceRequest(deviceRequest); setShowDisplayModal(true); }; const editDeviceRequest = ( event: React.MouseEvent, deviceRequest: DeviceRequest, ) => { event.stopPropagation(); navigate(deviceRequest.id as string, { relative: "path" }); }; const addDeviceRequest = () => { navigate("new", { relative: "path" }); }; const roleAllowsEdit = () => { return userRole && [USER_ROLES.PRACTITIONER].includes(userRole as string); }; /* * AND THE ROLE IS NOT THE RULE THE SERVER APPLIES. * * Writing a DeviceRequest needs a paid clinician seat, resolved per request by `callerMayWrite`. The role * check above passes for an unseated practitioner - a medical assistant, a nurse rooming patients, * somebody whose seat was turned off this morning - so the page offered Add and Edit, and the save * came back refused. A refusal that arrives after the typing has already spent the work. */ const seat = mayEditRecord({ seatStanding, resourceType: 'DeviceRequest', viewerIsStaff: Boolean(userRole) && ![USER_ROLES.PATIENT, USER_ROLES.RELATED_PERSON].includes(userRole as string), practitionerRoles, }); const isAuthorizedToEdit = () => roleAllowsEdit() && seat.mayEdit; return (

{isPatientOrProxy ? "Devices ordered for you" : "Devices"}

View and manage medical devices, implants, and associated traceability information.{" "}

{/* SAID, NOT WITHHELD. A control that simply disappears reads as a broken screen or as a permission the reader has lost, and both send them to an administrator to check permissions that are already correct. A seat is nameable, and a practice can enable one in a minute. */} {roleAllowsEdit() && !seat.mayEdit && (

{seat.reason}

)} {isAuthorizedToEdit() && ( )}
{" "} {error ? (
{" "}

Could not load Devices requests

Try again later.

) : !loading && deviceRequestsList.length === 0 ? (

No device requests found.

There are currently No device requests found to your health record. Devices are added by your healthcare provider and will appear here once assigned.

) : loading ? (
{[...Array(7)].map((_, index) => (
))}
) : ( deviceRequestsList.length !== 0 && (
{deviceRequestsList.map((deviceRequest: DeviceRequest) => { return (
viewDeviceRequest(deviceRequest)} onKeyDown={(event: React.KeyboardEvent) => { if (event.key !== 'Enter' && event.key !== ' ') return; event.preventDefault(); viewDeviceRequest(deviceRequest); }} >

{deviceRequest.codeCodeableConcept ? ( {deviceRequest.codeCodeableConcept.text ?.split("[")[0] .trim()} ) : ( "Device name not available" )} {deviceRequest.status ? deviceRequest.status : "-"}

{deviceRequest.intent ? deviceRequest.intent : "-"}

{" "} ยท

{deviceRequest.priority ? deviceRequest.priority : "-"}

{" "}
{isAuthorizedToEdit() && ( )}
); })}
) )} {selectedDeviceRequest && ( { if (!open) setSelectedDeviceRequest(null); }} > { setShowDisplayModal(false); setSelectedDeviceRequest(null); }} /> )}
); }; export default DeviceRequests; // import { useNavigate } from 'react-router-dom'; // import { DeviceRequest } from 'fhir/r4'; // import { AppSpinner, FhirDeviceRequestDisplay, ScrollableTab } from '../../../../components'; // import { formatDate } from '../../../../utils/dateUtils.ts'; // import { // useFhirDeviceRequestBroadcast, // useFhirDeviceRequests, // useFhirDeviceRequestTargetedBroadcast // } from '../../../../hooks'; // import { useAuthServiceContext, useDeviceRequestServiceContext } from '../../../../services'; // const DeviceRequests = () => { // const navigate = useNavigate(); // const { userRole, USER_ROLES } = useAuthServiceContext(); // const { getDeviceRequestDisplay } = useDeviceRequestServiceContext(); // const { loading, error, clearError, deviceRequestsList } = useFhirDeviceRequests(); // const [isGuideOpen, setIsGuideOpen] = useState(false); // const [selectedDeviceRequest, setSelectedDeviceRequest] = useState(null); // const [showDisplayModal, setShowDisplayModal] = useState(false); // const spacerMargin = '24px'; // useFhirDeviceRequestBroadcast(); // useFhirDeviceRequestTargetedBroadcast({ // deviceRequest: selectedDeviceRequest || undefined, // isOpen: showDisplayModal, // onUpdate: (updatedDeviceRequest) => { // setSelectedDeviceRequest(updatedDeviceRequest); // }, // onDelete: () => { // setShowDisplayModal(false); // setSelectedDeviceRequest(null); // } // }); // const viewDeviceRequest = (deviceRequest: DeviceRequest) => { // setSelectedDeviceRequest(deviceRequest); // setShowDisplayModal(true); // }; // const editDeviceRequest = (event: React.MouseEvent, deviceRequest: DeviceRequest) => { // event.stopPropagation(); // navigate(deviceRequest.id as string, { relative: 'path' }); // }; // const addDeviceRequest = () => { // navigate('new', { relative: 'path' }); // }; // const isAuthorizedToEdit = () => { // return userRole && [USER_ROLES.PRACTITIONER].includes(userRole as string); // }; // const getStatusDisplay = (deviceRequest: DeviceRequest): { text: string, color?: string } => { // if (!deviceRequest) return { text: 'Unknown' }; // switch (deviceRequest.status) { // case 'active': // return { text: 'Active', color: '#00a32a' }; // case 'draft': // return { text: 'Draft', color: '#4f94d4' }; // case 'entered-in-error': // return { text: 'Error', color: '#cc1818' }; // case 'on-hold': // return { text: 'On Hold', color: '#e39700' }; // case 'completed': // return { text: 'Completed', color: '#7b3ff2' }; // case 'cancelled': // return { text: 'Cancelled', color: '#757575' }; // case 'unknown': // default: // return { text: deviceRequest.status?.charAt(0).toUpperCase() + deviceRequest.status?.slice(1) || 'Unknown' }; // } // }; // const getPriorityDisplay = (deviceRequest: DeviceRequest): { text: string, color?: string } => { // if (!deviceRequest || !deviceRequest.priority) return { text: '' }; // switch (deviceRequest.priority) { // case 'urgent': // return { text: 'Urgent', color: '#cc1818' }; // case 'asap': // return { text: 'ASAP', color: '#e39700' }; // case 'stat': // return { text: 'STAT', color: '#cc1818' }; // case 'routine': // return { text: 'Routine', color: '#4f94d4' }; // default: // return { text: deviceRequest.priority.charAt(0).toUpperCase() + deviceRequest.priority.slice(1) }; // } // }; // const getDeviceRequestDate = (deviceRequest: DeviceRequest) => { // if (deviceRequest.occurrenceDateTime) { // return `Requested for: ${formatDate(deviceRequest.occurrenceDateTime)}`; // } else if (deviceRequest.occurrencePeriod?.start) { // return `Requested for: ${formatDate(deviceRequest.occurrencePeriod.start)} ${deviceRequest.occurrencePeriod.end ? `to ${formatDate(deviceRequest.occurrencePeriod.end)}`: '(ongoing)'}`; // } else if (deviceRequest.authoredOn) { // return `Authored on: ${formatDate(deviceRequest.authoredOn)}`; // } // return ''; // }; // return ( // // setIsGuideOpen(false)} // resourceType="DeviceRequest"/> // // // // // // // {isAuthorizedToEdit() && ( // // // // )} // // // {error && ( // <> // // {error} // // // // )} // {!loading && deviceRequestsList.length === 0 && ( // <> // // No device requests found. // // // // )} // <> // {loading ? ( // // ): ( // // {deviceRequestsList.length !== 0 && ( // // {deviceRequestsList.map((deviceRequest: DeviceRequest) => { // const displayTitle = getDeviceRequestDisplay(deviceRequest); // const { text: statusText, color: statusColor } = getStatusDisplay(deviceRequest); // const { text: priorityText, color: priorityColor } = getPriorityDisplay(deviceRequest); // const dateInfo = getDeviceRequestDate(deviceRequest); // const intent = deviceRequest.intent ? // `Intent: ${deviceRequest.intent.charAt(0).toUpperCase() + deviceRequest.intent.slice(1)}`: ''; // return ( // viewDeviceRequest(deviceRequest)}> // // // // // // // // // {displayTitle} // // {intent && ( // // {intent} // // )} // {priorityText && ( // // Priority: {priorityText} // // )} // {dateInfo && ( // // // {dateInfo} // // )} // // {isAuthorizedToEdit() && ( // // // // )} // // // // // ); // })} // // )} // // )} // // {selectedDeviceRequest && ( // { // setShowDisplayModal(false); // setSelectedDeviceRequest(null); // }} // /> // )} // // ); // }; // export default DeviceRequests;