"use client" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { User, UserPayload } from "@/types/user" import { useState } from "react" import { userService } from "@/services/userService" import { useSession } from "next-auth/react" import { toast } from "sonner" import { useRouter } from "next/navigation" interface UserDialogProps { open: boolean onOpenChange: (open: boolean) => void user: User onSuccess?: () => void } export function UserDialog({ open, onOpenChange, user, onSuccess }: UserDialogProps) { const { data: session } = useSession() const router = useRouter() const [loading, setLoading] = useState(false) const [formData, setFormData] = useState({ username: user.username, email: user.email, is_admin: user.is_admin, password: "", }) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!session?.user?.accessToken) return setLoading(true) try { // Create a payload copy to manipulate const payload = { ...formData } // Remove password if it's empty so we don't overwrite with empty string if (!payload.password || payload.password.trim() === "") { delete payload.password } await userService.updateUser(session.user.accessToken, user.id, payload) toast.success("Kullanıcı güncellendi") onOpenChange(false) if (onSuccess) onSuccess() // Trigger parent refresh router.refresh() } catch (error) { toast.error("Kullanıcı güncellenirken bir hata oluştu") console.error(error) } finally { setLoading(false) } } return ( Kullanıcı Düzenle Kullanıcı bilgilerini buradan güncelleyebilirsiniz. Şifre alanını boş bırakırsanız değişmez.
setFormData({ ...formData, username: e.target.value })} className="col-span-3" />
setFormData({ ...formData, email: e.target.value })} className="col-span-3" />
setFormData({ ...formData, password: e.target.value })} className="col-span-3" />
{/* Admin role toggle could be added here if needed */}
) }