|
| 1 | +import { Head, useForm, router, usePage } from '@inertiajs/react'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import AppLayout from '@/Layouts/AppLayout'; |
| 4 | +import { Button } from '@/Components/Common/Button'; |
| 5 | +import type { PageProps } from '@/types'; |
| 6 | + |
| 7 | +interface UserRow { |
| 8 | + id: number; |
| 9 | + name: string; |
| 10 | + email: string; |
| 11 | + is_active: boolean; |
| 12 | + roles: string[]; |
| 13 | + created_at: string; |
| 14 | +} |
| 15 | + |
| 16 | +interface Props extends PageProps { |
| 17 | + users: UserRow[]; |
| 18 | + roles: string[]; |
| 19 | +} |
| 20 | + |
| 21 | +const ROLE_COLORS: Record<string, string> = { |
| 22 | + 'super-admin': 'bg-purple-100 text-purple-700', |
| 23 | + admin: 'bg-indigo-100 text-indigo-700', |
| 24 | + manager: 'bg-blue-100 text-blue-700', |
| 25 | + staff: 'bg-slate-100 text-slate-600', |
| 26 | +}; |
| 27 | + |
| 28 | +export default function UsersIndex({ users, roles }: Props) { |
| 29 | + const { auth } = usePage<Props>().props; |
| 30 | + const [showInvite, setShowInvite] = useState(false); |
| 31 | + const { data, setData, post, processing, errors, reset } = useForm({ |
| 32 | + name: '', email: '', role: 'staff', |
| 33 | + }); |
| 34 | + |
| 35 | + function submitInvite(e: React.FormEvent) { |
| 36 | + e.preventDefault(); |
| 37 | + post('/settings/users/invite', { |
| 38 | + onSuccess: () => { setShowInvite(false); reset(); }, |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + function changeRole(userId: number, role: string) { |
| 43 | + router.patch(`/settings/users/${userId}/role`, { role }); |
| 44 | + } |
| 45 | + |
| 46 | + function toggleActive(userId: number) { |
| 47 | + router.patch(`/settings/users/${userId}/toggle-active`); |
| 48 | + } |
| 49 | + |
| 50 | + function removeUser(userId: number, name: string) { |
| 51 | + if (confirm(`Remove ${name} from the system?`)) { |
| 52 | + router.delete(`/settings/users/${userId}`); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return ( |
| 57 | + <AppLayout> |
| 58 | + <Head title="User Management" /> |
| 59 | + <div className="space-y-6"> |
| 60 | + <div className="flex items-center justify-between"> |
| 61 | + <h1 className="text-2xl font-semibold text-slate-900">Users</h1> |
| 62 | + <Button onClick={() => setShowInvite((v) => !v)}> |
| 63 | + {showInvite ? 'Cancel' : 'Invite User'} |
| 64 | + </Button> |
| 65 | + </div> |
| 66 | + |
| 67 | + {showInvite && ( |
| 68 | + <form onSubmit={submitInvite} className="rounded-lg border border-slate-200 bg-white p-5 shadow-sm space-y-4"> |
| 69 | + <h2 className="text-sm font-semibold text-slate-700">Invite New User</h2> |
| 70 | + <div className="grid grid-cols-3 gap-4"> |
| 71 | + <div> |
| 72 | + <label className="block text-xs font-medium text-slate-600 mb-1">Name</label> |
| 73 | + <input type="text" required value={data.name} onChange={(e) => setData('name', e.target.value)} |
| 74 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none" /> |
| 75 | + {errors.name && <p className="text-xs text-red-600 mt-1">{errors.name}</p>} |
| 76 | + </div> |
| 77 | + <div> |
| 78 | + <label className="block text-xs font-medium text-slate-600 mb-1">Email</label> |
| 79 | + <input type="email" required value={data.email} onChange={(e) => setData('email', e.target.value)} |
| 80 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none" /> |
| 81 | + {errors.email && <p className="text-xs text-red-600 mt-1">{errors.email}</p>} |
| 82 | + </div> |
| 83 | + <div> |
| 84 | + <label className="block text-xs font-medium text-slate-600 mb-1">Role</label> |
| 85 | + <select value={data.role} onChange={(e) => setData('role', e.target.value)} |
| 86 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none"> |
| 87 | + {roles.map((r) => <option key={r} value={r}>{r}</option>)} |
| 88 | + </select> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + <div className="flex justify-end gap-2"> |
| 92 | + <Button type="button" variant="secondary" onClick={() => setShowInvite(false)}>Cancel</Button> |
| 93 | + <Button type="submit" disabled={processing}>{processing ? 'Inviting…' : 'Send Invite'}</Button> |
| 94 | + </div> |
| 95 | + </form> |
| 96 | + )} |
| 97 | + |
| 98 | + <div className="rounded-lg border border-slate-200 bg-white shadow-sm overflow-hidden"> |
| 99 | + <table className="w-full text-sm"> |
| 100 | + <thead className="bg-slate-50 text-xs text-slate-500 uppercase border-b border-slate-200"> |
| 101 | + <tr> |
| 102 | + <th className="px-4 py-2 text-left font-medium">Name</th> |
| 103 | + <th className="px-4 py-2 text-left font-medium">Email</th> |
| 104 | + <th className="px-4 py-2 text-left font-medium">Role</th> |
| 105 | + <th className="px-4 py-2 text-left font-medium">Status</th> |
| 106 | + <th className="px-4 py-2 text-left font-medium">Joined</th> |
| 107 | + <th className="px-4 py-2 text-right font-medium">Actions</th> |
| 108 | + </tr> |
| 109 | + </thead> |
| 110 | + <tbody className="divide-y divide-slate-100"> |
| 111 | + {users.map((user) => ( |
| 112 | + <tr key={user.id} className={`hover:bg-slate-50 ${!user.is_active ? 'opacity-50' : ''}`}> |
| 113 | + <td className="px-4 py-3 font-medium text-slate-900">{user.name}</td> |
| 114 | + <td className="px-4 py-3 text-slate-500">{user.email}</td> |
| 115 | + <td className="px-4 py-3"> |
| 116 | + {user.roles.map((r) => ( |
| 117 | + <span key={r} className={`inline-flex rounded-full px-2 py-0.5 text-xs font-medium ${ROLE_COLORS[r] ?? 'bg-slate-100 text-slate-600'}`}> |
| 118 | + {r} |
| 119 | + </span> |
| 120 | + ))} |
| 121 | + </td> |
| 122 | + <td className="px-4 py-3"> |
| 123 | + <span className={`inline-flex rounded-full px-2 py-0.5 text-xs font-medium ${user.is_active ? 'bg-green-100 text-green-700' : 'bg-slate-100 text-slate-500'}`}> |
| 124 | + {user.is_active ? 'Active' : 'Inactive'} |
| 125 | + </span> |
| 126 | + </td> |
| 127 | + <td className="px-4 py-3 text-slate-500">{user.created_at}</td> |
| 128 | + <td className="px-4 py-3 text-right"> |
| 129 | + {user.id !== auth.user?.id && ( |
| 130 | + <div className="flex justify-end gap-2"> |
| 131 | + {!user.roles.includes('super-admin') && ( |
| 132 | + <select |
| 133 | + value={user.roles[0] ?? 'staff'} |
| 134 | + onChange={(e) => changeRole(user.id, e.target.value)} |
| 135 | + className="rounded-md border border-slate-300 px-2 py-1 text-xs focus:border-indigo-500 focus:outline-none" |
| 136 | + > |
| 137 | + {roles.map((r) => <option key={r} value={r}>{r}</option>)} |
| 138 | + </select> |
| 139 | + )} |
| 140 | + <button |
| 141 | + onClick={() => toggleActive(user.id)} |
| 142 | + className="rounded-md border border-slate-300 px-2 py-1 text-xs text-slate-600 hover:bg-slate-100" |
| 143 | + > |
| 144 | + {user.is_active ? 'Deactivate' : 'Reactivate'} |
| 145 | + </button> |
| 146 | + <button |
| 147 | + onClick={() => removeUser(user.id, user.name)} |
| 148 | + className="rounded-md border border-red-200 px-2 py-1 text-xs text-red-600 hover:bg-red-50" |
| 149 | + > |
| 150 | + Remove |
| 151 | + </button> |
| 152 | + </div> |
| 153 | + )} |
| 154 | + </td> |
| 155 | + </tr> |
| 156 | + ))} |
| 157 | + </tbody> |
| 158 | + </table> |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + </AppLayout> |
| 162 | + ); |
| 163 | +} |
0 commit comments