import { useState, useRef, useEffect } from "react"; import { useCurrentUser, useUpdateProfile } from "../../../hooks/useUser"; export default function MentorSettingsPage() { const { user, loading: userLoading } = useCurrentUser(); const { save, loading: saving, error: errorMsg, successMsg } = useUpdateProfile(); const [formData, setFormData] = useState({ fullName: "", email: "", bio: "", }); // File state untuk profile_picture const [photoFile, setPhotoFile] = useState(null); const [photoPreview, setPhotoPreview] = useState(null); const fileInputRef = useRef(null); // Sync form data ketika user sudah di-fetch useEffect(() => { if (user) { setFormData({ fullName: user.full_name || "", email: user.email || "", bio: user.professional_bio || "", }); if (user.profile_picture) { setPhotoPreview(user.profile_picture); } } }, [user]); const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value })); }; const handlePhotoUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { setPhotoFile(file); const reader = new FileReader(); reader.onloadend = () => setPhotoPreview(reader.result as string); reader.readAsDataURL(file); } }; const handleRemovePhoto = () => { setPhotoFile(null); setPhotoPreview(null); if (fileInputRef.current) fileInputRef.current.value = ""; }; const handleSaveChanges = async (e: React.FormEvent) => { e.preventDefault(); await save({ full_name: formData.fullName, email: formData.email, professional_bio: formData.bio, ...(photoFile ? { profile_picture: photoFile } : {}), }); // Reset file state setelah berhasil save if (!errorMsg) { setPhotoFile(null); } }; const initialLetter = formData.fullName ? formData.fullName.charAt(0).toUpperCase() : "U"; if (userLoading) { return (
Loading profile...
); } return (
{/* Header Title */}

Settings

Manage and edit your profile and account information.

{/* White Card Container */}
{/* Card Header */}

Profile Information

Update your photo and personal details.

{/* Card Body & Form */}
{/* Profile Photo Row */}
{/* Avatar dengan badge edit */}
{photoPreview ? ( Profile ) : ( {initialLetter} )}
{/* Badge edit icon */}
{/* Info & Action Buttons */}

Profile Photo

JPG, GIF or PNG. Recommended size 800x800px. Max size of 800K.

{photoFile && (

📎 {photoFile.name}

)}
{/* Hidden file input */}

{/* Grid Inputs */}
{/* Professional Bio */}