VirtualPR/frontend/dist/assets/form-C3gelC5r.js

2 lines
7.4 KiB
JavaScript

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