2 lines
10 KiB
JavaScript
2 lines
10 KiB
JavaScript
import{t as re,D as ie,v as le,r as o,d as f,j as e,l as i,C as oe,R as C,B as E,m as H}from"./index-B92aEPQH.js";import{O as L}from"./OptionsSelect-8ijTXas2.js";import{P as de}from"./PageBreadcrumb-Dsn9CkpH.js";import{S as ce}from"./SlotSelect-CKKlKzAo.js";import{u as me,W as ue}from"./useSettings-B5rMnTFQ.js";import{m as K,i as he}from"./mobile-CfiPsqNQ.js";import{F as d}from"./FormLabel-DFBiGE9d.js";import{F as p}from"./FormControl-BhzYseFY.js";import{C as ge,a as pe}from"./Card-DPmHd5BI.js";import{F as xe}from"./FormCheck-QYGhbIhB.js";import"./PageMetaData-DFmRpQLs.js";import"./FormContext-IkkKm1bh.js";const D={name:"",mobile:"",age:"",gender:"",sessionLanguage:"",cohort:"",slot:"",preferredWeekday:"",diseaseType:"",paymentStatus:"Unpaid",whatsappOptOut:!1};function _(t){return t?String(t).slice(0,10):""}function je(t){const h=[_(t.startDate),t.slot,t.language,t.diseaseType].filter(Boolean).join(" · ");return h?`${t.name} (${h})`:t.name}function x(t){return t==null||t===""?"—":t}function V(t){return{...D,name:t.name||"",mobile:t.mobile||"",age:t.age??"",gender:t.gender||"",sessionLanguage:t.sessionLanguage||"",slot:t.slot||""}}function Ie(){const{id:t}=re(),[O]=ie(),h=(O.get("patientId")||"").trim(),l=!!t,z=le(),[Q,M]=o.useState(D),[X,P]=o.useState(D),[W,T]=o.useState(""),[n,S]=o.useState(null),[B,v]=o.useState(""),[$,y]=o.useState(!1),[w,q]=o.useState([]),[A,k]=o.useState(null),[g,Z]=o.useState(!1),[R,m]=o.useState(""),[N,G]=o.useState(!1),{settings:b}=me(),ee=b?.cohortStartWeekdays||[1,2],a=n?X:Q,c=(s,r)=>{(n?P:M)(I=>({...I,[s]:r}))},F=!a.cohort,U=String(n?.importedLanguage||n?.sessionLanguage||"").trim(),se=l||!n||!U,ae=l||!String(n?.slot||"").trim(),te=l||!n;o.useEffect(()=>{if(l||!h)return;let s=!0;return T(h),m(""),v(""),y(!0),f(`/api/onboarding/patient-lookup?patientId=${encodeURIComponent(h)}`).then(r=>{s&&(k(null),r.found&&r.patient?(S(r.patient),P(V(r.patient))):(S(null),v("No patient found for this ID.")))}).catch(r=>{s&&m(r.message)}).finally(()=>{s&&y(!1)}),()=>{s=!1}},[l,h]),o.useEffect(()=>{l&&f(`/api/onboarding/${t}`).then(s=>{Z(s.reviewStatus==="confirmed"),M({name:s.name||"",mobile:s.mobile||"",age:s.age??"",gender:s.gender||"",sessionLanguage:s.sessionLanguage||"",cohort:s.cohort?.id||"",slot:s.slot||"",preferredWeekday:s.preferredWeekday??"",diseaseType:s.diseaseType||"",paymentStatus:s.paymentStatus||"Unpaid",whatsappOptOut:!!s.whatsappOptOut})}).catch(s=>m(s.message))},[t,l]),o.useEffect(()=>{const s=new URLSearchParams;a.sessionLanguage&&s.set("language",a.sessionLanguage),a.cohort&&s.set("include",a.cohort);let r=!0;return f(`/api/onboarding/available-cohorts?${s}`).then(j=>{r&&q(j.items||[])}).catch(()=>{r&&q([])}),()=>{r=!1}},[a.sessionLanguage,a.cohort]);const u=o.useMemo(()=>w.find(s=>s.id===a.cohort)||null,[w,a.cohort]),ne=async()=>{const s=W.trim();if(!s){m("Enter a patient ID.");return}m(""),v(""),y(!0);try{const r=await f(`/api/onboarding/patient-lookup?patientId=${encodeURIComponent(s)}`);k(null),r.found&&r.patient?(S(r.patient),P(V(r.patient))):(S(null),v("No patient found for this ID."))}catch(r){m(r.message)}finally{y(!1)}},J=async s=>{if(s.preventDefault(),m(""),v(""),!g&&F&&(!a.sessionLanguage||!a.slot||a.preferredWeekday===""||!a.diseaseType)){m("Without a cohort, language, slot, preferred weekday, and category are required.");return}if(!String(n?.mobile||"").trim()&&!he(a.mobile)){m("Mobile must be 10 digits.");return}G(!0);const r=new FormData;Object.entries(a).forEach(([j,I])=>r.append(j,I??"")),!l&&n?.sourcePatientId&&r.append("sourcePatientId",n.sourcePatientId),A&&r.append("screenshot",A);try{l?await f(`/api/onboarding/${t}`,{method:"PATCH",body:r}):await f("/api/onboarding",{method:"POST",body:r}),z("/onboarding")}catch(j){m(j.message)}finally{G(!1)}},Y=e.jsxs(e.Fragment,{children:[se?e.jsxs(i,{md:6,children:[e.jsxs(d,{children:["Language ",F?e.jsx("span",{className:"text-danger",children:"*"}):null]}),e.jsx(L,{value:a.sessionLanguage,onChange:s=>c("sessionLanguage",s),options:b?.languages,disabled:g}),n&&!U?e.jsx("div",{className:"form-text",children:"This patient has no language on the master record. Select one to continue."}):null]}):null,e.jsxs(i,{md:4,children:[e.jsx(d,{children:"Payment status"}),e.jsx(L,{value:a.paymentStatus,onChange:s=>c("paymentStatus",s),options:b?.paymentStatuses,required:!0})]}),F?e.jsxs(e.Fragment,{children:[ae?e.jsxs(i,{md:4,children:[e.jsxs(d,{children:["Slot ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(ce,{value:a.slot,onChange:s=>c("slot",s),required:!0,disabled:g}),n?.importedSlot?e.jsxs("div",{className:"form-text",children:["Imported sessions use ",n.importedSlot,". Select the matching slot."]}):null]}):null,e.jsxs(i,{md:4,children:[e.jsxs(d,{children:["Preferred start weekday ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsxs("select",{className:"form-select",value:a.preferredWeekday,onChange:s=>c("preferredWeekday",s.target.value),required:!0,disabled:g,children:[e.jsx("option",{value:"",children:"Select weekday"}),ee.map(s=>e.jsx("option",{value:s,children:ue[s]},s))]})]}),e.jsxs(i,{md:4,children:[e.jsxs(d,{children:["Category ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(L,{value:a.diseaseType,onChange:s=>c("diseaseType",s),options:b?.diseaseTypes,disabled:g})]})]}):null,e.jsxs(i,{md:6,children:[e.jsx(d,{children:"Payment screenshot"}),e.jsx(p,{type:"file",accept:".jpg,.jpeg,.png,.webp,image/jpeg,image/jpg,image/png,image/webp",onChange:s=>k(s.target.files?.[0]||null)}),e.jsxs("div",{className:"form-text",children:[l?"Leave empty to keep the current screenshot. ":"","JPG, JPEG, PNG, or WebP. Max 5MB."]})]})]});return e.jsxs(oe,{fluid:!0,children:[e.jsx(de,{title:l?"Edit patient":"Add patient",subtitle:"Patients"}),e.jsx(ge,{children:e.jsxs(pe,{children:[g?e.jsx("div",{className:"alert alert-info",children:"This patient is confirmed. You can update contact and clinical details. Language, slot, weekday, category, and cohort stay as they are — use Move on the patient list to change those."}):null,R?e.jsx("div",{className:"alert alert-danger",children:R}):null,l?null:e.jsx("form",{onSubmit:s=>{s.preventDefault(),ne()},children:e.jsx(C,{className:"g-3",children:e.jsxs(i,{md:6,children:[e.jsx(d,{children:"Patient ID"}),e.jsxs("div",{className:"d-flex gap-2",children:[e.jsx(p,{value:W,onChange:s=>T(s.target.value),placeholder:"Enter patient ID"}),e.jsx(E,{type:"submit",variant:"primary",disabled:$,children:$?"Searching...":"Search"})]})]})})}),!l&&B?e.jsx("div",{className:"alert alert-danger mt-3 mb-0",children:B}):null,!l&&n?e.jsxs("form",{onSubmit:J,className:"mt-4",children:[e.jsxs("div",{className:"border rounded p-3 bg-light mb-3",children:[e.jsx("div",{className:"fw-semibold mb-2",children:"Patient found"}),e.jsxs(C,{className:"g-2",children:[e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Patient ID"}),e.jsx("div",{children:x(n.sourcePatientId)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Name"}),e.jsx("div",{children:x(n.name)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Mobile"}),e.jsx("div",{children:x(n.mobile)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Age"}),e.jsx("div",{children:x(n.age)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Gender"}),e.jsx("div",{children:x(n.gender)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Language"}),e.jsx("div",{children:x(n.importedLanguage||n.sessionLanguage)})]}),e.jsxs(i,{md:4,children:[e.jsx("div",{className:"text-muted small",children:"Slot"}),e.jsx("div",{children:x(n.importedSlot||n.slot)})]})]})]}),e.jsxs(C,{className:"g-3",children:[String(n.name||"").trim()?null:e.jsxs(i,{md:6,children:[e.jsxs(d,{children:["Name ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(p,{value:a.name,onChange:s=>c("name",s.target.value),required:!0})]}),String(n.mobile||"").trim()?null:e.jsxs(i,{md:6,children:[e.jsxs(d,{children:["Mobile ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(p,{inputMode:"numeric",maxLength:10,value:a.mobile,onChange:s=>c("mobile",K(s.target.value)),required:!0})]}),Y]}),e.jsxs("div",{className:"d-flex gap-2 mt-4",children:[e.jsx(E,{type:"submit",disabled:N,children:N?"Saving...":"Add patient"}),e.jsx(H,{className:"btn btn-light",to:"/onboarding",children:"Cancel"})]})]}):null,te?e.jsxs(e.Fragment,{children:[l?null:e.jsxs("div",{className:"d-flex align-items-center my-4",children:[e.jsx("hr",{className:"flex-grow-1 m-0"}),e.jsx("span",{className:"px-3 text-muted fw-semibold",children:"OR"}),e.jsx("hr",{className:"flex-grow-1 m-0"})]}),e.jsxs("form",{onSubmit:J,children:[e.jsxs(C,{className:"g-3",children:[e.jsxs(i,{md:6,children:[e.jsxs(d,{children:["Name ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(p,{value:a.name,onChange:s=>c("name",s.target.value),required:!0})]}),e.jsxs(i,{md:6,children:[e.jsxs(d,{children:["Mobile ",e.jsx("span",{className:"text-danger",children:"*"})]}),e.jsx(p,{inputMode:"numeric",maxLength:10,value:a.mobile,onChange:s=>c("mobile",K(s.target.value)),required:!0})]}),e.jsxs(i,{md:3,children:[e.jsx(d,{children:"Age"}),e.jsx(p,{type:"number",min:0,value:a.age,onChange:s=>c("age",s.target.value)})]}),e.jsxs(i,{md:3,children:[e.jsx(d,{children:"Gender"}),e.jsx(L,{value:a.gender,onChange:s=>c("gender",s),options:b?.genders})]}),e.jsxs(i,{md:8,children:[e.jsx(d,{children:"Future cohort (optional)"}),e.jsxs("select",{className:"form-select",value:a.cohort,onChange:s=>c("cohort",s.target.value),disabled:g,children:[e.jsx("option",{value:"",children:"Leave unassigned for matching"}),w.map(s=>e.jsx("option",{value:s.id,children:je(s)},s.id))]}),u?e.jsxs("div",{className:"form-text",children:["Starts ",_(u.startDate)," · ",u.slot,u.language?` · ${u.language}`:"",u.virtualPrt?` · ${u.virtualPrt.firstName} ${u.virtualPrt.lastName}`:""]}):e.jsx("div",{className:"form-text",children:"Choose a future cohort, or leave empty and set language, slot, weekday, and category for the matching pool."})]}),e.jsx(i,{xs:12,children:e.jsx(xe,{checked:!!a.whatsappOptOut,onChange:s=>c("whatsappOptOut",s.target.checked),label:"Do not add this patient to the cohort WhatsApp group"})}),Y]}),e.jsxs("div",{className:"d-flex gap-2 mt-4",children:[e.jsx(E,{type:"submit",disabled:N,children:N?"Saving...":l?"Save changes":"Add patient"}),e.jsx(H,{className:"btn btn-light",to:"/onboarding",children:"Cancel"})]})]})]}):null]})})]})}export{Ie as default};
|