"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { updateUser } from "@/actions/users"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { useServerAction } from "@/hooks/use-server-action"; import { type UpdateUserInput, updateUserSchema } from "@/lib/validators/user"; interface UserEditFormProps { user: { id: number; username: string; mail: string; rank: number; motto: string; credits: number; pixels: number; diamonds: number; duckets: number; }; /** Ranks loaded from the `permissions` table (id + human-readable name). */ ranks?: Array<{ id: number; name: string }>; } // Fallback used if the page does not supply the DB-loaded rank list. const FALLBACK_RANKS: Array<{ id: number; name: string }> = [ { id: 1, name: "User" }, { id: 2, name: "Bronze" }, { id: 3, name: "Silver" }, { id: 4, name: "Gold" }, { id: 5, name: "Moderator" }, { id: 6, name: "Admin" }, { id: 7, name: "Super Admin" }, ]; export function UserEditForm({ user, ranks }: UserEditFormProps) { const rankOptions = ranks && ranks.length > 0 ? ranks : FALLBACK_RANKS; const { isPending, run } = useServerAction(); const { register, handleSubmit, setValue, formState: { errors }, } = useForm({ resolver: zodResolver(updateUserSchema), defaultValues: { username: user.username, mail: user.mail, rank: user.rank, motto: user.motto, credits: user.credits, pixels: user.pixels, diamonds: user.diamonds, duckets: user.duckets, }, }); function onSubmit(data: UpdateUserInput) { run(() => updateUser({ id: user.id, ...data }), { successMessage: "User updated successfully.", }); } return ( Edit User
{errors.username &&

{errors.username.message}

}
{errors.mail &&

{errors.mail.message}

}
); }