feat(phase-9): implement MVP polishing and closure
T-032: User management admin panel - backend users router with CRUD, frontend UsersPage with modals T-033: Audit log viewer - backend audit router with filters/pagination, frontend AuditLogPage T-034: Global error handling - ErrorBoundary, LoadingSpinner, ErrorMessage, Toast components T-035: Backend tests - pytest setup with SQLite, tests for health/auth/techniques/tests T-036: Documentation - Updated README with testing section, created docs/API.md
This commit is contained in:
293
frontend/src/pages/AuditLogPage.tsx
Normal file
293
frontend/src/pages/AuditLogPage.tsx
Normal file
@@ -0,0 +1,293 @@
|
||||
import { useState } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import {
|
||||
Loader2,
|
||||
AlertCircle,
|
||||
FileText,
|
||||
Filter,
|
||||
ChevronLeft,
|
||||
ChevronRight,
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import {
|
||||
getAuditLogs,
|
||||
getAuditActions,
|
||||
getAuditEntityTypes,
|
||||
type AuditLogFilters,
|
||||
} from "../api/audit";
|
||||
|
||||
const PAGE_SIZE = 25;
|
||||
|
||||
export default function AuditLogPage() {
|
||||
const [filters, setFilters] = useState<AuditLogFilters>({
|
||||
offset: 0,
|
||||
limit: PAGE_SIZE,
|
||||
});
|
||||
|
||||
const {
|
||||
data: logsData,
|
||||
isLoading: logsLoading,
|
||||
error: logsError,
|
||||
} = useQuery({
|
||||
queryKey: ["audit-logs", filters],
|
||||
queryFn: () => getAuditLogs(filters),
|
||||
});
|
||||
|
||||
const { data: actions } = useQuery({
|
||||
queryKey: ["audit-actions"],
|
||||
queryFn: getAuditActions,
|
||||
});
|
||||
|
||||
const { data: entityTypes } = useQuery({
|
||||
queryKey: ["audit-entity-types"],
|
||||
queryFn: getAuditEntityTypes,
|
||||
});
|
||||
|
||||
const handleFilterChange = (key: keyof AuditLogFilters, value: string) => {
|
||||
setFilters((prev) => ({
|
||||
...prev,
|
||||
[key]: value || undefined,
|
||||
offset: 0, // Reset pagination on filter change
|
||||
}));
|
||||
};
|
||||
|
||||
const clearFilters = () => {
|
||||
setFilters({ offset: 0, limit: PAGE_SIZE });
|
||||
};
|
||||
|
||||
const goToPage = (newOffset: number) => {
|
||||
setFilters((prev) => ({ ...prev, offset: newOffset }));
|
||||
};
|
||||
|
||||
const hasActiveFilters = filters.action || filters.entity_type || filters.start_date || filters.end_date;
|
||||
|
||||
const formatDate = (dateStr: string) => {
|
||||
return new Date(dateStr).toLocaleString("en-US", {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
});
|
||||
};
|
||||
|
||||
const formatDetails = (details: Record<string, unknown> | null) => {
|
||||
if (!details) return null;
|
||||
return JSON.stringify(details, null, 2);
|
||||
};
|
||||
|
||||
const totalPages = logsData ? Math.ceil(logsData.total / PAGE_SIZE) : 0;
|
||||
const currentPage = Math.floor((filters.offset || 0) / PAGE_SIZE) + 1;
|
||||
|
||||
if (logsLoading) {
|
||||
return (
|
||||
<div className="flex h-64 items-center justify-center">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-cyan-400" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (logsError) {
|
||||
return (
|
||||
<div className="flex h-64 flex-col items-center justify-center gap-2">
|
||||
<AlertCircle className="h-10 w-10 text-red-400" />
|
||||
<p className="text-red-400">Failed to load audit logs</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold text-white">Audit Log</h1>
|
||||
<p className="mt-1 text-sm text-gray-400">
|
||||
System activity and change history
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Filters */}
|
||||
<div className="flex flex-wrap items-center gap-4 rounded-xl border border-gray-800 bg-gray-900 p-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<Filter className="h-4 w-4 text-gray-400" />
|
||||
<span className="text-sm font-medium text-gray-400">Filters:</span>
|
||||
</div>
|
||||
|
||||
{/* Action filter */}
|
||||
<select
|
||||
value={filters.action || ""}
|
||||
onChange={(e) => handleFilterChange("action", e.target.value)}
|
||||
className="rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none"
|
||||
>
|
||||
<option value="">All Actions</option>
|
||||
{actions?.map((action) => (
|
||||
<option key={action} value={action}>
|
||||
{action.replace(/_/g, " ")}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
{/* Entity type filter */}
|
||||
<select
|
||||
value={filters.entity_type || ""}
|
||||
onChange={(e) => handleFilterChange("entity_type", e.target.value)}
|
||||
className="rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none"
|
||||
>
|
||||
<option value="">All Entity Types</option>
|
||||
{entityTypes?.map((type) => (
|
||||
<option key={type} value={type}>
|
||||
{type}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
{/* Date range */}
|
||||
<input
|
||||
type="date"
|
||||
value={filters.start_date?.split("T")[0] || ""}
|
||||
onChange={(e) =>
|
||||
handleFilterChange(
|
||||
"start_date",
|
||||
e.target.value ? `${e.target.value}T00:00:00` : ""
|
||||
)
|
||||
}
|
||||
className="rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none"
|
||||
placeholder="Start date"
|
||||
/>
|
||||
<span className="text-gray-500">to</span>
|
||||
<input
|
||||
type="date"
|
||||
value={filters.end_date?.split("T")[0] || ""}
|
||||
onChange={(e) =>
|
||||
handleFilterChange(
|
||||
"end_date",
|
||||
e.target.value ? `${e.target.value}T23:59:59` : ""
|
||||
)
|
||||
}
|
||||
className="rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none"
|
||||
placeholder="End date"
|
||||
/>
|
||||
|
||||
{hasActiveFilters && (
|
||||
<button
|
||||
onClick={clearFilters}
|
||||
className="flex items-center gap-1 rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-400 hover:border-red-500/50 hover:text-red-400"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
Clear
|
||||
</button>
|
||||
)}
|
||||
|
||||
<div className="ml-auto text-sm text-gray-500">
|
||||
{logsData?.total || 0} total records
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Logs Table */}
|
||||
<div className="rounded-xl border border-gray-800 bg-gray-900">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-left text-sm">
|
||||
<thead>
|
||||
<tr className="border-b border-gray-800">
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Timestamp</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">User</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Action</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Entity</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Details</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{logsData?.items.map((log) => (
|
||||
<tr
|
||||
key={log.id}
|
||||
className="border-b border-gray-800/50 hover:bg-gray-800/30 transition-colors"
|
||||
>
|
||||
<td className="px-6 py-4">
|
||||
<span className="font-mono text-xs text-gray-400">
|
||||
{formatDate(log.timestamp)}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<span className="text-gray-200">
|
||||
{log.username || "System"}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<span className="inline-flex rounded-full border border-cyan-500/30 bg-cyan-900/30 px-2 py-0.5 text-xs font-medium text-cyan-400">
|
||||
{log.action.replace(/_/g, " ")}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
{log.entity_type && (
|
||||
<div>
|
||||
<span className="text-gray-300">{log.entity_type}</span>
|
||||
{log.entity_id && (
|
||||
<span className="ml-1 font-mono text-xs text-gray-500">
|
||||
({log.entity_id.slice(0, 8)}...)
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{!log.entity_type && <span className="text-gray-600">—</span>}
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
{log.details ? (
|
||||
<details className="cursor-pointer">
|
||||
<summary className="text-xs text-gray-400 hover:text-gray-200">
|
||||
View details
|
||||
</summary>
|
||||
<pre className="mt-2 max-w-xs overflow-auto rounded bg-gray-800 p-2 text-xs text-gray-300">
|
||||
{formatDetails(log.details)}
|
||||
</pre>
|
||||
</details>
|
||||
) : (
|
||||
<span className="text-gray-600">—</span>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{logsData?.items.length === 0 && (
|
||||
<div className="py-12 text-center text-gray-400">
|
||||
No audit logs found
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Pagination */}
|
||||
{logsData && logsData.total > PAGE_SIZE && (
|
||||
<div className="flex items-center justify-between border-t border-gray-800 px-6 py-4">
|
||||
<div className="text-sm text-gray-400">
|
||||
Showing {(filters.offset || 0) + 1} to{" "}
|
||||
{Math.min((filters.offset || 0) + PAGE_SIZE, logsData.total)} of{" "}
|
||||
{logsData.total}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
onClick={() => goToPage((filters.offset || 0) - PAGE_SIZE)}
|
||||
disabled={(filters.offset || 0) === 0}
|
||||
className="flex items-center gap-1 rounded-lg border border-gray-700 px-3 py-1.5 text-sm text-gray-400 hover:bg-gray-800 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
<ChevronLeft className="h-4 w-4" />
|
||||
Previous
|
||||
</button>
|
||||
<span className="text-sm text-gray-400">
|
||||
Page {currentPage} of {totalPages}
|
||||
</span>
|
||||
<button
|
||||
onClick={() => goToPage((filters.offset || 0) + PAGE_SIZE)}
|
||||
disabled={(filters.offset || 0) + PAGE_SIZE >= logsData.total}
|
||||
className="flex items-center gap-1 rounded-lg border border-gray-700 px-3 py-1.5 text-sm text-gray-400 hover:bg-gray-800 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
Next
|
||||
<ChevronRight className="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
472
frontend/src/pages/UsersPage.tsx
Normal file
472
frontend/src/pages/UsersPage.tsx
Normal file
@@ -0,0 +1,472 @@
|
||||
import { useState } from "react";
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import {
|
||||
Loader2,
|
||||
AlertCircle,
|
||||
Users,
|
||||
Plus,
|
||||
X,
|
||||
Check,
|
||||
UserX,
|
||||
UserCheck,
|
||||
Edit,
|
||||
} from "lucide-react";
|
||||
import { getUsers, createUser, updateUser, type UserOut, type UserCreatePayload } from "../api/users";
|
||||
|
||||
const ROLES = [
|
||||
{ value: "viewer", label: "Viewer" },
|
||||
{ value: "red_tech", label: "Red Tech" },
|
||||
{ value: "blue_tech", label: "Blue Tech" },
|
||||
{ value: "red_lead", label: "Red Lead" },
|
||||
{ value: "blue_lead", label: "Blue Lead" },
|
||||
{ value: "admin", label: "Admin" },
|
||||
];
|
||||
|
||||
const roleBadgeColors: Record<string, string> = {
|
||||
admin: "bg-purple-900/50 text-purple-400 border-purple-500/30",
|
||||
red_tech: "bg-red-900/50 text-red-400 border-red-500/30",
|
||||
blue_tech: "bg-blue-900/50 text-blue-400 border-blue-500/30",
|
||||
red_lead: "bg-orange-900/50 text-orange-400 border-orange-500/30",
|
||||
blue_lead: "bg-cyan-900/50 text-cyan-400 border-cyan-500/30",
|
||||
viewer: "bg-gray-800/50 text-gray-400 border-gray-600/30",
|
||||
};
|
||||
|
||||
export default function UsersPage() {
|
||||
const queryClient = useQueryClient();
|
||||
const [showCreateModal, setShowCreateModal] = useState(false);
|
||||
const [editingUser, setEditingUser] = useState<UserOut | null>(null);
|
||||
|
||||
const {
|
||||
data: users,
|
||||
isLoading,
|
||||
error,
|
||||
} = useQuery({
|
||||
queryKey: ["users"],
|
||||
queryFn: getUsers,
|
||||
});
|
||||
|
||||
const createMutation = useMutation({
|
||||
mutationFn: createUser,
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["users"] });
|
||||
setShowCreateModal(false);
|
||||
},
|
||||
});
|
||||
|
||||
const updateMutation = useMutation({
|
||||
mutationFn: ({ userId, payload }: { userId: string; payload: Parameters<typeof updateUser>[1] }) =>
|
||||
updateUser(userId, payload),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["users"] });
|
||||
setEditingUser(null);
|
||||
},
|
||||
});
|
||||
|
||||
const toggleUserActive = (user: UserOut) => {
|
||||
updateMutation.mutate({
|
||||
userId: user.id,
|
||||
payload: { is_active: !user.is_active },
|
||||
});
|
||||
};
|
||||
|
||||
const formatDate = (dateStr: string | null) => {
|
||||
if (!dateStr) return "Never";
|
||||
return new Date(dateStr).toLocaleDateString("en-US", {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
});
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex h-64 items-center justify-center">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-cyan-400" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="flex h-64 flex-col items-center justify-center gap-2">
|
||||
<AlertCircle className="h-10 w-10 text-red-400" />
|
||||
<p className="text-red-400">Failed to load users</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold text-white">User Management</h1>
|
||||
<p className="mt-1 text-sm text-gray-400">
|
||||
Manage user accounts and permissions
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setShowCreateModal(true)}
|
||||
className="flex items-center gap-1.5 rounded-lg bg-cyan-600 px-4 py-2 text-sm font-medium text-white hover:bg-cyan-500 transition-colors"
|
||||
>
|
||||
<Plus className="h-4 w-4" />
|
||||
New User
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Users Table */}
|
||||
<div className="rounded-xl border border-gray-800 bg-gray-900">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-left text-sm">
|
||||
<thead>
|
||||
<tr className="border-b border-gray-800">
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Username</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Email</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Role</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Status</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Created</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Last Login</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{users?.map((user) => (
|
||||
<tr
|
||||
key={user.id}
|
||||
className="border-b border-gray-800/50 hover:bg-gray-800/30 transition-colors"
|
||||
>
|
||||
<td className="px-6 py-4">
|
||||
<span className="font-medium text-white">{user.username}</span>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-gray-400">
|
||||
{user.email || "—"}
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<span
|
||||
className={`inline-flex rounded-full border px-2 py-0.5 text-xs font-medium ${
|
||||
roleBadgeColors[user.role] || roleBadgeColors.viewer
|
||||
}`}
|
||||
>
|
||||
{user.role.replace(/_/g, " ")}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
{user.is_active ? (
|
||||
<span className="inline-flex items-center gap-1 text-green-400">
|
||||
<Check className="h-3.5 w-3.5" />
|
||||
Active
|
||||
</span>
|
||||
) : (
|
||||
<span className="inline-flex items-center gap-1 text-red-400">
|
||||
<X className="h-3.5 w-3.5" />
|
||||
Inactive
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-gray-400">
|
||||
{formatDate(user.created_at)}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-gray-400">
|
||||
{formatDate(user.last_login)}
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
onClick={() => setEditingUser(user)}
|
||||
className="rounded p-1.5 text-gray-400 hover:bg-gray-800 hover:text-cyan-400"
|
||||
title="Edit user"
|
||||
>
|
||||
<Edit className="h-4 w-4" />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => toggleUserActive(user)}
|
||||
disabled={updateMutation.isPending}
|
||||
className={`rounded p-1.5 ${
|
||||
user.is_active
|
||||
? "text-gray-400 hover:bg-red-900/50 hover:text-red-400"
|
||||
: "text-gray-400 hover:bg-green-900/50 hover:text-green-400"
|
||||
}`}
|
||||
title={user.is_active ? "Deactivate" : "Activate"}
|
||||
>
|
||||
{user.is_active ? (
|
||||
<UserX className="h-4 w-4" />
|
||||
) : (
|
||||
<UserCheck className="h-4 w-4" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{users?.length === 0 && (
|
||||
<div className="py-12 text-center text-gray-400">
|
||||
No users found
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Create User Modal */}
|
||||
{showCreateModal && (
|
||||
<CreateUserModal
|
||||
onClose={() => setShowCreateModal(false)}
|
||||
onSubmit={(data) => createMutation.mutate(data)}
|
||||
isSubmitting={createMutation.isPending}
|
||||
error={createMutation.error as Error | null}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Edit User Modal */}
|
||||
{editingUser && (
|
||||
<EditUserModal
|
||||
user={editingUser}
|
||||
onClose={() => setEditingUser(null)}
|
||||
onSubmit={(payload) =>
|
||||
updateMutation.mutate({ userId: editingUser.id, payload })
|
||||
}
|
||||
isSubmitting={updateMutation.isPending}
|
||||
error={updateMutation.error as Error | null}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ── Create User Modal ──────────────────────────────────────────────
|
||||
|
||||
interface CreateUserModalProps {
|
||||
onClose: () => void;
|
||||
onSubmit: (data: UserCreatePayload) => void;
|
||||
isSubmitting: boolean;
|
||||
error: Error | null;
|
||||
}
|
||||
|
||||
function CreateUserModal({ onClose, onSubmit, isSubmitting, error }: CreateUserModalProps) {
|
||||
const [formData, setFormData] = useState({
|
||||
username: "",
|
||||
email: "",
|
||||
password: "",
|
||||
role: "viewer",
|
||||
});
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const validate = () => {
|
||||
const newErrors: Record<string, string> = {};
|
||||
if (!formData.username.trim()) newErrors.username = "Username is required";
|
||||
if (!formData.password) newErrors.password = "Password is required";
|
||||
if (formData.password.length < 6) newErrors.password = "Password must be at least 6 characters";
|
||||
setErrors(newErrors);
|
||||
return Object.keys(newErrors).length === 0;
|
||||
};
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (validate()) {
|
||||
onSubmit({
|
||||
username: formData.username,
|
||||
email: formData.email || undefined,
|
||||
password: formData.password,
|
||||
role: formData.role,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm">
|
||||
<div className="w-full max-w-md rounded-xl border border-gray-800 bg-gray-900 p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h3 className="text-lg font-semibold text-white">Create New User</h3>
|
||||
<button onClick={onClose} className="text-gray-400 hover:text-white">
|
||||
<X className="h-5 w-5" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 rounded-lg border border-red-500/30 bg-red-900/20 p-3">
|
||||
<p className="text-sm text-red-400">{error.message}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Username *</label>
|
||||
<input
|
||||
type="text"
|
||||
value={formData.username}
|
||||
onChange={(e) => setFormData({ ...formData, username: e.target.value })}
|
||||
className={`mt-1 w-full rounded-lg border bg-gray-800 px-3 py-2 text-gray-200 ${
|
||||
errors.username ? "border-red-500" : "border-gray-700"
|
||||
}`}
|
||||
/>
|
||||
{errors.username && <p className="mt-1 text-sm text-red-400">{errors.username}</p>}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Email</label>
|
||||
<input
|
||||
type="email"
|
||||
value={formData.email}
|
||||
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
|
||||
className="mt-1 w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-gray-200"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Password *</label>
|
||||
<input
|
||||
type="password"
|
||||
value={formData.password}
|
||||
onChange={(e) => setFormData({ ...formData, password: e.target.value })}
|
||||
className={`mt-1 w-full rounded-lg border bg-gray-800 px-3 py-2 text-gray-200 ${
|
||||
errors.password ? "border-red-500" : "border-gray-700"
|
||||
}`}
|
||||
/>
|
||||
{errors.password && <p className="mt-1 text-sm text-red-400">{errors.password}</p>}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Role</label>
|
||||
<select
|
||||
value={formData.role}
|
||||
onChange={(e) => setFormData({ ...formData, role: e.target.value })}
|
||||
className="mt-1 w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-gray-200"
|
||||
>
|
||||
{ROLES.map((role) => (
|
||||
<option key={role.value} value={role.value}>
|
||||
{role.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end gap-3 pt-4">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
className="rounded-lg border border-gray-700 px-4 py-2 text-sm text-gray-400 hover:bg-gray-800"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isSubmitting}
|
||||
className="flex items-center gap-1.5 rounded-lg bg-cyan-600 px-4 py-2 text-sm font-medium text-white hover:bg-cyan-500 disabled:opacity-50"
|
||||
>
|
||||
{isSubmitting && <Loader2 className="h-4 w-4 animate-spin" />}
|
||||
Create User
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ── Edit User Modal ──────────────────────────────────────────────
|
||||
|
||||
interface EditUserModalProps {
|
||||
user: UserOut;
|
||||
onClose: () => void;
|
||||
onSubmit: (payload: Parameters<typeof updateUser>[1]) => void;
|
||||
isSubmitting: boolean;
|
||||
error: Error | null;
|
||||
}
|
||||
|
||||
function EditUserModal({ user, onClose, onSubmit, isSubmitting, error }: EditUserModalProps) {
|
||||
const [formData, setFormData] = useState({
|
||||
email: user.email || "",
|
||||
role: user.role,
|
||||
password: "",
|
||||
});
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
const payload: Parameters<typeof updateUser>[1] = {
|
||||
email: formData.email || undefined,
|
||||
role: formData.role,
|
||||
};
|
||||
if (formData.password) {
|
||||
payload.password = formData.password;
|
||||
}
|
||||
onSubmit(payload);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm">
|
||||
<div className="w-full max-w-md rounded-xl border border-gray-800 bg-gray-900 p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h3 className="text-lg font-semibold text-white">Edit User: {user.username}</h3>
|
||||
<button onClick={onClose} className="text-gray-400 hover:text-white">
|
||||
<X className="h-5 w-5" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 rounded-lg border border-red-500/30 bg-red-900/20 p-3">
|
||||
<p className="text-sm text-red-400">{error.message}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Email</label>
|
||||
<input
|
||||
type="email"
|
||||
value={formData.email}
|
||||
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
|
||||
className="mt-1 w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-gray-200"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Role</label>
|
||||
<select
|
||||
value={formData.role}
|
||||
onChange={(e) => setFormData({ ...formData, role: e.target.value })}
|
||||
className="mt-1 w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-gray-200"
|
||||
>
|
||||
{ROLES.map((role) => (
|
||||
<option key={role.value} value={role.value}>
|
||||
{role.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">
|
||||
New Password <span className="text-gray-500">(leave blank to keep current)</span>
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
value={formData.password}
|
||||
onChange={(e) => setFormData({ ...formData, password: e.target.value })}
|
||||
className="mt-1 w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-gray-200"
|
||||
placeholder="••••••••"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end gap-3 pt-4">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
className="rounded-lg border border-gray-700 px-4 py-2 text-sm text-gray-400 hover:bg-gray-800"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isSubmitting}
|
||||
className="flex items-center gap-1.5 rounded-lg bg-cyan-600 px-4 py-2 text-sm font-medium text-white hover:bg-cyan-500 disabled:opacity-50"
|
||||
>
|
||||
{isSubmitting && <Loader2 className="h-4 w-4 animate-spin" />}
|
||||
Save Changes
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user