import { useState } from "react"; import { useNavigate } from "react-router-dom"; import { NutritionOrder } from "fhir/r4"; import { FhirNutritionOrderDisplay } from "../../../../components"; import { useFhirNutritionOrderBroadcast, useFhirNutritionOrders, useFhirNutritionOrderTargetedBroadcast, } from "../../../../hooks"; import { useAuthServiceContext } from "../../../../services"; import { useViewerIsPatient } from "../../../../hooks"; import { ChevronRight, Edit, FileText, Plus } from "lucide-react"; import { Skeleton } from "../../../../components/ui/skeleton"; import { Button } from "../../../../components/ui/button"; import { Sheet, SheetContent } from "../../../../components/ui/sheet"; import { mayEditRecord } from '../../../../utils/recordEditability'; import { dietLines } from './dietLines'; import { useSeatStanding } from '../../../../utils/seatStanding'; const NutritionOrders = () => { const navigate = useNavigate(); const { userRole, USER_ROLES, practitionerRoles } = useAuthServiceContext(); const seatStanding = useSeatStanding(); const viewerIsPatient = useViewerIsPatient(); // 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, nutritionOrdersList } = useFhirNutritionOrders(); const [selectedNutritionOrder, setSelectedNutritionOrder] = useState(null); const [showDisplayModal, setShowDisplayModal] = useState(false); useFhirNutritionOrderBroadcast(); useFhirNutritionOrderTargetedBroadcast({ nutritionOrder: selectedNutritionOrder || undefined, isOpen: showDisplayModal, onUpdate: (updatedNutritionOrder) => { setSelectedNutritionOrder(updatedNutritionOrder); }, onDelete: () => { setShowDisplayModal(false); setSelectedNutritionOrder(null); }, }); const viewNutritionOrder = (nutritionOrder: NutritionOrder) => { setSelectedNutritionOrder(nutritionOrder); setShowDisplayModal(true); }; const editNutritionOrder = ( event: React.MouseEvent, nutritionOrder: NutritionOrder, ) => { event.stopPropagation(); navigate(nutritionOrder.id as string, { relative: "path" }); }; const addNutritionOrder = () => { 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 NutritionOrder 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: 'NutritionOrder', viewerIsStaff: Boolean(userRole) && ![USER_ROLES.PATIENT, USER_ROLES.RELATED_PERSON].includes(userRole as string), practitionerRoles, }); const isAuthorizedToEdit = () => roleAllowsEdit() && seat.mayEdit; const getOrderDetails = (nutritionOrder: NutritionOrder): string => { const details = []; if (nutritionOrder.oralDiet) { details.push("Oral Diet"); } if (nutritionOrder.supplement && nutritionOrder.supplement.length > 0) { details.push( `${nutritionOrder.supplement.length} Supplement${ nutritionOrder.supplement.length !== 1 ? "s" : "" }`, ); } if (nutritionOrder.enteralFormula) { details.push("Enteral Formula"); } return details.join(", "); }; return (

{isPatientOrProxy ? "Diet & nutrition" : "Nutrition Orders"}

{viewerIsPatient ? 'Track and manage your personalized nutrition plans defined by your care team.' : "Track and manage this patient's nutrition plans, defined by their care team."}

{/* 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 Nutrition Orders

Try again later.

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

No Nutrition Orders found

{viewerIsPatient ? 'You have no active nutrition orders.' : 'No nutrition orders yet. Add an order for this patient.'}

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

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

{dietLines(nutritionOrder).length ? dietLines(nutritionOrder).map((line, index) => (

{line}

)) :

No diet details recorded

}
{isAuthorizedToEdit() && (
)}
); })}
) )} {selectedNutritionOrder && ( { if (!open) setSelectedNutritionOrder(null); }} > { setShowDisplayModal(false); setSelectedNutritionOrder(null); }} /> )}
); }; export default NutritionOrders; // import { useNavigate } from 'react-router-dom'; // import { NutritionOrder } from 'fhir/r4'; // import { AppSpinner, FhirNutritionOrderDisplay, ScrollableTab } from '../../../../components'; // import { // useFhirNutritionOrderBroadcast, // useFhirNutritionOrders, // useFhirNutritionOrderTargetedBroadcast // } from '../../../../hooks'; // import { useAuthServiceContext, useNutritionOrderServiceContext } from '../../../../services'; // const NutritionOrders = () => { // const navigate = useNavigate(); // const { userRole, USER_ROLES } = useAuthServiceContext(); // const { getNutritionOrderDisplay } = useNutritionOrderServiceContext(); // const { loading, error, clearError, nutritionOrdersList } = useFhirNutritionOrders(); // const [isGuideOpen, setIsGuideOpen] = useState(false); // const [selectedNutritionOrder, setSelectedNutritionOrder] = useState(null); // const [showDisplayModal, setShowDisplayModal] = useState(false); // const spacerMargin = '24px'; // useFhirNutritionOrderBroadcast(); // useFhirNutritionOrderTargetedBroadcast({ // nutritionOrder: selectedNutritionOrder || undefined, // isOpen: showDisplayModal, // onUpdate: (updatedNutritionOrder) => { // setSelectedNutritionOrder(updatedNutritionOrder); // }, // onDelete: () => { // setShowDisplayModal(false); // setSelectedNutritionOrder(null); // } // }); // const viewNutritionOrder = (nutritionOrder: NutritionOrder) => { // setSelectedNutritionOrder(nutritionOrder); // setShowDisplayModal(true); // }; // const editNutritionOrder = (event: React.MouseEvent, nutritionOrder: NutritionOrder) => { // event.stopPropagation(); // navigate(nutritionOrder.id as string, { relative: 'path' }); // }; // const addNutritionOrder = () => { // navigate('new', { relative: 'path' }); // }; // const isAuthorizedToEdit = () => { // return userRole && [USER_ROLES.PRACTITIONER].includes(userRole as string); // }; // const getStatusDisplay = (nutritionOrder: NutritionOrder): { text: string, color?: string } => { // if (!nutritionOrder) return { text: 'Unknown' }; // switch (nutritionOrder.status) { // case 'active': // return { text: 'Active', color: '#00a32a' }; // case 'draft': // return { text: 'Draft', color: '#757575' }; // case 'on-hold': // return { text: 'On Hold', color: '#e39700' }; // case 'revoked': // return { text: 'Revoked', color: '#cc1818' }; // case 'completed': // return { text: 'Completed', color: '#4f94d4' }; // case 'entered-in-error': // return { text: 'Error', color: '#cc1818' }; // case 'unknown': // default: // return { text: nutritionOrder.status?.charAt(0).toUpperCase() + nutritionOrder.status?.slice(1) || 'Unknown' }; // } // }; // const getOrderDetails = (nutritionOrder: NutritionOrder): string => { // const details = []; // if (nutritionOrder.oralDiet) { // details.push('Oral Diet'); // } // if (nutritionOrder.supplement && nutritionOrder.supplement.length > 0) { // details.push(`${nutritionOrder.supplement.length} Supplement${nutritionOrder.supplement.length !== 1 ? 's': ''}`); // } // if (nutritionOrder.enteralFormula) { // details.push('Enteral Formula'); // } // return details.join(', '); // }; // return ( // // setIsGuideOpen(false)} // resourceType="NutritionOrder"/> // // // // // // // {isAuthorizedToEdit() && ( // // // // )} // // // {error && ( // <> // // {error} // // // // )} // {!loading && nutritionOrdersList.length === 0 && ( // <> // // No nutrition orders found. // // // // )} // <> // {loading ? ( // // ): ( // // {nutritionOrdersList.length !== 0 && ( // // {nutritionOrdersList.map((nutritionOrder: NutritionOrder) => { // const displayTitle = getNutritionOrderDisplay(nutritionOrder); // const { text: statusText, color: statusColor } = getStatusDisplay(nutritionOrder); // const orderDetails = getOrderDetails(nutritionOrder); // return ( // viewNutritionOrder(nutritionOrder)}> // // // // // // // // // {displayTitle} // // {orderDetails && ( // // {orderDetails} // // )} // // {isAuthorizedToEdit() && ( // // // // )} // // // // // ); // })} // // )} // // )} // // {selectedNutritionOrder && ( // { // setShowDisplayModal(false); // setSelectedNutritionOrder(null); // }} // /> // )} // // ); // }; // export default NutritionOrders;