feat: Settings Users page with toggle active
This commit is contained in:
@@ -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 ? (
|
|
||||||
<TableRow><TableCell colSpan={5} className="text-center py-12 text-slate-400">No users found</TableCell></TableRow>
|
|
||||||
) : (
|
|
||||||
users.map((u) => {
|
|
||||||
const roles = u.roleAssignments?.map(r => r.role) ?? [];
|
|
||||||
return (
|
|
||||||
<TableRow key={u.id} className="hover:bg-slate-50">
|
|
||||||
<TableCell className="font-medium">{u.firstName} {u.lastName}</TableCell>
|
|
||||||
<TableCell className="text-sm text-slate-500">{u.email}</TableCell>
|
|
||||||
<TableCell>
|
|
||||||
<div className="flex gap-1 flex-wrap">
|
|
||||||
{roles.length === 0 ? <span className="text-xs text-slate-400">No role</span> :
|
|
||||||
roles.map(r => (
|
|
||||||
<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>
|
|
||||||
))}
|
))}
|
||||||
</div>
|
</tr>
|
||||||
</TableCell>
|
))
|
||||||
<TableCell>
|
: users?.map((u) => {
|
||||||
<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'}`}>
|
const role = u.roles?.[0]?.role ?? 'STAFF';
|
||||||
|
return (
|
||||||
|
<tr key={u.id} className="border-b hover:bg-slate-50">
|
||||||
|
<td className="px-4 py-3 font-medium text-slate-800">
|
||||||
|
{u.firstName} {u.lastName}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-3 text-slate-600 hidden md:table-cell">{u.email}</td>
|
||||||
|
<td className="px-4 py-3">
|
||||||
|
<span className={`px-2 py-0.5 rounded-full text-xs font-medium ${roleColors[role] ?? 'bg-gray-100 text-gray-500'}`}>
|
||||||
|
{role}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-3">
|
||||||
|
<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'}`}>
|
||||||
{u.isActive ? 'Active' : 'Inactive'}
|
{u.isActive ? 'Active' : 'Inactive'}
|
||||||
</span>
|
</span>
|
||||||
</TableCell>
|
</td>
|
||||||
<TableCell className="text-xs text-slate-400">
|
<td className="px-4 py-3">
|
||||||
{format(new Date(u.createdAt), 'MMM d, yyyy')}
|
<button
|
||||||
</TableCell>
|
onClick={() => toggleActive.mutate({ id: u.id, isActive: u.isActive })}
|
||||||
</TableRow>
|
className="text-xs text-slate-500 hover:text-slate-800 underline"
|
||||||
|
>
|
||||||
|
{u.isActive ? 'Deactivate' : 'Activate'}
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
);
|
);
|
||||||
})
|
})}
|
||||||
)}
|
</tbody>
|
||||||
</TableBody>
|
</table>
|
||||||
</Table>
|
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user