import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import axios from 'axios'; import { toast } from 'sonner'; interface User { id: string; name: string; email: string; role: string; created_at: string; } export const UserManagement = () => { const queryClient = useQueryClient(); // Fetch Users const { data: users, isLoading, isError, } = useQuery({ queryKey: ['users'], queryFn: async () => { const res = await axios.get('http://localhost:8080/api/v1/users'); return res.data.data as User[]; }, }); // Update Role const updateRoleMutation = useMutation({ mutationFn: async ({ id, role }: { id: string; role: string }) => { await axios.put(`http://localhost:8080/api/v1/users/${id}/role`, { role, }); }, onSuccess: () => { toast.success('User role updated'); queryClient.invalidateQueries({ queryKey: ['users'] }); }, onError: () => toast.error('Failed to update user role'), }); // Delete User const deleteMutation = useMutation({ mutationFn: async (id: string) => { await axios.delete(`http://localhost:8080/api/v1/users/${id}`); }, onSuccess: () => { toast.success('User deleted'); queryClient.invalidateQueries({ queryKey: ['users'] }); }, onError: () => toast.error('Failed to delete user'), }); if (isLoading) return
Loading users...
; if (isError) return
Error loading users.
; return (

Users

{users?.map((user) => ( ))}
Name Email Role Actions
{user.name} {user.email}
); };