import { useState } from "react"; import { useNavigate } from "react-router-dom"; import { VisionPrescription } from "fhir/r4"; import { FhirVisionPrescriptionDisplay } from "../../../../components"; import { useFhirVisionPrescriptionBroadcast, useFhirVisionPrescriptions, useFhirVisionPrescriptionTargetedBroadcast, } from "../../../../hooks"; import { useAuthServiceContext, } from "../../../../services"; import { Button } from "../../../../components/ui/button"; import { Calendar, ChevronRight, Edit, Eye, FileText, Layers, Plus, UserRoundPlus, } from "lucide-react"; import { Skeleton } from "../../../../components/ui/skeleton"; import { formatDateForDisplay } from "../../../../lib/utils"; import { Sheet, SheetContent } from "../../../../components/ui/sheet"; import { mayEditRecord } from '../../../../utils/recordEditability'; import { useSeatStanding } from '../../../../utils/seatStanding'; const VisionPrescriptions = () => { 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, visionPrescriptionsList } = useFhirVisionPrescriptions(); const [selectedVisionPrescription, setSelectedVisionPrescription] = useState(null); const [showDisplayModal, setShowDisplayModal] = useState(false); useFhirVisionPrescriptionBroadcast(); useFhirVisionPrescriptionTargetedBroadcast({ visionPrescription: selectedVisionPrescription || undefined, isOpen: showDisplayModal, onUpdate: (updatedVisionPrescription) => { setSelectedVisionPrescription(updatedVisionPrescription); }, onDelete: () => { setShowDisplayModal(false); setSelectedVisionPrescription(null); }, }); const viewVisionPrescription = (visionPrescription: VisionPrescription) => { setSelectedVisionPrescription(visionPrescription); setShowDisplayModal(true); }; const editVisionPrescription = ( event: React.MouseEvent, visionPrescription: VisionPrescription, ) => { event.stopPropagation(); navigate(visionPrescription.id as string, { relative: "path" }); }; const addVisionPrescription = () => { 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 VisionPrescription 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: 'VisionPrescription', viewerIsStaff: Boolean(userRole) && ![USER_ROLES.PATIENT, USER_ROLES.RELATED_PERSON].includes(userRole as string), practitionerRoles, }); const isAuthorizedToEdit = () => roleAllowsEdit() && seat.mayEdit; const getLensCount = (visionPrescription: VisionPrescription): number => { return visionPrescription.lensSpecification?.length || 0; }; return (

{isPatientOrProxy ? "Glasses & contacts" : "Vision Prescriptions"}

Manage vision prescriptions, lens specifications, and corrective lens details.

{/* 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 Vision Prescriptions

Try again later.

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

No Vision Prescriptions found

You have no active vision prescriptions.

) : loading ? (
{[...Array(7)].map((_, index) => (
))}
) : ( visionPrescriptionsList.length !== 0 && (
{visionPrescriptionsList.map( (visionPrescription: VisionPrescription) => { const lensCount = getLensCount(visionPrescription); // - the row IS the control, so it must be reachable without a pointer. // Enter and Space both, because role="button" promises both. return (
viewVisionPrescription(visionPrescription)} onKeyDown={(event: React.KeyboardEvent) => { if (event.key !== 'Enter' && event.key !== ' ') return; event.preventDefault(); viewVisionPrescription(visionPrescription); }} >

{visionPrescription?.resourceType ? visionPrescription.resourceType .replace(/([A-Z])/g, " $1") .trim() : "Prescription name not available"} {visionPrescription.status ? visionPrescription.status : "Status unknown"}

{visionPrescription.prescriber?.display ? (

Practitioner -{" "} {visionPrescription.prescriber?.display}

) : ( "Practitioner not available" )}
{lensCount > 0 && ( {lensCount} lens spec {lensCount !== 1 ? "s" : ""} )} {visionPrescription?.lensSpecification?.map( (vision, index) => ( OD ({vision?.eye}) ), )} {visionPrescription?.dateWritten && ( {" "} {formatDateForDisplay( visionPrescription?.dateWritten, )} )}
{isAuthorizedToEdit() && (
)}
); }, )}
) )} {selectedVisionPrescription && ( { if (!open) setSelectedVisionPrescription(null); }} > { setShowDisplayModal(false); setSelectedVisionPrescription(null); }} /> )}
); }; export default VisionPrescriptions; // import { useNavigate } from 'react-router-dom'; // import { VisionPrescription } from 'fhir/r4'; // import { AppSpinner, FhirVisionPrescriptionDisplay, ScrollableTab } from '../../../../components'; // import { // useFhirVisionPrescriptionBroadcast, // useFhirVisionPrescriptions, // useFhirVisionPrescriptionTargetedBroadcast // } from '../../../../hooks'; // import { useAuthServiceContext, useVisionPrescriptionServiceContext } from '../../../../services'; // const VisionPrescriptions = () => { // const navigate = useNavigate(); // const { userRole, USER_ROLES } = useAuthServiceContext(); // const { getVisionPrescriptionDisplay } = useVisionPrescriptionServiceContext(); // const { loading, error, clearError, visionPrescriptionsList } = useFhirVisionPrescriptions(); // const [isGuideOpen, setIsGuideOpen] = useState(false); // const [selectedVisionPrescription, setSelectedVisionPrescription] = useState(null); // const [showDisplayModal, setShowDisplayModal] = useState(false); // const spacerMargin = '24px'; // useFhirVisionPrescriptionBroadcast(); // useFhirVisionPrescriptionTargetedBroadcast({ // visionPrescription: selectedVisionPrescription || undefined, // isOpen: showDisplayModal, // onUpdate: (updatedVisionPrescription) => { // setSelectedVisionPrescription(updatedVisionPrescription); // }, // onDelete: () => { // setShowDisplayModal(false); // setSelectedVisionPrescription(null); // } // }); // const viewVisionPrescription = (visionPrescription: VisionPrescription) => { // setSelectedVisionPrescription(visionPrescription); // setShowDisplayModal(true); // }; // const editVisionPrescription = (event: React.MouseEvent, visionPrescription: VisionPrescription) => { // event.stopPropagation(); // navigate(visionPrescription.id as string, { relative: 'path' }); // }; // const addVisionPrescription = () => { // navigate('new', { relative: 'path' }); // }; // const isAuthorizedToEdit = () => { // return userRole && [USER_ROLES.PRACTITIONER].includes(userRole as string); // }; // const getStatusDisplay = (visionPrescription: VisionPrescription): { text: string, color?: string } => { // if (!visionPrescription) return { text: 'Unknown' }; // switch (visionPrescription.status) { // case 'active': // return { text: 'Active', color: '#00a32a' }; // case 'cancelled': // return { text: 'Cancelled', color: '#cc1818' }; // case 'draft': // return { text: 'Draft', color: '#757575' }; // case 'entered-in-error': // return { text: 'Error', color: '#cc1818' }; // default: // return { text: visionPrescription.status?.charAt(0).toUpperCase() + visionPrescription.status?.slice(1) || 'Unknown' }; // } // }; // const getLensCount = (visionPrescription: VisionPrescription): number => { // return visionPrescription.lensSpecification?.length || 0; // }; // return ( // // setIsGuideOpen(false)} // resourceType="VisionPrescription"/> // // // // // // // {isAuthorizedToEdit() && ( // // // // )} // // // {error && ( // <> // // {error} // // // // )} // {!loading && visionPrescriptionsList.length === 0 && ( // <> // // No vision prescriptions found. // // // // )} // <> // {loading ? ( // // ): ( // // {visionPrescriptionsList.length !== 0 && ( // // {visionPrescriptionsList.map((visionPrescription: VisionPrescription) => { // const displayTitle = getVisionPrescriptionDisplay(visionPrescription); // const { text: statusText, color: statusColor } = getStatusDisplay(visionPrescription); // const lensCount = getLensCount(visionPrescription); // return ( // viewVisionPrescription(visionPrescription)}> // // // // // // // // // {displayTitle} // // {lensCount > 0 && ( // // {lensCount} lens specification{lensCount !== 1 ? 's': ''} // // )} // // {isAuthorizedToEdit() && ( // // // // )} // // // // // ); // })} // // )} // // )} // // {selectedVisionPrescription && ( // { // setShowDisplayModal(false); // setSelectedVisionPrescription(null); // }} // /> // )} // // ); // }; // export default VisionPrescriptions;