import { useState, useRef, useEffect } from "react"; import { useCurrentUser, useUpdateProfile, getInitials } from "../../hooks/useUser"; import { customToast } from "../utils/showToast"; import { Pen } from "lucide-react"; import UpdateProfileDialog from "./UpdateProfileDialogue"; import { resolveFileUrl } from "../utils/resolveFileFromUrl"; export default function SettingsPage() { const { user, loading: userLoading } = useCurrentUser(); const { save, loading: saving } = useUpdateProfile(); const [confirmOpen, setConfirmOpen] = useState(false); const isIntern = user?.role === "intern"; const hasPendingChanges = isIntern && (user?.changes ?? 0) > 0; const [formData, setFormData] = useState({ fullName: "", email: "", bio: "", }); const [formErrors, setFormErrors] = useState({ fullName: "", email: "", }); const [photoFile, setPhotoFile] = useState(null); const [photoPreview, setPhotoPreview] = useState(null); const [imageError, setImageError] = useState(false); const fileInputRef = useRef(null); useEffect(() => { if (!user) return; setFormData({ fullName: user.full_name || "", email: user.email || "", bio: user.professional_bio || "", }); setPhotoFile(null); setPhotoPreview(resolveFileUrl(user.profile_picture)); setImageError(false); }, [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) return; setPhotoFile(file); setImageError(false); const reader = new FileReader(); reader.onloadend = () => setPhotoPreview(reader.result as string); reader.readAsDataURL(file); }; const handleCancelPhoto = () => { setPhotoFile(null); setPhotoPreview(resolveFileUrl(user?.profile_picture)); setImageError(false); if (fileInputRef.current) fileInputRef.current.value = ""; }; const validateForm = () => { const errors = { fullName: "", email: "" }; if (!formData.fullName.trim()) errors.fullName = "Full name is required."; if (!formData.email.trim()) errors.email = "Email address is required."; setFormErrors(errors); return !errors.fullName && !errors.email; }; const handleSaveChanges = async () => { setConfirmOpen(false); if (!validateForm()) return; const emailChanged = formData.email.trim().toLowerCase() !== (user?.email ?? "").trim().toLowerCase(); try { await customToast.promise( save({ full_name: formData.fullName, email: formData.email, professional_bio: formData.bio, profile_picture: !isIntern && photoFile ? photoFile : undefined, }), { loading: "Menyimpan perubahan...", success: (updated) => ({ title: "Perubahan berhasil disimpan!", description: emailChanged ? "Email berubah, silakan login ulang..." : updated.message }), error: (err) => ({ title: "Gagal menyimpan perubahan!", description: err.message ?? "", }), } ); // email berubah -> sesi lama tidak valid lagi, paksa login ulang if (emailChanged) { document.cookie = "token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"; setTimeout(() => { window.location.href = "/login-internify"; }, 1200); return; } setPhotoFile(null); if (fileInputRef.current) fileInputRef.current.value = ""; } catch { } }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); setConfirmOpen(true); }; if (userLoading) { return (
{[...Array(3)].map((_, i) => (
))}
); } 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 */}
{photoPreview && !imageError ? ( Profile setImageError(true)} /> ) : (

{getInitials(user?.full_name) || "U"}

)}
{!isIntern && ( )}
{!isIntern ? (

Profile Photo

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

{photoFile && (

{photoFile.name}

)}
) : (

Nama

{user?.full_name || formData.fullName || "User"}

{user.kelompok_peminatan}

)} {/* Hidden file input */}

{/* Grid Inputs */}
{formErrors.fullName && (

{formErrors.fullName}

)} {hasPendingChanges && (

Nama hanya bisa diubah satu kali dan sudah pernah diubah.

)}
{formErrors.email && (

{formErrors.email}

)}

Mengubah email akan mengakhiri sesi — kamu harus login ulang.

{/* Professional Bio */}