admin board
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { Search, ChevronDown, ChevronUp, ExternalLink, FileText, User, Calendar, Settings, ListChecks, Save } from "lucide-react";
|
||||
import { Search, ChevronDown, ChevronUp, ExternalLink, FileText, User, Users, Calendar, Settings, ListChecks, Save, ShieldCheck, ShieldOff } from "lucide-react";
|
||||
|
||||
export default function AdminDashboard() {
|
||||
const [reports, setReports] = useState<any[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [search, setSearch] = useState("");
|
||||
const [expandedId, setExpandedId] = useState<string | null>(null);
|
||||
const [tab, setTab] = useState<"REPORTS" | "SETTINGS">("REPORTS");
|
||||
const [tab, setTab] = useState<"REPORTS" | "USERS" | "SETTINGS">("REPORTS");
|
||||
const [folderId, setFolderId] = useState("");
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [users, setUsers] = useState<any[]>([]);
|
||||
const [usersLoading, setUsersLoading] = useState(false);
|
||||
const [togglingId, setTogglingId] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
fetchReports();
|
||||
@@ -45,6 +48,41 @@ export default function AdminDashboard() {
|
||||
}
|
||||
};
|
||||
|
||||
const fetchUsers = async () => {
|
||||
setUsersLoading(true);
|
||||
try {
|
||||
const res = await fetch("/api/admin/users");
|
||||
const data = await res.json();
|
||||
setUsers(data);
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch users");
|
||||
} finally {
|
||||
setUsersLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const toggleRole = async (userId: string, currentRole: string) => {
|
||||
setTogglingId(userId);
|
||||
const newRole = currentRole === "ADMIN" ? "EMPLOYEE" : "ADMIN";
|
||||
try {
|
||||
const res = await fetch("/api/admin/users", {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ userId, role: newRole }),
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.error) {
|
||||
alert(data.error);
|
||||
} else {
|
||||
setUsers(users.map(u => u.id === userId ? { ...u, role: newRole } : u));
|
||||
}
|
||||
} catch (error) {
|
||||
alert("Failed to update role");
|
||||
} finally {
|
||||
setTogglingId(null);
|
||||
}
|
||||
};
|
||||
|
||||
const fetchReports = async () => {
|
||||
try {
|
||||
const res = await fetch("/api/reports");
|
||||
@@ -76,7 +114,15 @@ export default function AdminDashboard() {
|
||||
>
|
||||
<ListChecks size={18} /> Reports
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={() => { setTab("USERS"); if (!users.length) fetchUsers(); }}
|
||||
className={`flex items-center gap-2 px-4 py-2 rounded-lg transition-colors text-sm font-medium ${
|
||||
tab === "USERS" ? "bg-accent-primary text-white" : "hover:bg-white/5 text-text-dim"
|
||||
}`}
|
||||
>
|
||||
<Users size={18} /> Users
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setTab("SETTINGS")}
|
||||
className={`flex items-center gap-2 px-4 py-2 rounded-lg transition-colors text-sm font-medium ${
|
||||
tab === "SETTINGS" ? "bg-accent-primary text-white" : "hover:bg-white/5 text-text-dim"
|
||||
@@ -164,6 +210,81 @@ export default function AdminDashboard() {
|
||||
))}
|
||||
{filteredReports.length === 0 && <p className="text-center py-10 text-text-dim">No reports found matching your criteria.</p>}
|
||||
</div>
|
||||
) : tab === "USERS" ? (
|
||||
<div className="space-y-4 animate-fade-in">
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold flex items-center gap-2">
|
||||
<Users size={20} className="text-accent-primary" /> Employee List
|
||||
</h3>
|
||||
<p className="text-sm text-text-dim">Manage admin privileges for your team.</p>
|
||||
</div>
|
||||
|
||||
{usersLoading ? (
|
||||
<div className="text-center py-10 animate-pulse text-text-dim">Loading users...</div>
|
||||
) : (
|
||||
<div className="grid gap-3">
|
||||
{users.map((user) => {
|
||||
const lastReport = user.reports?.[0];
|
||||
return (
|
||||
<div key={user.id} className="glass-card p-4 flex items-center justify-between gap-4">
|
||||
<div className="flex items-center gap-4 min-w-0">
|
||||
{user.image ? (
|
||||
<img src={user.image} alt={user.name} className="h-10 w-10 rounded-full flex-shrink-0" />
|
||||
) : (
|
||||
<div className="h-10 w-10 rounded-full bg-accent-primary/20 flex items-center justify-center text-accent-primary flex-shrink-0">
|
||||
<User size={20} />
|
||||
</div>
|
||||
)}
|
||||
<div className="min-w-0">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<p className="font-semibold truncate">{user.name || "Unnamed"}</p>
|
||||
<span className={`text-[10px] uppercase tracking-wider px-2 py-0.5 rounded-full font-bold flex-shrink-0 ${
|
||||
user.role === "ADMIN"
|
||||
? "bg-accent-primary/20 text-accent-primary"
|
||||
: "bg-white/10 text-text-dim"
|
||||
}`}>
|
||||
{user.role}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-xs text-text-dim truncate">{user.email}</p>
|
||||
{lastReport && (
|
||||
<p className="text-[10px] text-text-dim mt-0.5">
|
||||
Last report: {new Date(lastReport.date).toLocaleDateString()} •{" "}
|
||||
<span className={lastReport.status === "SUBMITTED" ? "text-green-400" : "text-yellow-400"}>
|
||||
{lastReport.status}
|
||||
</span>
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button
|
||||
onClick={() => toggleRole(user.id, user.role)}
|
||||
disabled={togglingId === user.id}
|
||||
className={`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium transition-colors flex-shrink-0 disabled:opacity-50 ${
|
||||
user.role === "ADMIN"
|
||||
? "bg-red-500/10 hover:bg-red-500/20 text-red-400 border border-red-500/20"
|
||||
: "bg-accent-primary/10 hover:bg-accent-primary/20 text-accent-primary border border-accent-primary/20"
|
||||
}`}
|
||||
title={user.role === "ADMIN" ? "Remove admin privileges" : "Grant admin privileges"}
|
||||
>
|
||||
{togglingId === user.id ? (
|
||||
"..."
|
||||
) : user.role === "ADMIN" ? (
|
||||
<><ShieldOff size={16} /> Remove Admin</>
|
||||
) : (
|
||||
<><ShieldCheck size={16} /> Make Admin</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
{users.length === 0 && (
|
||||
<p className="text-center py-10 text-text-dim">No users found.</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="glass-card p-8 space-y-8 animate-fade-in">
|
||||
<div className="space-y-4">
|
||||
|
||||
Reference in New Issue
Block a user