"use client" import { useState } from "react" interface User { id: string name: string email: string role: string points: number createdAt: Date } export default function AdminUserList({ users }: { users: User[] }) { const [resettingUserId, setResettingUserId] = useState(null) const [newPassword, setNewPassword] = useState>({}) const [error, setError] = useState("") const [success, setSuccess] = useState("") const handleResetPassword = async (userId: string) => { const password = newPassword[userId] if (!password || password.length < 6) { setError("Password must be at least 6 characters") return } setError("") setResettingUserId(userId) try { const response = await fetch(`/api/admin/users/${userId}/reset-password`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ password }), }) const data = await response.json() if (!response.ok) { setError(data.error || "Failed to reset password") } else { setSuccess("Password reset successfully!") setNewPassword({ ...newPassword, [userId]: "" }) setTimeout(() => { setSuccess("") }, 3000) } } catch { setError("An error occurred. Please try again.") } finally { setResettingUserId(null) } } return (
{error && (

{error}

)} {success && (

{success}

)}
{users.map((user) => ( ))}
Name Email Role Points Actions
{user.name} {user.email} {user.role} {user.points}
setNewPassword({ ...newPassword, [user.id]: e.target.value }) } className="px-2 py-1 border border-gray-300 rounded text-xs w-32 bg-white text-gray-900" />
) }