"use client"; import { useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Separator } from "@/components/ui/separator"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { updateUser } from "@/actions/users"; import { useToast } from "@/components/ui/use-toast"; import { Eye, EyeOff, ShieldAlert } from "lucide-react"; export interface EditableUser { id: string; name: string | null; email: string; role: string; isActive: boolean; } interface EditUserDialogProps { user: EditableUser; isSelf: boolean; open: boolean; onClose: () => void; onSuccess?: () => void; } export function EditUserDialog({ user, isSelf, open, onClose, onSuccess }: EditUserDialogProps) { const { toast } = useToast(); const [isSubmitting, setIsSubmitting] = useState(false); const [showPassword, setShowPassword] = useState(false); const [form, setForm] = useState({ name: user.name ?? "", role: user.role, isActive: user.isActive, newPassword: "", }); const [passwordError, setPasswordError] = useState(""); const set = (key: K, value: typeof form[K]) => { setForm((f) => ({ ...f, [key]: value })); if (key === "newPassword") setPasswordError(""); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (form.newPassword && form.newPassword.length < 8) { setPasswordError("At least 8 characters"); return; } setIsSubmitting(true); try { await updateUser({ userId: user.id, name: form.name || undefined, role: form.role as "ADMIN" | "PRODUCER" | "SUPERVISOR" | "ARTIST" | "CLIENT", isActive: form.isActive, newPassword: form.newPassword || undefined, }); toast({ title: "User updated" }); onSuccess?.(); onClose(); } catch (err) { toast({ title: "Failed to update user", description: err instanceof Error ? err.message : undefined, variant: "destructive" }); } finally { setIsSubmitting(false); } }; return ( !v && onClose()}> Edit User
{/* Read-only email */}

{user.email}

set("name", e.target.value)} placeholder="Jane Smith" />
{isSelf && (

You cannot change your own role or deactivate yourself.

)} {/* Password reset */}
set("newPassword", e.target.value)} placeholder="Min. 8 characters" autoComplete="new-password" className="pr-10" />
{passwordError &&

{passwordError}

}
); }