2 lines
7.8 KiB
JavaScript
2 lines
7.8 KiB
JavaScript
import{t as U,v as G,r as l,d as v,j as a,C as J,R as K,l as h,B as Q,m as X}from"./index-DGCPJq4z.js";import{D as Z}from"./DateField-CH86z_T4.js";import{P as _}from"./PageBreadcrumb-zFeQijFR.js";import{S as ee}from"./SlotSelect-Bk2AJPwg.js";import{u as te,b as V,f as E}from"./useSettings-YCBBpGwE.js";import{c as ae,C as se,a as ne,b as re,d as ie,s as oe}from"./CohortStatusField-BxNQFxkd.js";import{C as le,a as de}from"./Card-D1j94NWu.js";import{F as g}from"./FormLabel-DoSLr8ng.js";import{F as q}from"./FormControl-DGdX8GC2.js";import{F as ce}from"./FormCheck-tmQhdIFS.js";import"./PageMetaData-B-bWCH8Q.js";import"./Modal-DBjPCxyq.js";import"./FormContext-DDFZ16IF.js";const ue={name:"",startDate:"",slot:"",virtualPrt:"",language:"",diseaseType:"",scheduleId:"",patients:[]};function S(i){return i?Array.isArray(i.languages)&&i.languages.length?i.languages:i.language?[i.language]:[]:[]}function me(i,c){const j=new Set((c||[]).map(Number).filter(o=>o>0&&o<=6));if(!i)return"";const s=new Date(`${String(i).slice(0,10)}T00:00:00`);if(Number.isNaN(s.getTime())||!j.size)return"";for(let o=0;o<14;o+=1){if(j.has(s.getDay())){const f=y=>String(y).padStart(2,"0");return`${s.getFullYear()}-${f(s.getMonth()+1)}-${f(s.getDate())}`}s.setDate(s.getDate()+1)}return""}function we(){const{id:i}=U(),c=!!i,j=G(),[s,o]=l.useState(ue),[f,y]=l.useState([]),[F,A]=l.useState([]),[B,O]=l.useState([]),[C,M]=l.useState(""),[R,p]=l.useState(""),[b,I]=l.useState(!1),[x,L]=l.useState(null),[Y,P]=l.useState([]),[N,w]=l.useState(!1),{settings:m}=te(),D=m?.cohortStartWeekdays||[1,2],k=m?.cohortSchedules||[],T=k.find(e=>e.id===s.scheduleId),$=(T?.weekdays?.length?T.weekdays.map(Number):D).filter(e=>e!==0);l.useEffect(()=>{v("/api/users/virtual-prts").then(e=>y(e.items||[])).catch(()=>{})},[]),l.useEffect(()=>{const e=setTimeout(()=>{const t=new URLSearchParams({limit:"50",page:"1",reviewStatus:"confirmed"}),n=C.trim();n&&t.set("search",n),v(`/api/onboarding?${t}`).then(u=>A(u.items||[])).catch(()=>A([]))},300);return()=>clearTimeout(e)},[C]),l.useEffect(()=>{if(!c)return;let e=!0;return v(`/api/cohorts/${i}`).then(t=>{if(!e)return;const n=t.startDate?String(t.startDate).slice(0,10):"",u=V({slot:t.slot||"",language:t.language||"",startDate:n});o({name:t.name||"",startDate:n,slot:t.slot||"",virtualPrt:t.virtualPrt?.id||"",language:t.language||"",diseaseType:t.diseaseType||"",scheduleId:t.scheduleId||"",patients:(t.patients||[]).map(r=>r.id)}),w(!!(t.name&&t.name!==u)),O((t.patients||[]).map(r=>({id:r.id,name:r.name,mobile:r.mobile,whatsappOptOut:r.whatsappOptOut}))),L(ae(t))}).catch(t=>{e&&p(t.message)}),()=>{e=!1}},[i,c]),l.useEffect(()=>{const e=f.find(r=>r.id===s.virtualPrt),t=S(e),n=t.includes(s.language)?s.language:t[0]||"",u=V({slot:s.slot,language:n,startDate:s.startDate});o(r=>{const d={...r,language:n};return N||(d.name=u),r.name===d.name&&r.language===d.language?r:d})},[s.slot,s.startDate,s.virtualPrt,s.language,f,N]);const W=l.useMemo(()=>{const e=new Set,t=[];for(const n of F)!n?.id||e.has(n.id)||(e.add(n.id),t.push(n));for(const n of B)!n?.id||e.has(n.id)||!s.patients.includes(n.id)||t.push(n);return t},[F,B,s.patients]),z=e=>{O(t=>t.some(n=>n.id===e.id)?t:[...t,e]),o(t=>({...t,patients:t.patients.includes(e.id)?t.patients.filter(n=>n!==e.id):[...t.patients,e.id]}))},H=async(e,t=!1)=>{e?.preventDefault(),p("");const n=m?.maxPatientsPerCohort||10;if(s.patients.length>n){p(`A cohort can have at most ${n} patients.`);return}const u=re(x);if(u){p(u);return}if(c&&!t)try{const d=await ie(i,x);if(d.length){P(d);return}}catch(d){p(d.message);return}I(!0);const r=c?{...s}:{...s,createdAs:"cohorts"};try{c&&await v(`/api/cohorts/${i}`,{method:"PATCH",body:r}),c?await oe(i,x,{force:t}):await v("/api/cohorts",{method:"POST",body:r}),j("/cohorts")}catch(d){p(d.message)}finally{I(!1)}};return a.jsxs(J,{fluid:!0,children:[a.jsx(_,{title:c?"Edit cohort":"New cohort",subtitle:"Cohorts"}),a.jsx(le,{children:a.jsxs(de,{children:[R?a.jsx("div",{className:"alert alert-danger",children:R}):null,a.jsxs("p",{className:"text-muted",children:[T?`This schedule meets on ${E($)}.`:`Start date must be ${E(D)||"a configured weekday"}.`," Sessions are created each meeting day.",m?.skipHolidays!==!1||m?.skipPrtLeave!==!1?` Skipped ${[m?.skipHolidays!==!1?"holidays":null,m?.skipPrtLeave!==!1?"Virtual PRT leave":null].filter(Boolean).join(" and ")} do not count as a meeting.`:""]}),a.jsxs("form",{onSubmit:H,children:[a.jsxs(K,{className:"g-3",children:[a.jsxs(h,{md:3,children:[a.jsx(g,{children:"Cohort name"}),a.jsx(q,{value:s.name,onChange:e=>{w(!0),o(t=>({...t,name:e.target.value}))},onBlur:()=>{s.name.trim()||w(!1)},placeholder:"Created from slot, language, and start day"}),a.jsx("div",{className:"form-text",children:N?"Custom name — saved as typed. Clear the field and leave it to restore the default.":"Auto-filled from timeslot, language, and start day. Edit to set a custom name."})]}),a.jsxs(h,{md:3,children:[a.jsx(g,{children:"Schedule"}),a.jsxs("select",{className:"form-select",value:s.scheduleId,onChange:e=>{const t=e.target.value,n=k.find(r=>r.id===t),u=(n?.weekdays?.length?n.weekdays.map(Number):D).filter(r=>r!==0);o(r=>({...r,scheduleId:t,startDate:me(r.startDate,u)}))},children:[a.jsx("option",{value:"",children:"Normal (current)"}),k.map(e=>a.jsx("option",{value:e.id,children:e.name},e.id))]})]}),a.jsxs(h,{md:3,children:[a.jsx(g,{children:"Start date"}),a.jsx(Z,{value:s.startDate,onChange:e=>o(t=>({...t,startDate:e})),required:!0,filterDate:e=>e.getDay()!==0&&$.includes(e.getDay()),placeholder:E($)||"Select date"})]}),c&&x&&x.original!=="proposed"?a.jsx(h,{md:3,children:a.jsx(se,{cohortId:i,value:x,onChange:L})}):null,a.jsxs(h,{md:3,children:[a.jsx(g,{children:"Slot"}),a.jsx(ee,{value:s.slot,onChange:e=>o(t=>({...t,slot:e})),required:!0})]}),a.jsxs(h,{md:6,children:[a.jsx(g,{children:"Virtual PRT"}),a.jsxs("select",{className:"form-select",value:s.virtualPrt,onChange:e=>o(t=>({...t,virtualPrt:e.target.value,language:""})),required:!0,children:[a.jsx("option",{value:"",children:"Select PRT"}),f.map(e=>a.jsxs("option",{value:e.id,children:[e.firstName," ",e.lastName,S(e).length?` (${S(e).join(", ")})`:""]},e.id))]})]}),a.jsxs(h,{md:3,children:[a.jsx(g,{children:"Category"}),a.jsxs("select",{className:"form-select",value:s.diseaseType,onChange:e=>o(t=>({...t,diseaseType:e.target.value})),children:[a.jsx("option",{value:"",children:"Any / not set"}),(m?.diseaseTypes||[]).map(e=>a.jsx("option",{value:e,children:e},e))]})]}),a.jsxs(h,{md:3,children:[a.jsx(g,{children:"Language"}),a.jsxs("select",{className:"form-select",value:s.language,onChange:e=>o(t=>({...t,language:e.target.value})),required:!0,disabled:!s.virtualPrt,children:[a.jsx("option",{value:"",children:s.virtualPrt?"Select language":"Select a Virtual PRT first"}),S(f.find(e=>e.id===s.virtualPrt)).map(e=>a.jsx("option",{value:e,children:e},e))]})]}),a.jsxs(h,{xs:12,children:[a.jsx(g,{children:"Patients"}),a.jsxs("div",{className:"form-text mb-2",children:["Only patients confirmed by Admin can be added. Search by exact name or mobile. Maximum ",m?.maxPatientsPerCohort||10,"."]}),a.jsx(q,{className:"mb-2",placeholder:"Search exact name or mobile",value:C,onChange:e=>M(e.target.value)}),a.jsxs("div",{className:"border rounded p-3",style:{maxHeight:260,overflowY:"auto"},children:[W.length===0?a.jsx("div",{className:"text-muted",children:"No confirmed patients found."}):null,W.map(e=>a.jsx(ce,{type:"checkbox",id:`patient-${e.id}`,className:"mb-2",label:`${e.name} (${e.mobile})${e.whatsappOptOut?" · WhatsApp opt-out":""}`,checked:s.patients.includes(e.id),onChange:()=>z(e)},e.id))]})]})]}),a.jsxs("div",{className:"d-flex gap-2 mt-4",children:[a.jsx(Q,{type:"submit",disabled:b,children:b?"Saving...":"Save cohort"}),a.jsx(X,{className:"btn btn-light",to:"/cohorts",children:"Cancel"})]})]})]})}),a.jsx(ne,{warnings:Y,busy:b,onCancel:()=>P([]),onConfirm:()=>{P([]),H(null,!0)}})]})}export{we as default};
|