"use client"; import { useEffect, useState, useRef } from "react"; import { useAppDispatch, useAppSelector } from "@/lib/hooks"; import { fetchProfile, updateProfile, changePassword, changeEmail } from "@/lib/features/auth/authSlice"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { getAvatarUrl } from "@/lib/utils"; import Swal from "sweetalert2"; import { Loader2, Camera, Shield, Mail, User as UserIcon } from "lucide-react"; export default function ProfilePage() { const dispatch = useAppDispatch(); const { user, isLoading } = useAppSelector((state) => state.auth); const [file, setFile] = useState(null); const fileInputRef = useRef(null); // Form states const [username, setUsername] = useState(""); // Password change states const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); // Email change states const [newEmail, setNewEmail] = useState(""); const [emailPassword, setEmailPassword] = useState(""); const [isMounted, setIsMounted] = useState(false); useEffect(() => { setIsMounted(true); dispatch(fetchProfile()); }, [dispatch]); useEffect(() => { if (user) { setUsername(user.username); } }, [user]); const handleAvatarChange = (e: React.ChangeEvent) => { if (e.target.files && e.target.files[0]) { setFile(e.target.files[0]); } }; const handleProfileUpdate = () => { const formData = new FormData(); formData.append("user_name", username); if (file) { formData.append("avatar", file); } dispatch(updateProfile(formData)) .unwrap() .then(() => { Swal.fire("Başarılı", "Profil bilgileriniz güncellendi.", "success"); setFile(null); }) .catch((err) => { Swal.fire("Hata", err || "Profil güncellenemedi.", "error"); }); }; const handleChangePassword = () => { if (!currentPassword || !newPassword) { Swal.fire("Uyarı", "Lütfen tüm alanları doldurun.", "warning"); return; } if (newPassword.length < 6) { Swal.fire("Uyarı", "Yeni şifre en az 6 karakter olmalıdır.", "warning"); return; } dispatch(changePassword({ current_password: currentPassword, new_password: newPassword })) .unwrap() .then(() => { Swal.fire("Başarılı", "Şifreniz değiştirildi. Lütfen yeni şifrenizle tekrar giriş yapın.", "success"); setCurrentPassword(""); setNewPassword(""); }) .catch((err) => { Swal.fire("Hata", err || "Şifre değiştirilemedi.", "error"); }); }; const handleChangeEmail = () => { if (!newEmail || !emailPassword) { Swal.fire("Uyarı", "Lütfen tüm alanları doldurun.", "warning"); return; } dispatch(changeEmail({ new_email: newEmail, password: emailPassword })) .unwrap() .then((res: any) => { Swal.fire({ title: "Başarılı", text: `Email adresiniz güncellendi. Lütfen ${newEmail} adresine gönderilen doğrulama linkine tıklayın.`, icon: "success" }); setNewEmail(""); setEmailPassword(""); }) .catch((err) => { Swal.fire("Hata", err || "Email değiştirilemedi.", "error"); }); }; if (!isMounted) { return
; } if (!user && isLoading) { return
; } if (!user) { return
Kullanıcı bilgileri yüklenemedi.
; } return (

Profilim

{/* Profile Info & Edit */}
Profil Bilgileri Kişisel bilgilerinizi buradan güncelleyebilirsiniz.
{user.username.slice(0, 2).toUpperCase()}

{user.username}

{user.email}

{user.roles.map(role => ( {role.name} ))} {user.is_oauth_user ? ( OAuth ) : ( user.email_verified && Email Onaylı )}
setUsername(e.target.value)} className="pl-9" />
{/* Security Settings - Only for non-OAuth users */} {!user.is_oauth_user && (
{/* Change Password */} Şifre Değiştir Güvenliğiniz için güçlü bir şifre kullanın.
setCurrentPassword(e.target.value)} />
setNewPassword(e.target.value)} />
{/* Change Email */} Email Değiştir Email adresinizi değiştirirseniz yeniden doğrulama yapmanız gerekir.
setNewEmail(e.target.value)} />
setEmailPassword(e.target.value)} />
)} {/* OAuth Info - Only for OAuth users */} {user.is_oauth_user && ( Bağlı Hesaplar Hesabınız aşağıdaki sosyal medya platformlarına bağlıdır.
{user.social_accounts?.map((account) => (
{account.provider === 'google' ? 'G' : account.provider.charAt(0).toUpperCase()}

{account.provider}

{account.email}

))} {(!user.social_accounts || user.social_accounts.length === 0) && (

Bağlı hesap bilgisi bulunamadı (OAuth User flag true olmasına rağmen).

)}
)}
); }