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

2 lines
6.9 KiB
JavaScript

import{b as _,r as t,j as s,m as N,B as d,d as h,o as U,p as q,C as V,R as K,l as Q,q as G,s as $}from"./index-DGCPJq4z.js";import{c as J,u as X,D as Y,g as Z,a as ee}from"./DataTable-JvKU9cjO.js";import{P as se}from"./PageBreadcrumb-zFeQijFR.js";import{D as ae}from"./DeleteConfirmationModal-DValqyOH.js";import{T as te}from"./TablePagination-ozYSophQ.js";import{a as re}from"./dates-DzZ1gfzQ.js";import{C as oe,b as le,c as ne}from"./Card-D1j94NWu.js";import{M as ie,a as ce,b as de,c as he}from"./Modal-DBjPCxyq.js";import"./Table-DqFE0W6i.js";import"./PageMetaData-B-bWCH8Q.js";function me(l){return re(l)}function ve(){const{can:l,user:A}=_(),R=A?.role?.systemKey==="educator",f=(e,r)=>l((e||"cohorts")==="manual_cohorts"?"prt_cohorts":"cohorts",r),[B,D]=t.useState([]),[m,O]=t.useState(0),[i,u]=t.useState(0),[x]=t.useState(10),[C,E]=t.useState(""),[S,I]=t.useState(""),[b,H]=t.useState(""),[P,v]=t.useState(null),[a,c]=t.useState(null),[T,n]=t.useState(""),[w,y]=t.useState(""),o=J(),g=async()=>{const e=new URLSearchParams({search:S,page:String(i+1),limit:String(x)});b&&e.set("status",b);const r=await h(`/api/cohorts?${e}`);D(r.items||[]),O(r.total||0)};t.useEffect(()=>{g().catch(()=>{})},[i,S,b]);const z=t.useMemo(()=>[o.accessor("name",{header:"Cohort",cell:({row:e})=>f(e.original.createdAs,"edit")?s.jsx(N,{to:`/cohorts/${e.original.id}/edit`,className:"link-reset fw-semibold",children:e.original.name}):s.jsx("span",{className:"fw-semibold",children:e.original.name})}),o.accessor(e=>me(e.startDate),{id:"startDate",header:"Start date"}),o.accessor("slot",{header:"Slot"}),o.accessor(e=>e.virtualPrt?`${e.virtualPrt.firstName} ${e.virtualPrt.lastName}`:"",{id:"virtualPrt",header:"Virtual PRT"}),o.accessor(e=>e.status||"active",{id:"status",header:"Status",cell:({getValue:e})=>s.jsx("span",{className:"badge bg-primary-subtle text-primary text-capitalize",children:e()})}),o.accessor(e=>e.diseaseType||"—",{id:"diseaseType",header:"Category"}),o.accessor("patientCount",{header:"Patients"}),o.accessor(e=>e.sessionsCompleted||0,{id:"sessions",header:"Sessions"}),o.accessor(e=>e.whatsapp?.status||"—",{id:"whatsapp",header:"WhatsApp",cell:({row:e,getValue:r})=>s.jsx(d,{variant:"link",className:"p-0 text-capitalize",onClick:async()=>{n("");try{const p=await h(`/api/cohorts/${e.original.id}`);c(p)}catch(p){n(p.message),c(e.original)}},children:r()||"—"})}),{header:"Actions",cell:({row:e})=>s.jsxs("div",{className:"d-flex gap-1",children:[f(e.original.createdAs,"edit")?s.jsx(N,{className:"btn btn-default btn-icon btn-sm",to:`/cohorts/${e.original.id}/edit`,children:s.jsx(U,{className:"fs-lg"})}):null,f(e.original.createdAs,"delete")?s.jsx(d,{variant:"default",size:"sm",className:"btn-icon",onClick:()=>v(e.original.id),children:s.jsx(q,{className:"fs-lg"})}):null]})}],[l]),W=X({data:B,columns:z,getCoreRowModel:ee(),getSortedRowModel:Z()}),F=m===0?0:i*x+1,L=Math.min((i+1)*x,m);return s.jsxs(V,{fluid:!0,children:[s.jsx(se,{title:"Cohorts"}),s.jsx(K,{children:s.jsx(Q,{children:s.jsxs(oe,{children:[s.jsx(le,{className:"border-light",children:s.jsxs("div",{className:"d-flex flex-nowrap gap-2 align-items-center w-100",children:[s.jsxs("form",{className:"d-flex flex-nowrap gap-2 align-items-center flex-grow-1",onSubmit:e=>{e.preventDefault(),u(0),I(C)},children:[s.jsxs("div",{className:"app-search flex-shrink-0",style:{width:240},children:[s.jsx("input",{className:"form-control",placeholder:"Search cohorts...",value:C,onChange:e=>E(e.target.value)}),s.jsx(G,{className:"app-search-icon text-muted"})]}),s.jsx(d,{type:"submit",variant:"light",children:"Search"}),s.jsxs("select",{className:"form-select",style:{width:160},value:b,onChange:e=>{u(0),H(e.target.value)},children:[s.jsx("option",{value:"",children:"All live"}),s.jsx("option",{value:"proposed",children:"Proposed"}),s.jsx("option",{value:"active",children:"Active"}),s.jsx("option",{value:"completed",children:"Completed"}),s.jsx("option",{value:"cancelled",children:"Cancelled"}),s.jsx("option",{value:"all",children:"All"})]})]}),l("prt_cohorts","add")?s.jsxs(N,{className:"btn btn-success flex-shrink-0",to:"/cohorts/prt/new",children:[s.jsx($,{className:"me-1"})," Add PRT cohorts"]}):null,l("cohorts","add")&&!R?s.jsxs(N,{className:"btn btn-success flex-shrink-0",to:"/cohorts/new",children:[s.jsx($,{className:"me-1"})," New cohort"]}):null]})}),s.jsx(Y,{table:W,emptyMessage:"No cohorts yet"}),m>0&&s.jsx(ne,{className:"border-0",children:s.jsx(te,{totalItems:m,start:F,end:L,itemsName:"cohorts",showInfo:!0,previousPage:()=>u(e=>Math.max(0,e-1)),canPreviousPage:i>0,pageCount:Math.ceil(m/x),pageIndex:i,setPageIndex:u,nextPage:()=>u(e=>e+1),canNextPage:(i+1)*x<m})})]})})}),s.jsxs(ie,{show:!!a,onHide:()=>c(null),children:[s.jsx(ce,{closeButton:!0,children:s.jsxs("h5",{className:"modal-title",children:["WhatsApp · ",a?.name]})}),s.jsxs(de,{children:[T?s.jsx("div",{className:"alert alert-danger",children:T}):null,s.jsxs("p",{className:"mb-1 text-capitalize",children:["Status: ",a?.whatsapp?.status||"not created"]}),a?.whatsapp?.error?s.jsx("p",{className:"text-danger",children:a.whatsapp.error}):null,a?.whatsapp?.inviteUrl?s.jsx("p",{children:s.jsx("a",{href:a.whatsapp.inviteUrl,target:"_blank",rel:"noreferrer",children:"Invite link"})}):null,s.jsx("ul",{className:"mb-0",children:(a?.patients||[]).map(e=>{const r=(a.whatsapp?.members||[]).find(j=>j.patientId===e.id),p=Number(e.sessionsPaid??30),k=Number(e.sessionsCompleted??0),M=k>=p;return s.jsxs("li",{className:`mb-2${M?" text-danger fw-semibold":""}`,children:[e.name,M?` · ${k}/${p} sessions`:""," · ",e.whatsappOptOut?"opted out":r?.status||"not synced",r?.error?` · ${r.error}`:"",l("onboarding","edit")?s.jsx(d,{size:"sm",variant:"link",className:"py-0",onClick:async()=>{n("");try{await h(`/api/onboarding/${e.id}/whatsapp`,{method:"PATCH",body:{optOut:!e.whatsappOptOut}});const j=await h(`/api/cohorts/${a.id}`);c(j),await g()}catch(j){n(j.message)}},children:e.whatsappOptOut?"Opt in":"Opt out"}):null]},e.id)})}),(a?.patients||[]).length?null:s.jsx("div",{className:"text-muted",children:"No patients on this cohort."})]}),s.jsxs(he,{children:[f(a?.createdAs,"edit")?s.jsxs(s.Fragment,{children:[s.jsx(d,{variant:"outline-secondary",disabled:!!w,onClick:async()=>{y("retry"),n("");try{const e=await h(`/api/cohorts/${a.id}/whatsapp/retry`,{method:"POST"});c(e),await g()}catch(e){n(e.message)}finally{y("")}},children:w==="retry"?"Retrying…":"Retry sync"}),["active","confirmed"].includes(a?.status)?s.jsx(d,{variant:"outline-primary",disabled:!!w,onClick:async()=>{y("complete"),n("");try{const e=await h(`/api/cohorts/${a.id}/complete`,{method:"POST"});c(e),await g()}catch(e){n(e.message)}finally{y("")}},children:w==="complete"?"Completing…":"Complete cohort"}):null]}):null,s.jsx(d,{variant:"light",onClick:()=>c(null),children:"Close"})]})]}),s.jsx(ae,{show:!!P,onHide:()=>v(null),itemName:"cohort",selectedCount:1,onConfirm:async()=>{await h(`/api/cohorts/${P}`,{method:"DELETE"}),v(null),await g()}})]})}export{ve as default};