feat: Settings Users page with toggle active

This commit is contained in:
Forge
2026-03-25 08:41:51 +08:00
parent d3792d19eb
commit 56482bf66a

View File

@@ -1,105 +1,114 @@
'use client'; 'use client';
import { useQuery } from '@tanstack/react-query'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { Card, CardContent } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Skeleton } from '@/components/ui/skeleton';
import {
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
} from '@/components/ui/table';
import { UserPlus, Shield, User } from 'lucide-react';
import { api } from '@/lib/api'; import { api } from '@/lib/api';
import { format } from 'date-fns'; import { Card, CardContent } from '@/components/ui/card';
import { Skeleton } from '@/components/ui/skeleton';
import { Badge } from '@/components/ui/badge';
import { UserPlus } from 'lucide-react';
interface UserItem { interface User {
id: string; id: string;
firstName: string; firstName: string;
lastName: string; lastName: string;
email: string; email: string;
isActive: boolean; isActive: boolean;
createdAt: string; roles: Array<{ role: string }>;
roleAssignments?: { role: string }[];
} }
const ROLE_COLORS: Record<string, string> = { const roleColors: Record<string, string> = {
ADMIN: 'bg-purple-100 text-purple-700', ADMIN: 'bg-purple-100 text-purple-700',
STAFF: 'bg-blue-100 text-blue-700', STAFF: 'bg-blue-100 text-blue-700',
TECHNICIAN: 'bg-orange-100 text-orange-700', TECHNICIAN: 'bg-orange-100 text-orange-700',
COLLECTOR: 'bg-cyan-100 text-cyan-700', COLLECTOR: 'bg-green-100 text-green-700',
}; };
export default function UsersSettingsPage() { export default function UsersSettingsPage() {
const { data, isLoading } = useQuery({ const qc = useQueryClient();
const { data: users, isLoading } = useQuery<User[]>({
queryKey: ['users'], queryKey: ['users'],
queryFn: async () => (await api.get('/api/v1/users')).data, queryFn: async () => {
const res = await api.get('/api/v1/users');
return res.data;
},
}); });
const users: UserItem[] = Array.isArray(data) ? data : (data?.data ?? []); const toggleActive = useMutation({
mutationFn: async ({ id, isActive }: { id: string; isActive: boolean }) => {
await api.patch(`/api/v1/users/${id}`, { isActive: !isActive });
},
onSuccess: () => qc.invalidateQueries({ queryKey: ['users'] }),
});
return ( return (
<div> <div>
<div className="mb-6 flex items-center justify-between"> <div className="flex items-center justify-between mb-6">
<div> <div>
<h1 className="text-2xl font-bold text-slate-800">User Management</h1> <h1 className="text-2xl font-bold text-slate-800">Users</h1>
<p className="text-slate-500 text-sm mt-1">Staff accounts and role assignments</p> <p className="text-slate-500 text-sm mt-1">Manage staff access and roles</p>
</div> </div>
<Button style={{ backgroundColor: '#0891B2', color: 'white' }}> <button
<UserPlus size={16} className="mr-2" /> className="flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium text-white"
style={{ backgroundColor: '#0891B2' }}
>
<UserPlus size={16} />
Add User Add User
</Button> </button>
</div> </div>
<Card> <Card className="border shadow-sm">
<CardContent className="p-0"> <CardContent className="p-0">
<Table> <table className="w-full text-sm">
<TableHeader> <thead>
<TableRow> <tr className="border-b bg-slate-50">
<TableHead>Name</TableHead> <th className="text-left px-4 py-3 font-medium text-slate-500">Name</th>
<TableHead>Email</TableHead> <th className="text-left px-4 py-3 font-medium text-slate-500 hidden md:table-cell">Email</th>
<TableHead>Roles</TableHead> <th className="text-left px-4 py-3 font-medium text-slate-500">Role</th>
<TableHead>Status</TableHead> <th className="text-left px-4 py-3 font-medium text-slate-500">Status</th>
<TableHead>Joined</TableHead> <th className="px-4 py-3"></th>
</TableRow> </tr>
</TableHeader> </thead>
<TableBody> <tbody>
{isLoading ? ( {isLoading
Array.from({ length: 5 }).map((_, i) => ( ? Array.from({ length: 4 }).map((_, i) => (
<TableRow key={i}>{Array.from({ length: 5 }).map((_, j) => ( <tr key={i} className="border-b">
<TableCell key={j}><Skeleton className="h-4 w-full" /></TableCell> {[...Array(5)].map((_, j) => (
))}</TableRow> <td key={j} className="px-4 py-3"><Skeleton className="h-4 w-20" /></td>
)) ))}
) : users.length === 0 ? ( </tr>
<TableRow><TableCell colSpan={5} className="text-center py-12 text-slate-400">No users found</TableCell></TableRow> ))
) : ( : users?.map((u) => {
users.map((u) => { const role = u.roles?.[0]?.role ?? 'STAFF';
const roles = u.roleAssignments?.map(r => r.role) ?? []; return (
return ( <tr key={u.id} className="border-b hover:bg-slate-50">
<TableRow key={u.id} className="hover:bg-slate-50"> <td className="px-4 py-3 font-medium text-slate-800">
<TableCell className="font-medium">{u.firstName} {u.lastName}</TableCell> {u.firstName} {u.lastName}
<TableCell className="text-sm text-slate-500">{u.email}</TableCell> </td>
<TableCell> <td className="px-4 py-3 text-slate-600 hidden md:table-cell">{u.email}</td>
<div className="flex gap-1 flex-wrap"> <td className="px-4 py-3">
{roles.length === 0 ? <span className="text-xs text-slate-400">No role</span> : <span className={`px-2 py-0.5 rounded-full text-xs font-medium ${roleColors[role] ?? 'bg-gray-100 text-gray-500'}`}>
roles.map(r => ( {role}
<span key={r} className={`inline-flex px-2 py-0.5 rounded-full text-xs font-medium ${ROLE_COLORS[r] ?? 'bg-gray-100 text-gray-600'}`}>{r}</span> </span>
))} </td>
</div> <td className="px-4 py-3">
</TableCell> <span className={`px-2 py-0.5 rounded-full text-xs font-medium ${u.isActive ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'}`}>
<TableCell> {u.isActive ? 'Active' : 'Inactive'}
<span className={`inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium ${u.isActive ? 'bg-emerald-100 text-emerald-700' : 'bg-red-100 text-red-600'}`}> </span>
{u.isActive ? 'Active' : 'Inactive'} </td>
</span> <td className="px-4 py-3">
</TableCell> <button
<TableCell className="text-xs text-slate-400"> onClick={() => toggleActive.mutate({ id: u.id, isActive: u.isActive })}
{format(new Date(u.createdAt), 'MMM d, yyyy')} className="text-xs text-slate-500 hover:text-slate-800 underline"
</TableCell> >
</TableRow> {u.isActive ? 'Deactivate' : 'Activate'}
); </button>
}) </td>
)} </tr>
</TableBody> );
</Table> })}
</tbody>
</table>
</CardContent> </CardContent>
</Card> </Card>
</div> </div>