import{R as u,o as I,k as U,l as k,j as a,E as F,a9 as m}from"./app-BgLa5dit.js";import{C as p}from"./q-IBKcuF.chunk.js";import"./DUpYPoLs.chunk.js";import"./EZim8aq-.chunk.js";import"./BD9MrUJP.chunk.js";import"./s6cGcwlA.chunk.js";import"./Zwjmkasg.chunk.js";import"./4qGb1BIY.chunk.js";import"./DnXDEnAR.chunk.js";import"./DpUpv6rY.chunk.js";import"./CG7zbly5.chunk.js";import"./m4aqaK_l.chunk.js";import"./MEkA24R1.chunk.js";import"./C53kfXmd.chunk.js";function Q(r){var g,x,S;const[s,h]=u.useState(""),[f,c]=u.useState(!1),e=I(p.getUser(r.customerId,r.userId)),n=I(p.yearGroups(r.customerId)),N=U(),y=k();u.useEffect(()=>{var t,o,i,d;e&&((o=(t=e==null?void 0:e.data)==null?void 0:t.masterYearGroup)!=null&&o.id)&&h((d=(i=e==null?void 0:e.data)==null?void 0:i.masterYearGroup)==null?void 0:d.id.toString())},[e]);async function P(){var t,o,i,d,j,G,v,C;c(!0);try{const l=(t=n.data)==null?void 0:t.find(R=>R.id.toString()===(s==null?void 0:s.toString()));if(!l){c(!1);return}const Y={...e.data,masterYearGroup:l,masterRole:(i=(o=e.data)==null?void 0:o.userProfile)==null?void 0:i.role,customerId:r.customerId,firstName:((d=e.data)==null?void 0:d.name)||"",lastName:((j=e.data)==null?void 0:j.surname)||"",userId:(G=e.data)==null?void 0:G.id,email:((v=e.data)==null?void 0:v.email)||"",displayName:((C=e.data)==null?void 0:C.displayName)||""};await N(p.updateUser(r.customerId,Y,e.data.id)),y.success("Year group updated successfully.")}catch{y.error("Failed to update year group.")}finally{c(!1),r.closePopup()}}const b=e.hasCompleted&&n.hasCompleted;return a.jsx(F,{title:"Update year group",closePopup:r.closePopup,showLoadingSpinner:!b,children:n?a.jsxs(a.Fragment,{children:[a.jsxs("div",{className:"form-group",children:[a.jsx("label",{children:"Select year group:"}),a.jsxs("select",{id:"yearGroupSelect",name:"yearGroup",className:"form-control mb-2",value:s,onChange:t=>{h(t.target.value)},children:[a.jsx("option",{className:"text-secondary",value:"",disabled:!0,children:"Select a year group"}),(g=n.data)==null?void 0:g.map(t=>a.jsx("option",{value:t.id,children:t.name},t.id))]})]}),a.jsxs(m,{children:[a.jsx(m.Cancel,{onClick:r.closePopup}),a.jsx(m.Submit,{onClick:P,text:"Update",variant:"dark",showSpinner:f,disabled:s===((S=(x=e==null?void 0:e.data)==null?void 0:x.masterYearGroup)==null?void 0:S.id.toString())||f})]})]}):a.jsx("div",{children:"This customer doesn't have any year groups"})})}export{Q as ManageYearGroup};
//# sourceMappingURL=B7cojqDf.chunk.js.map
