2 lines
3.8 KiB
JavaScript
2 lines
3.8 KiB
JavaScript
import{b as B,r as c,j as s,C as T,R as E,x as M,y as z,z as F,A as y,l as I,B as O,d as u}from"./index-DGCPJq4z.js";import{P as _}from"./PageBreadcrumb-zFeQijFR.js";import{C as f,a as b}from"./Card-D1j94NWu.js";import{F as H}from"./FormCheck-tmQhdIFS.js";import"./PageMetaData-B-bWCH8Q.js";import"./FormContext-DDFZ16IF.js";const U={super_admin:y,admin:F,educator:z,physical_prt:M};function J(){const{can:g,permissions:v}=B(),[N,S]=c.useState([]),[o,C]=c.useState({modules:[],actions:[]}),[i,d]=c.useState(null),[h,x]=c.useState(!1),[p,m]=c.useState(""),r=g("roles","edit")&&v.isSuperAdmin,j=async()=>{const[e,t]=await Promise.all([u("/api/roles"),u("/api/roles/catalog")]);S(e.items||[]),C(t),d(n=>(e.items||[]).find(l=>l.id===n?.id)||e.items?.[0]||null)};c.useEffect(()=>{j().catch(()=>{})},[]);const k=(e,t)=>!!i?.permissions?.find(a=>a.module===e)?.actions?.includes(t),A=e=>i?.permissions?.find(t=>t.module===e)?.scope||"all",w=(e,t)=>{if(!r||!i)return;const n=[...i.permissions||[]];let a=n.find(l=>l.module===e);a||(a={module:e,actions:[],scope:"all"},n.push(a)),a.actions.includes(t)?a.actions=a.actions.filter(l=>l!==t):a.actions=[...a.actions,t],d({...i,permissions:n})},P=(e,t)=>{if(!r||!i)return;const n=[...i.permissions||[]];let a=n.find(l=>l.module===e);a?a.scope=t:(a={module:e,actions:["view"],scope:t},n.push(a)),d({...i,permissions:n})},R=async()=>{if(i){x(!0),m("");try{await u(`/api/roles/${i.id}`,{method:"PATCH",body:{permissions:i.permissions}}),m("Permissions saved."),await j()}catch(e){m(e.message)}finally{x(!1)}}};return s.jsxs(T,{fluid:!0,children:[s.jsx(_,{title:"Roles",subtitle:"Admin"}),s.jsx(E,{className:"g-3 mb-3",children:N.map(e=>{const t=U[e.systemKey]||y,n=i?.id===e.id;return s.jsx(I,{md:6,lg:3,children:s.jsx(f,{className:n?"border-primary":"",role:"button",onClick:()=>d(e),children:s.jsx(b,{children:s.jsxs("div",{className:"d-flex gap-3",children:[s.jsx("div",{className:"avatar-lg rounded bg-primary-subtle d-flex align-items-center justify-content-center",children:s.jsx(t,{className:"fs-24 text-primary"})}),s.jsxs("div",{children:[s.jsx("h5",{className:"mb-1",children:e.name}),s.jsx("p",{className:"text-muted mb-1 fs-sm",children:e.description}),s.jsxs("span",{className:"text-muted fs-xs",children:[e.userCount," users"]})]})]})})})},e.id)})}),i?s.jsx(f,{children:s.jsxs(b,{children:[s.jsxs("div",{className:"d-flex justify-content-between align-items-center mb-3",children:[s.jsxs("div",{children:[s.jsxs("h5",{className:"mb-0",children:[i.name," permissions"]}),s.jsx("p",{className:"text-muted mb-0 fs-sm",children:"Module access and the actions this role can perform."})]}),r?s.jsx(O,{onClick:R,disabled:h,children:h?"Saving...":"Save changes"}):s.jsx("span",{className:"text-muted",children:"View only"})]}),p?s.jsx("div",{className:"alert alert-info",children:p}):null,s.jsx("div",{className:"table-responsive",children:s.jsxs("table",{className:"table table-custom align-middle",children:[s.jsx("thead",{children:s.jsxs("tr",{children:[s.jsx("th",{children:"Module"}),o.actions.map(e=>s.jsx("th",{className:"text-capitalize text-center",children:e},e)),s.jsx("th",{children:"Scope"})]})}),s.jsx("tbody",{children:o.modules.map(e=>s.jsxs("tr",{children:[s.jsx("td",{className:"fw-semibold",children:e.label}),o.actions.map(t=>s.jsx("td",{className:"text-center",children:t==="import"&&e.key!=="patients"?s.jsx("span",{className:"text-muted",children:"—"}):s.jsx(H,{className:"d-inline-flex justify-content-center",checked:k(e.key,t),disabled:!r,onChange:()=>w(e.key,t)})},t)),s.jsx("td",{children:s.jsxs("select",{className:"form-select form-select-sm",style:{maxWidth:140},value:A(e.key),disabled:!r,onChange:t=>P(e.key,t.target.value),children:[s.jsx("option",{value:"all",children:"All records"}),s.jsx("option",{value:"assigned",children:"Assigned only"})]})})]},e.key))})]})})]})}):null]})}export{J as default};
|