VirtualPR/frontend/dist/assets/prt-form-BS1zksXe.js

2 lines
10 KiB
JavaScript

import{t as ue,v as ce,b as me,r as i,d as D,j as e,C as fe,R as ge,l as f,B as I,s as he,p as xe,m as pe}from"./index-DGCPJq4z.js";import{D as je}from"./DateField-CH86z_T4.js";import{O as ye}from"./OptionsSelect-Mr65YEpO.js";import{P as Se}from"./PageBreadcrumb-zFeQijFR.js";import{S as be}from"./SlotSelect-Bk2AJPwg.js";import{u as ve,b as ae}from"./useSettings-YCBBpGwE.js";import{c as Ce,C as Pe,a as Ne,b as De,d as ke,s as Te}from"./CohortStatusField-BxNQFxkd.js";import{m as Re,i as ne}from"./mobile-CfiPsqNQ.js";import{C as Ae,a as Ee}from"./Card-D1j94NWu.js";import{F as h}from"./FormLabel-DoSLr8ng.js";import{F as j}from"./FormControl-DGdX8GC2.js";import{F as ie}from"./FormCheck-tmQhdIFS.js";import"./PageMetaData-B-bWCH8Q.js";import"./Modal-DBjPCxyq.js";import"./FormContext-DDFZ16IF.js";function L(l){return Array.isArray(l?.languages)&&l.languages.length?l.languages:l?.language?[l.language]:[]}const Oe={name:"",mobile:"",age:"",gender:"",offlineSessions:"",offlineStartDate:"",sessionsPaid:30,patientStatus:"active"};function Fe(){return{key:`${Date.now()}-${Math.random()}`,id:"",existing:!1,...Oe}}function le(l,d){const S=l.getDay();return S===0?!1:d==="yes"?!0:d==="no"?S===1||S===2:!1}function Qe(){const{id:l}=ue(),d=!!l,S=ce(),{user:k}=me(),{settings:M}=ve(),x=k?.role?.systemKey==="educator",[q,z]=i.useState([]),[T,G]=i.useState(""),[C,H]=i.useState(""),[c,K]=i.useState(""),[p,R]=i.useState(""),[Y,A]=i.useState(""),[_,J]=i.useState(""),[E,O]=i.useState(""),[F,V]=i.useState(!1),[m,P]=i.useState([]),[Q,r]=i.useState(""),[$,U]=i.useState(!1),[y,X]=i.useState(null),[re,W]=i.useState([]),oe=x?k:q.find(s=>s.id===T),b=L(oe),B=x?k?.id:T,v=b.includes(Y)?Y:b[0]||"";i.useEffect(()=>{if(x)return;let s=!0;return D("/api/users/virtual-prts").then(n=>{s&&z(n.items||[])}).catch(()=>{s&&z([])}),()=>{s=!1}},[x]),i.useEffect(()=>{if(!d)return;let s=!0;return D(`/api/cohorts/${l}`).then(n=>{if(!s)return;const o=n.startDate?String(n.startDate).slice(0,10):"",u=ae({slot:n.slot||"",language:n.language||"",startDate:o});H(n.slot||""),R(o),A(n.language||""),J(n.diseaseType||""),O(n.name||""),V(!!(n.name&&n.name!==u)),G(n.virtualPrt?.id||""),K(n.sixDayWeek?"yes":"no"),X(Ce(n)),P((n.patients||[]).map(a=>({key:a.id,id:a.id,existing:!0,name:a.name||"",mobile:a.mobile||"",age:a.age??"",gender:a.gender||"",originalMobile:a.mobile||"",offlineSessions:a.offlineSessions??0,offlineStartDate:a.offlineStartDate||"",sessionsPaid:a.sessionsPaid??30,patientStatus:a.patientStatus||"active",sessionsCompleted:a.sessionsCompleted??0})))}).catch(n=>{s&&r(n.message)}),()=>{s=!1}},[l,d]),i.useEffect(()=>{F||O(ae({slot:C,language:v,startDate:p}))},[C,p,v,F]);const Z=s=>{if(K(s),!p)return;const n=new Date(`${p}T00:00:00`);le(n,s)||R("")},g=(s,n)=>{P(o=>o.map(u=>u.key===s?{...u,...n}:u))},w=async(s,n=!1)=>{if(s?.preventDefault(),r(""),!B){r("Select a Virtual PRT.");return}if(c!=="yes"&&c!=="no"){r("Choose yes or no for all weekdays.");return}if(!p){r("Select a start date.");return}if(!v){r(x?"Add a language on your profile before creating a cohort.":"Select a language this Virtual PRT can teach.");return}const o=m.filter(t=>!t.existing&&(t.name.trim()||t.mobile.trim()||String(t.age||"").trim()||t.gender)),u=o.findIndex(t=>!t.name.trim()||!ne(t.mobile));if(u>=0){r(`Row ${m.indexOf(o[u])+1} needs a name and a 10 digit mobile.`);return}const a=o.findIndex(t=>t.offlineSessions===""||!(Number(t.offlineSessions)>=0));if(a>=0){r(`Row ${m.indexOf(o[a])+1} needs Offline completed (0 or more).`);return}const N=m.findIndex(t=>t.existing&&(t.offlineSessions===""||!(Number(t.offlineSessions)>=0)));if(N>=0){r(`Row ${N+1} needs Offline completed (0 or more).`);return}const ee=m.findIndex(t=>t.existing&&(!t.name.trim()||t.mobile!==t.originalMobile&&!ne(t.mobile)));if(ee>=0){r(`Row ${ee+1} needs a name and a 10 digit mobile.`);return}const de=o.map(t=>({name:t.name.trim(),mobile:t.mobile.trim(),age:t.age===""||t.age==null?"":t.age,gender:t.gender||"",whatsappOptOut:!1,offlineSessions:Number(t.offlineSessions),offlineStartDate:t.offlineStartDate||"",sessionsPaid:t.sessionsPaid===""||t.sessionsPaid==null?30:Number(t.sessionsPaid),patientStatus:t.patientStatus||"active"})),se=De(y);if(se){r(se);return}if(d&&!n)try{const t=await ke(l,y);if(t.length){W(t);return}}catch(t){r(t.message);return}U(!0);const te={name:E.trim(),startDate:p,slot:C,virtualPrt:B,language:v,diseaseType:_,patients:m.filter(t=>t.existing&&t.id).map(t=>t.id),newPatients:de,patientEdits:m.filter(t=>t.existing&&t.id).map(t=>({id:t.id,name:t.name.trim(),mobile:t.mobile.trim(),age:t.age===""||t.age==null?"":t.age,gender:t.gender||"",offlineSessions:Number(t.offlineSessions)||0,offlineStartDate:t.offlineStartDate||"",sessionsPaid:t.sessionsPaid===""||t.sessionsPaid==null?30:Number(t.sessionsPaid),patientStatus:t.patientStatus||"active"})),scheduleId:"",createdAs:"manual_cohorts",sixDayWeek:c==="yes"};try{d&&await D(`/api/cohorts/${l}`,{method:"PATCH",body:te}),d?await Te(l,y,{force:n}):await D("/api/cohorts",{method:"POST",body:te}),S("/cohorts")}catch(t){r(t.message)}finally{U(!1)}};return e.jsxs(fe,{fluid:!0,children:[e.jsx(Se,{title:d?"Edit cohort":"Add PRT cohorts",subtitle:"Cohorts"}),e.jsx(Ae,{children:e.jsxs(Ee,{children:[Q?e.jsx("div",{className:"alert alert-danger",children:Q}):null,e.jsxs("form",{onSubmit:w,children:[e.jsxs(ge,{className:"g-3",children:[e.jsxs(f,{md:3,children:[e.jsx(h,{children:"Cohort name"}),e.jsx(j,{value:E,onChange:s=>{V(!0),O(s.target.value)},onBlur:()=>{E.trim()||V(!1)},placeholder:"Created from slot, language, and start day"}),e.jsx("div",{className:"form-text",children:F?"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."})]}),e.jsxs(f,{md:3,children:[e.jsx(h,{children:"Slot"}),e.jsx(be,{value:C,onChange:H,required:!0})]}),e.jsxs(f,{md:3,children:[e.jsx(h,{children:"All weekdays"}),e.jsxs("div",{className:"d-flex gap-3 pt-2",children:[e.jsx(ie,{type:"radio",name:"sixDayWeek",id:"six-day-yes",label:"Yes",checked:c==="yes",onChange:()=>Z("yes")}),e.jsx(ie,{type:"radio",name:"sixDayWeek",id:"six-day-no",label:"No",checked:c==="no",onChange:()=>Z("no")})]})]}),e.jsxs(f,{md:3,children:[e.jsx(h,{children:"Start date"}),e.jsx(je,{value:p,onChange:R,required:!0,disabled:!c,filterDate:s=>le(s,c),placeholder:c==="yes"?"Any day except Sunday":c==="no"?"Monday or Tuesday":"Choose all weekdays first"})]}),x?null:e.jsxs(f,{md:3,children:[e.jsx(h,{children:"Virtual PRT"}),e.jsxs("select",{className:"form-select",value:T,onChange:s=>{G(s.target.value),A("")},required:!0,children:[e.jsx("option",{value:"",children:"Select PRT"}),q.map(s=>e.jsxs("option",{value:s.id,children:[s.firstName," ",s.lastName,L(s).length?` (${L(s).join(", ")})`:""]},s.id))]})]}),e.jsxs(f,{md:3,children:[e.jsx(h,{children:"Category"}),e.jsxs("select",{className:"form-select",value:_,onChange:s=>J(s.target.value),children:[e.jsx("option",{value:"",children:"Any / not set"}),(M?.diseaseTypes||[]).map(s=>e.jsx("option",{value:s,children:s},s))]})]}),e.jsxs(f,{md:3,children:[e.jsx(h,{children:"Language"}),e.jsxs("select",{className:"form-select",value:v,onChange:s=>A(s.target.value),required:!0,disabled:!b.length,children:[e.jsx("option",{value:"",children:b.length?"Select language":x?"Add a language on your profile":B?"This Virtual PRT has no language":"Select a Virtual PRT first"}),b.map(s=>e.jsx("option",{value:s,children:s},s))]})]}),d&&y&&y.original!=="proposed"?e.jsx(f,{md:3,children:e.jsx(Pe,{cohortId:l,value:y,onChange:X})}):null,e.jsxs(f,{xs:12,children:[e.jsxs("div",{className:"d-flex justify-content-between align-items-center mb-2",children:[e.jsx(h,{className:"mb-0",children:"Patients"}),e.jsxs(I,{type:"button",variant:"light",onClick:()=>P(s=>[...s,Fe()]),children:[e.jsx(he,{className:"me-1"})," Add patient"]})]}),m.length?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"form-text mb-2",children:"Fill one row per patient. Name, a 10 digit mobile and Offline completed (sessions already done offline, 0 if none) are required. Start date is optional. Empty rows are skipped. Save cohort creates them."}),e.jsx("div",{className:"table-responsive border rounded",children:e.jsxs("table",{className:"table table-sm align-middle mb-0",children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx("th",{children:"Name"}),e.jsx("th",{children:"Mobile"}),e.jsx("th",{style:{width:110},children:"Age"}),e.jsx("th",{style:{width:160},children:"Gender"}),e.jsx("th",{style:{width:140},children:"Offline completed"}),e.jsx("th",{style:{width:170},children:"Start date"}),e.jsx("th",{style:{width:120},children:"Sessions paid"}),e.jsx("th",{style:{width:150},children:"Patient status"}),e.jsx("th",{style:{width:48}})]})}),e.jsx("tbody",{children:m.map(s=>{const n=Number(s.sessionsPaid===""||s.sessionsPaid==null?30:s.sessionsPaid),o=Number(s.sessionsCompleted??0),u=s.existing&&o>=n;return e.jsxs("tr",{className:u?"table-danger":"",children:[e.jsx("td",{children:e.jsx(j,{value:s.name,onChange:a=>g(s.key,{name:a.target.value})})}),e.jsx("td",{children:e.jsx(j,{inputMode:"numeric",maxLength:10,value:s.mobile,onChange:a=>g(s.key,{mobile:Re(a.target.value)})})}),e.jsx("td",{children:e.jsx(j,{type:"number",min:0,value:s.age,onChange:a=>g(s.key,{age:a.target.value})})}),e.jsx("td",{children:e.jsx(ye,{value:s.gender,onChange:a=>g(s.key,{gender:a}),options:M?.genders,placeholder:"Select"})}),e.jsx("td",{children:e.jsx(j,{type:"number",min:0,value:s.offlineSessions,placeholder:"0",onChange:a=>g(s.key,{offlineSessions:a.target.value})})}),e.jsx("td",{children:e.jsx(j,{type:"date",value:s.offlineStartDate,onChange:a=>g(s.key,{offlineStartDate:a.target.value})})}),e.jsx("td",{children:e.jsx(j,{type:"number",min:0,value:s.sessionsPaid,placeholder:"30",onChange:a=>g(s.key,{sessionsPaid:a.target.value})})}),e.jsx("td",{children:e.jsxs("select",{className:"form-select form-select-sm",value:s.patientStatus||"active",onChange:a=>g(s.key,{patientStatus:a.target.value}),children:[e.jsx("option",{value:"active",children:"Active"}),e.jsx("option",{value:"deactivated",children:"Deactivated"})]})}),e.jsx("td",{children:e.jsx(I,{type:"button",variant:"outline-danger",size:"sm",className:"btn-icon",onClick:()=>P(a=>a.filter(N=>N.key!==s.key)),children:e.jsx(xe,{className:"fs-lg"})})})]},s.key)})})]})})]}):null]})]}),e.jsxs("div",{className:"d-flex gap-2 mt-4",children:[e.jsx(I,{type:"submit",disabled:$,children:$?"Saving...":"Save cohort"}),e.jsx(pe,{className:"btn btn-light",to:"/cohorts",children:"Cancel"})]})]})]})}),e.jsx(Ne,{warnings:re,busy:$,onCancel:()=>W([]),onConfirm:()=>{W([]),w(null,!0)}})]})}export{Qe as default};