VirtualPR/frontend/dist/assets/index-C2JkeduJ.js

2 lines
14 KiB
JavaScript

import{r as l,d as C,j as e,R as O,l as w,B as h,b as je,D as fe,E as ve,F as W,m as Y,o as ye,p as Se,C as Ne,q as Ce,s as we}from"./index-B92aEPQH.js";import{c as Pe,u as Re,D as Be,g as ke,a as Me}from"./DataTable-bL18i-bk.js";import{P as Te}from"./PageBreadcrumb-Dsn9CkpH.js";import{s as Z,B as Ie,A as De,e as Ae,v as $}from"./SessionVitalsModal-DDMDFsin.js";import{M as H,a as U,b as z,c as Ee}from"./Modal-D4T7V4-p.js";import{F as Le}from"./FormCheck-QYGhbIhB.js";import{F as q}from"./FormControl-BhzYseFY.js";import{D as $e}from"./DeleteConfirmationModal-D9TSHum9.js";import{T as Oe}from"./TablePagination-CZupPUhN.js";import{u as He}from"./useSettings-B5rMnTFQ.js";import{C as Ue,b as ze,c as Fe}from"./Card-DPmHd5BI.js";import"./Table-BCHyhTAY.js";import"./PageMetaData-DFmRpQLs.js";import"./FormContext-IkkKm1bh.js";const ee=r=>r??"";function Ve({patient:r,onClose:g}){const[S,b]=l.useState([]),[c,k]=l.useState(!1),[M,j]=l.useState(!1),[T,p]=l.useState("");l.useEffect(()=>{if(!r?.id)return;let a=!0;return k(!0),p(""),C(`/api/onboarding/${r.id}/vitals`).then(i=>{a&&b((i.items||[]).map(n=>({...n,pre:{...n.pre,...Z(n.pre?.bp)},post:{...n.post,...Z(n.post?.bp)}})))}).catch(i=>{a&&p(i.message)}).finally(()=>{a&&k(!1)}),()=>{a=!1}},[r?.id]);const o=(a,i)=>b(n=>n.map((d,f)=>f===a?{...d,...i}:d)),N=async()=>{j(!0),p("");try{const a=S.map(i=>({kind:i.kind,number:i.number,sessionId:i.sessionId,attended:!!i.attended,pre:i.attended?$(i.pre):$({}),post:i.attended?$(i.post):$({}),assessment:i.attended?i.assessment||{}:Ae()}));await C(`/api/onboarding/${r.id}/vitals`,{method:"PUT",body:{rows:a}}),g()}catch(a){p(a.message)}finally{j(!1)}};return e.jsxs(H,{show:!!r,onHide:g,size:"lg",children:[e.jsx(U,{closeButton:!0,children:e.jsxs("h5",{className:"modal-title",children:["Vitals · ",r?.name]})}),e.jsxs(z,{children:[T?e.jsx("div",{className:"alert alert-danger",children:T}):null,c?e.jsx("p",{className:"text-muted mb-0",children:"Loading…"}):null,!c&&!S.length?e.jsx("p",{className:"text-muted mb-0",children:"No completed sessions yet."}):null,S.map((a,i)=>e.jsxs("div",{className:"border rounded p-3 mb-3",children:[e.jsxs("div",{className:"d-flex justify-content-between align-items-center mb-2",children:[e.jsxs("strong",{children:["Session ",a.number||"—",e.jsx("span",{className:"text-muted small ms-2",children:[a.date,a.cohort].filter(Boolean).join(" · ")})]}),e.jsx(Le,{type:"switch",label:"Attended",checked:!!a.attended,onChange:n=>o(i,{attended:n.target.checked})})]}),e.jsx(O,{className:"g-2",children:["pre","post"].map(n=>e.jsxs(w,{md:6,children:[e.jsx("div",{className:"text-muted text-uppercase small mb-1",children:n}),e.jsxs(O,{className:"g-2",children:[e.jsx(w,{xs:6,children:e.jsx(Ie,{value:a[n],disabled:!a.attended,onChange:d=>o(i,{[n]:d})})}),e.jsx(w,{children:e.jsx(q,{placeholder:"SpO2",type:"number",value:ee(a[n]?.spo2),disabled:!a.attended,onChange:d=>o(i,{[n]:{...a[n],spo2:d.target.value}})})}),e.jsx(w,{children:e.jsx(q,{placeholder:"Pulse",type:"number",value:ee(a[n]?.pulse),disabled:!a.attended,onChange:d=>o(i,{[n]:{...a[n],pulse:d.target.value}})})})]})]},n))}),[15,30].includes(Number(a.number))?e.jsx(O,{className:"g-2 mt-2",children:De.map(([n,d])=>e.jsx(w,{md:4,children:e.jsx(q,{placeholder:d,value:a.assessment?.[n]||"",disabled:!a.attended,onChange:f=>o(i,{assessment:{...a.assessment,[n]:f.target.value}})})},n))}):null]},`${a.kind}-${a.sessionId||a.number}`))]}),e.jsxs(Ee,{children:[e.jsx(h,{variant:"light",onClick:g,children:"Close"}),e.jsx(h,{onClick:N,disabled:M||!S.length,children:M?"Saving…":"Save vitals"})]})]})}function qe(r){return r?String(r).slice(0,10):""}function Ke(r){return r==="confirmed"?e.jsx("span",{className:"badge bg-success-subtle text-success",children:"Confirmed"}):r==="rejected"?e.jsx("span",{className:"badge bg-danger-subtle text-danger",children:"Rejected"}):e.jsx("span",{className:"badge bg-warning-subtle text-warning",children:"Pending"})}async function se(r){const g=await fetch(`/api/onboarding/${r}/screenshot`,{credentials:"include"});if(g.status===401&&(await fetch("/api/auth/refresh",{method:"POST",credentials:"include"})).ok)return se(r);if(!g.ok)throw new Error("Could not open screenshot");if((g.headers.get("content-type")||"").includes("application/json")){const c=await g.json();if(!c?.url)throw new Error("Could not open screenshot");return{url:c.url,blob:!1}}const b=await g.blob();return{url:URL.createObjectURL(b),blob:!0}}const B={id:"",cohortId:"",warnings:[],error:"",busy:!1};function is(){const{can:r,permissions:g,user:S}=je(),[b]=fe(),c=g.isSuperAdmin||S?.role?.systemKey==="admin",[k,M]=l.useState([]),[j,T]=l.useState(0),[p,o]=l.useState(0),[N]=l.useState(10),[a,i]=l.useState(""),[n,d]=l.useState(""),[f,te]=l.useState(b.get("list")||""),[I,ae]=l.useState(b.get("paymentStatus")||""),[D,ne]=l.useState(b.get("category")||""),[A,le]=l.useState(b.get("addedBy")||""),[ie,re]=l.useState([]),{settings:K}=He(),[P,R]=l.useState({id:"",reason:""}),[m,v]=l.useState(B),[oe,Q]=l.useState(null),[de,ce]=l.useState([]),[X,F]=l.useState(null),[V,_]=l.useState(null),[u,E]=l.useState({url:"",title:"",loading:!1}),x=Pe(),L=async()=>{const s=new URLSearchParams({search:n,page:String(p+1),limit:String(N)});f&&s.set("list",f),I&&s.set("paymentStatus",I),D&&s.set("diseaseType",D),A&&s.set("onboardedBy",A);const t=await C(`/api/onboarding?${s}`);M(t.items||[]),T(t.total||0),Array.isArray(t.addedBy)&&re(t.addedBy)},me=async s=>{v(t=>({...t,busy:!0,error:""}));try{await C(`/api/onboarding/${m.id}/migrate`,{method:"POST",body:{cohortId:m.cohortId,force:s}}),v(B),await L()}catch(t){t.status===409&&t.data?.warnings?.length?v(y=>({...y,busy:!1,warnings:t.data.warnings})):v(y=>({...y,busy:!1,error:t.message}))}};l.useEffect(()=>{L().catch(()=>{})},[p,n,f,I,D,A]),l.useEffect(()=>{c&&C("/api/onboarding/available-cohorts").then(s=>ce(s.items||[])).catch(()=>{})},[c]);const ue=()=>{u.blob&&u.url&&URL.revokeObjectURL(u.url),E({url:"",title:"",loading:!1,blob:!1})},G=async s=>{u.blob&&u.url&&URL.revokeObjectURL(u.url),E({url:"",title:s.name||"Receipt",loading:!0,blob:!1});try{const t=await se(s.id);E({url:t.url,title:s.name||"Receipt",loading:!1,blob:t.blob})}catch{E({url:"",title:"",loading:!1,blob:!1}),window.alert("Could not open screenshot")}},J=async(s,t,y)=>{_(s);try{await C(`/api/onboarding/${s}/review`,{method:"POST",body:{status:t,reason:y}}),R({id:"",reason:""}),await L()}catch(be){window.alert(be.message)}finally{_(null)}},he=l.useMemo(()=>[x.accessor("name",{header:"Name"}),x.accessor("mobile",{header:"Mobile"}),x.accessor(s=>s.cohort?.name||(s.matchingStatus==="held"?"Held":"Pool"),{id:"cohort",header:"Cohort"}),x.accessor(s=>s.diseaseType||"—",{id:"diseaseType",header:"Category"}),x.accessor(s=>qe(s.progress?.firstDate||s.cohort?.startDate||s.sessionDate),{id:"sessionDate",header:"Start date"}),x.accessor("slot",{header:"Slot"}),x.accessor("paymentStatus",{header:"Payment"}),x.accessor(s=>s.progress?.completed??0,{id:"progress",header:"Sessions completed"}),x.accessor(s=>s.reviewStatus||"pending",{id:"reviewStatus",header:"Status",cell:({row:s})=>Ke(s.original.reviewStatus)}),x.accessor(s=>s.onboardedBy?`${s.onboardedBy.firstName} ${s.onboardedBy.lastName}`:"",{id:"onboardedBy",header:"Added by"}),{header:"Receipt",cell:({row:s})=>s.original.hasScreenshot?e.jsx(h,{variant:"link",className:"p-0",onClick:()=>G(s.original),children:"View"}):"—"},{header:"Actions",cell:({row:s})=>{const t=s.original,y=t.reviewStatus==="confirmed";return e.jsxs("div",{className:"d-flex flex-wrap gap-1",children:[c&&!y?e.jsx(h,{variant:"success",size:"sm",title:"Confirm",disabled:V===t.id,onClick:()=>J(t.id,"confirmed"),children:e.jsx(ve,{className:"fs-lg"})}):null,c&&t.reviewStatus!=="rejected"?e.jsx(h,{variant:"outline-danger",size:"sm",title:"Reject",disabled:V===t.id,onClick:()=>R({id:t.id,reason:""}),children:e.jsx(W,{className:"fs-lg"})}):null,t.reviewStatus==="confirmed"?e.jsx(h,{variant:"outline-primary",size:"sm",onClick:()=>Q({id:t.id,name:t.name}),children:"Vitals"}):null,c&&t.reviewStatus==="confirmed"?e.jsx(h,{variant:"outline-secondary",size:"sm",onClick:()=>v({...B,id:t.id,cohortId:t.cohort?.id||""}),children:"Move"}):null,r("onboarding","edit")?e.jsx(Y,{className:"btn btn-default btn-sm btn-icon",to:`/onboarding/${t.id}/edit`,title:"Edit",children:e.jsx(ye,{className:"fs-lg"})}):null,!y&&r("onboarding","delete")?e.jsx(h,{variant:"default",size:"sm",className:"btn-icon",title:"Delete",onClick:()=>F(t.id),children:e.jsx(Se,{className:"fs-lg"})}):null]})}}],[r,c,V,G]),pe=Re({data:k,columns:he,getCoreRowModel:Me(),getSortedRowModel:ke()}),ge=j===0?0:p*N+1,xe=Math.min((p+1)*N,j);return e.jsxs(Ne,{fluid:!0,children:[e.jsx(Te,{title:"Patients"}),e.jsx(O,{children:e.jsx(w,{children:e.jsxs(Ue,{children:[e.jsx(ze,{className:"border-light",children:e.jsxs("div",{className:"d-flex flex-wrap gap-2 align-items-center w-100",children:[e.jsxs("form",{className:"d-flex flex-wrap gap-2 align-items-center flex-grow-1",onSubmit:s=>{s.preventDefault(),o(0),d(a)},children:[e.jsxs("div",{className:"app-search flex-shrink-0",style:{width:240},children:[e.jsx("input",{className:"form-control",placeholder:"Search name or mobile...",value:a,style:a?{paddingRight:32}:void 0,onChange:s=>{const t=s.target.value;i(t),t.trim()||(o(0),d(""))}}),e.jsx(Ce,{className:"app-search-icon text-muted"}),a?e.jsx("button",{type:"button",className:"btn btn-link position-absolute top-50 translate-middle-y end-0 text-muted p-0 pe-2","aria-label":"Clear search",onClick:()=>{i(""),o(0),d("")},children:e.jsx(W,{})}):null]}),e.jsx(h,{type:"submit",variant:"light",children:"Search"}),e.jsxs("select",{className:"form-select",style:{width:150},value:f,onChange:s=>{o(0),te(s.target.value)},children:[e.jsx("option",{value:"",children:"All status"}),e.jsx("option",{value:"pending",children:"Pending"}),e.jsx("option",{value:"confirmed",children:"Confirmed"}),e.jsx("option",{value:"pool",children:"Pool"}),e.jsx("option",{value:"assigned",children:"Assigned"}),e.jsx("option",{value:"rejected",children:"Rejected"})]}),e.jsxs("select",{className:"form-select",style:{width:150},value:I,onChange:s=>{o(0),ae(s.target.value)},children:[e.jsx("option",{value:"",children:"Payment status"}),(K?.paymentStatuses||["Unpaid","Paid","Partial"]).map(s=>e.jsx("option",{value:s,children:s},s))]}),e.jsxs("select",{className:"form-select",style:{width:140},value:D,onChange:s=>{o(0),ne(s.target.value)},children:[e.jsx("option",{value:"",children:"Category"}),(K?.diseaseTypes||["ILD","OAD"]).map(s=>e.jsx("option",{value:s,children:s},s))]}),e.jsxs("select",{className:"form-select",style:{width:170},value:A,onChange:s=>{o(0),le(s.target.value)},children:[e.jsx("option",{value:"",children:"Added by"}),ie.map(s=>e.jsx("option",{value:s.id,children:s.name||`${s.firstName} ${s.lastName}`},s.id))]})]}),r("onboarding","add")?e.jsxs(Y,{className:"btn btn-success flex-shrink-0",to:"/onboarding/new",children:[e.jsx(we,{className:"me-1"})," Add patient"]}):null]})}),e.jsx(Be,{table:pe,emptyMessage:"No patients yet"}),j>0&&e.jsx(Fe,{className:"border-0",children:e.jsx(Oe,{totalItems:j,start:ge,end:xe,itemsName:"patients",showInfo:!0,previousPage:()=>o(s=>Math.max(0,s-1)),canPreviousPage:p>0,pageCount:Math.ceil(j/N),pageIndex:p,setPageIndex:o,nextPage:()=>o(s=>s+1),canNextPage:(p+1)*N<j})})]})})}),e.jsxs(H,{show:!!(u.loading||u.url),onHide:ue,centered:!0,size:"lg",children:[e.jsx(U,{closeButton:!0,children:e.jsx("h5",{className:"modal-title",children:u.title||"Receipt"})}),e.jsxs(z,{className:"text-center",children:[u.loading?e.jsx("div",{className:"text-muted py-5",children:"Loading receipt..."}):null,u.url?e.jsx("img",{src:u.url,alt:u.title||"Receipt",className:"img-fluid rounded"}):null]})]}),e.jsxs(H,{show:!!P.id,onHide:()=>R({id:"",reason:""}),children:[e.jsx(U,{closeButton:!0,children:e.jsx("h5",{className:"modal-title",children:"Reject patient"})}),e.jsxs(z,{children:[e.jsx("label",{className:"form-label",children:"Reason"}),e.jsx("textarea",{className:"form-control",rows:3,value:P.reason,onChange:s=>R(t=>({...t,reason:s.target.value}))})]}),e.jsxs("div",{className:"modal-footer",children:[e.jsx(h,{variant:"light",onClick:()=>R({id:"",reason:""}),children:"Cancel"}),e.jsx(h,{variant:"danger",disabled:!P.reason.trim(),onClick:()=>J(P.id,"rejected",P.reason),children:"Reject"})]})]}),e.jsxs(H,{show:!!m.id,onHide:()=>v(B),children:[e.jsx(U,{closeButton:!0,children:e.jsx("h5",{className:"modal-title",children:"Move patient"})}),e.jsxs(z,{children:[m.error?e.jsx("div",{className:"alert alert-danger",children:m.error}):null,e.jsx("label",{className:"form-label",children:"Target cohort"}),e.jsxs("select",{className:"form-select",value:m.cohortId,onChange:s=>v(t=>({...t,cohortId:s.target.value,warnings:[],error:""})),children:[e.jsx("option",{value:"",children:"Select cohort"}),de.map(s=>e.jsx("option",{value:s.id,children:s.name},s.id))]}),e.jsx("div",{className:"form-text",children:"The patient takes this cohort's slot, day and PRT."}),m.warnings.length?e.jsxs("div",{className:"alert alert-warning mt-3 mb-0",children:[e.jsx("div",{className:"fw-semibold mb-1",children:"This cohort does not fully match:"}),e.jsx("ul",{className:"mb-1 ps-3",children:m.warnings.map(s=>e.jsx("li",{children:s},s))}),"Do you still want to move this patient?"]}):null]}),e.jsxs("div",{className:"modal-footer",children:[e.jsx(h,{variant:"light",onClick:()=>v(B),children:"Cancel"}),e.jsx(h,{variant:m.warnings.length?"warning":"primary",disabled:!m.cohortId||m.busy,onClick:()=>me(m.warnings.length>0),children:m.warnings.length?"Move anyway":"Move"})]})]}),e.jsx(Ve,{patient:oe,onClose:()=>Q(null)}),e.jsx($e,{show:!!X,onHide:()=>F(null),itemName:"patient",selectedCount:1,onConfirm:async()=>{await C(`/api/onboarding/${X}`,{method:"DELETE"}),F(null),await L()}})]})}export{is as default};