2 lines
16 KiB
JavaScript
2 lines
16 KiB
JavaScript
import{r as a,j as e,L as T,q as O,s as Z,a4 as p,z as n}from"./index-9FS9bw8D.js";import{F as B}from"./MagnifyingGlassIcon-DIIDUrHz.js";import{F as J,a as W}from"./UsersIcon-DAyxbQQr.js";import{F as G}from"./ExclamationTriangleIcon-BZPGBcaI.js";function H({title:g,titleId:f,...j},b){return a.createElement("svg",Object.assign({xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",strokeWidth:1.5,stroke:"currentColor","aria-hidden":"true","data-slot":"icon",ref:b,"aria-labelledby":f},j),g?a.createElement("title",{id:f},g):null,a.createElement("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M18 7.5v3m0 0v3m0-3h3m-3 0h-3m-2.25-4.125a3.375 3.375 0 1 1-6.75 0 3.375 3.375 0 0 1 6.75 0ZM3 19.235v-.11a6.375 6.375 0 0 1 12.75 0v.109A12.318 12.318 0 0 1 9.374 21c-2.331 0-4.512-.645-6.374-1.766Z"}))}const K=a.forwardRef(H),_=()=>{const[g,f]=a.useState([]),[j,b]=a.useState(!0),[y,S]=a.useState(""),[N,R]=a.useState("all"),[d,D]=a.useState(null),[x,A]=a.useState(1),[r,c]=a.useState(null),[E,w]=a.useState(!1),[M,v]=a.useState(!1),[L,C]=a.useState(!1),[I,k]=a.useState(!1),[i,u]=a.useState({firstName:"",lastName:"",email:"",role:"user"}),[t,o]=a.useState({firstName:"",lastName:"",email:"",role:"",newPassword:"",confirmPassword:""}),m=async(s=1)=>{try{b(!0);const l={page:s,limit:20,...y&&{search:y},...N!=="all"&&{role:N}},h=await p.getUsers(l);f(h.data.data.users),D(h.data.data.pagination),A(s)}catch(l){console.error("Failed to fetch users:",l),n.error("Failed to load users")}finally{b(!1)}};a.useEffect(()=>{m(1)},[y,N]);const P=async s=>{try{await p.updateUserRole(r.id,s),n.success(`User role updated to ${s}`),w(!1),c(null),m(x)}catch(l){console.error("Failed to update role:",l),n.error("Failed to update user role")}},q=async()=>{try{await p.deleteUser(r.id),n.success("User deleted successfully"),v(!1),c(null),m(x)}catch(s){console.error("Failed to delete user:",s),n.error("Failed to delete user")}},$=async s=>{var l,h;s.preventDefault();try{await p.inviteUser(i),n.success("Invitation sent"),C(!1),u({firstName:"",lastName:"",email:"",role:"user"})}catch(F){console.error("Failed to send invitation:",F),n.error(((h=(l=F.response)==null?void 0:l.data)==null?void 0:h.message)||"Failed to send invitation")}},z=async s=>{if(s.preventDefault(),t.newPassword||t.confirmPassword){if(t.newPassword!==t.confirmPassword){n.error("Passwords do not match");return}if(t.newPassword.length<6){n.error("Password must be at least 6 characters long");return}}try{const l={firstName:t.firstName,lastName:t.lastName,email:t.email,role:t.role,...t.newPassword&&{password:t.newPassword}};await p.updateUser(r.id,l),n.success("User updated successfully"),k(!1),c(null),o({firstName:"",lastName:"",email:"",role:"",newPassword:"",confirmPassword:""}),m(x)}catch(l){console.error("Failed to update user:",l),n.error("Failed to update user")}},U=s=>new Date(s).toLocaleDateString("en-US",{year:"numeric",month:"short",day:"numeric"});return e.jsxs("div",{className:"p-6",children:[e.jsxs("div",{className:"mb-6",children:[e.jsxs("div",{className:"flex justify-between items-center mb-4",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-gray-900",children:"Manage Users"}),e.jsx("p",{className:"text-gray-600",children:"Add, edit, and manage user accounts"})]}),e.jsxs("button",{onClick:()=>C(!0),className:"btn-primary flex items-center",children:[e.jsx(K,{className:"h-5 w-5 mr-2"}),"Invite User"]})]}),e.jsxs("div",{className:"flex flex-col sm:flex-row gap-4 mb-6",children:[e.jsxs("div",{className:"flex-1 relative",children:[e.jsx(B,{className:"h-5 w-5 absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400"}),e.jsx("input",{type:"text",placeholder:"Search users...",value:y,onChange:s=>S(s.target.value),className:"pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 w-full"})]}),e.jsxs("select",{value:N,onChange:s=>R(s.target.value),className:"px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"all",children:"All Roles"}),e.jsx("option",{value:"admin",children:"Admins"}),e.jsx("option",{value:"user",children:"Users"})]})]})]}),j?e.jsx("div",{className:"flex justify-center items-center h-64",children:e.jsx(T,{size:"lg"})}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"card overflow-hidden",children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"min-w-full divide-y divide-gray-200",children:[e.jsx("thead",{className:"bg-gray-50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"User"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Role"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Properties"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Applications"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Joined"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Actions"})]})}),e.jsx("tbody",{className:"bg-white divide-y divide-gray-200",children:g.map(s=>e.jsxs("tr",{className:"hover:bg-gray-50",children:[e.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:e.jsxs("div",{className:"flex items-center",children:[e.jsx("div",{className:"h-10 w-10 rounded-full bg-blue-100 flex items-center justify-center",children:e.jsx(J,{className:"h-6 w-6 text-blue-600"})}),e.jsxs("div",{className:"ml-4",children:[e.jsxs("div",{className:"text-sm font-medium text-gray-900",children:[s.firstName," ",s.lastName]}),e.jsx("div",{className:"text-sm text-gray-500",children:s.email})]})]})}),e.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:e.jsx("span",{className:`px-2 py-1 text-xs font-medium rounded-full ${s.role==="admin"?"bg-purple-100 text-purple-800":"bg-gray-100 text-gray-800"}`,children:s.role})}),e.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm text-gray-900",children:s.propertyCount}),e.jsxs("td",{className:"px-6 py-4 whitespace-nowrap",children:[e.jsx("div",{className:"text-sm text-gray-900",children:s.applicationCount}),s.lastApplication&&e.jsxs("div",{className:"text-xs text-gray-500",children:["Last: ",U(s.lastApplication)]})]}),e.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm text-gray-500",children:U(s.createdAt)}),e.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm font-medium",children:e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx("button",{onClick:()=>{c(s),o({firstName:s.firstName,lastName:s.lastName,email:s.email,role:s.role,newPassword:"",confirmPassword:""}),k(!0)},className:"text-green-600 hover:text-green-900",title:"Edit User",children:e.jsx(O,{className:"h-4 w-4"})}),e.jsx("button",{onClick:()=>{c(s),w(!0)},className:"text-indigo-600 hover:text-indigo-900",title:"Change Role",children:e.jsx(W,{className:"h-4 w-4"})}),e.jsx("button",{onClick:()=>{c(s),v(!0)},className:"text-red-600 hover:text-red-900",title:"Delete User",children:e.jsx(Z,{className:"h-4 w-4"})})]})})]},s.id))})]})})}),d&&d.totalPages>1&&e.jsxs("div",{className:"flex items-center justify-between mt-6",children:[e.jsxs("div",{className:"text-sm text-gray-700",children:["Showing page ",d.currentPage," of ",d.totalPages,"(",d.totalUsers," total users)"]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx("button",{onClick:()=>m(x-1),disabled:!d.hasPrev,className:"px-3 py-2 text-sm bg-white border border-gray-300 rounded-md disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50",children:"Previous"}),e.jsx("button",{onClick:()=>m(x+1),disabled:!d.hasNext,className:"px-3 py-2 text-sm bg-white border border-gray-300 rounded-md disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50",children:"Next"})]})]})]}),E&&r&&e.jsx("div",{className:"fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full z-50",children:e.jsx("div",{className:"relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white",children:e.jsxs("div",{className:"mt-3",children:[e.jsxs("h3",{className:"text-lg font-medium text-gray-900 mb-4",children:["Change Role for ",r.firstName," ",r.lastName]}),e.jsxs("p",{className:"text-sm text-gray-600 mb-6",children:["Current role: ",e.jsx("span",{className:"font-medium",children:r.role})]}),e.jsxs("div",{className:"flex justify-end space-x-3",children:[e.jsx("button",{onClick:()=>w(!1),className:"px-4 py-2 text-sm text-gray-700 bg-gray-200 rounded hover:bg-gray-300",children:"Cancel"}),r.role!=="user"&&e.jsx("button",{onClick:()=>P("user"),className:"px-4 py-2 text-sm text-white bg-blue-600 rounded hover:bg-blue-700",children:"Make User"}),r.role!=="admin"&&e.jsx("button",{onClick:()=>P("admin"),className:"px-4 py-2 text-sm text-white bg-purple-600 rounded hover:bg-purple-700",children:"Make Admin"})]})]})})}),M&&r&&e.jsx("div",{className:"fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full z-50",children:e.jsx("div",{className:"relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white",children:e.jsxs("div",{className:"mt-3",children:[e.jsxs("div",{className:"flex items-center mb-4",children:[e.jsx(G,{className:"h-6 w-6 text-red-600 mr-2"}),e.jsx("h3",{className:"text-lg font-medium text-gray-900",children:"Delete User"})]}),e.jsxs("p",{className:"text-sm text-gray-600 mb-6",children:["Are you sure you want to delete ",r.firstName," ",r.lastName,"? This action cannot be undone and will permanently remove all their data."]}),e.jsxs("div",{className:"flex justify-end space-x-3",children:[e.jsx("button",{onClick:()=>v(!1),className:"px-4 py-2 text-sm text-gray-700 bg-gray-200 rounded hover:bg-gray-300",children:"Cancel"}),e.jsx("button",{onClick:q,className:"px-4 py-2 text-sm text-white bg-red-600 rounded hover:bg-red-700",children:"Delete User"})]})]})})}),I&&r&&e.jsx("div",{className:"fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full z-50",children:e.jsx("div",{className:"relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white",children:e.jsxs("div",{className:"mt-3",children:[e.jsxs("h3",{className:"text-lg font-medium text-gray-900 mb-4",children:["Edit User: ",r.firstName," ",r.lastName]}),e.jsxs("form",{onSubmit:z,children:[e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"First Name"}),e.jsx("input",{type:"text",value:t.firstName,onChange:s=>o({...t,firstName:s.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",required:!0})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Last Name"}),e.jsx("input",{type:"text",value:t.lastName,onChange:s=>o({...t,lastName:s.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",required:!0})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Email"}),e.jsx("input",{type:"email",value:t.email,onChange:s=>o({...t,email:s.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",required:!0})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Role"}),e.jsxs("select",{value:t.role,onChange:s=>o({...t,role:s.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",required:!0,children:[e.jsx("option",{value:"user",children:"User"}),e.jsx("option",{value:"admin",children:"Admin"})]})]}),e.jsxs("div",{className:"border-t pt-4",children:[e.jsx("h4",{className:"text-sm font-medium text-gray-900 mb-3",children:"Password Reset (Optional)"}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"New Password"}),e.jsx("input",{type:"password",value:t.newPassword,onChange:s=>o({...t,newPassword:s.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"Leave blank to keep current password"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Confirm New Password"}),e.jsx("input",{type:"password",value:t.confirmPassword,onChange:s=>o({...t,confirmPassword:s.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",placeholder:"Confirm new password"})]})]})]})]}),e.jsxs("div",{className:"flex justify-end space-x-3 mt-6",children:[e.jsx("button",{type:"button",onClick:()=>{k(!1),o({firstName:"",lastName:"",email:"",role:"",newPassword:"",confirmPassword:""})},className:"px-4 py-2 text-sm text-gray-700 bg-gray-200 rounded hover:bg-gray-300",children:"Cancel"}),e.jsx("button",{type:"submit",className:"px-4 py-2 text-sm text-white bg-green-600 rounded hover:bg-green-700",children:"Update User"})]})]})]})})}),L&&e.jsx("div",{className:"fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full z-50",children:e.jsx("div",{className:"relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white",children:e.jsxs("div",{className:"mt-3",children:[e.jsx("h3",{className:"text-lg font-medium text-gray-900 mb-4",children:"Invite User"}),e.jsxs("form",{onSubmit:$,className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"First Name"}),e.jsx("input",{type:"text",value:i.firstName,onChange:s=>u({...i,firstName:s.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",required:!0})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Last Name"}),e.jsx("input",{type:"text",value:i.lastName,onChange:s=>u({...i,lastName:s.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",required:!0})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Email"}),e.jsx("input",{type:"email",value:i.email,onChange:s=>u({...i,email:s.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",required:!0})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Role"}),e.jsxs("select",{value:i.role,onChange:s=>u({...i,role:s.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"user",children:"User"}),e.jsx("option",{value:"admin",children:"Admin"})]})]}),e.jsx("p",{className:"text-sm text-gray-600",children:"The user will receive an email with a secure invitation link."}),e.jsxs("div",{className:"flex justify-end space-x-3",children:[e.jsx("button",{type:"button",onClick:()=>C(!1),className:"px-4 py-2 text-sm text-gray-700 bg-gray-200 rounded hover:bg-gray-300",children:"Cancel"}),e.jsx("button",{type:"submit",className:"px-4 py-2 text-sm text-white bg-green-600 rounded hover:bg-green-700",children:"Send Invite"})]})]})]})})})]})};export{_ as default};
|