'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import { useToast } from '@/components/ui/use-toast'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Skeleton } from '@/components/ui/skeleton'; import { User } from 'lucide-react'; import { useTheme } from 'next-themes'; interface MahasiswaProfile { nim: string; nama: string; jk: 'Pria' | 'Wanita'; agama: string; kabupaten: string; provinsi: string; jenis_pendaftaran: string; status_beasiswa: 'YA' | 'TIDAK'; tahun_angkatan: string; ipk: number | null; prestasi: 'YA' | 'TIDAK'; status_kuliah: string; } export default function ProfilePage() { const [profile, setProfile] = useState(null); const [loading, setLoading] = useState(true); const { toast } = useToast(); const router = useRouter(); const { theme } = useTheme(); useEffect(() => { const fetchProfile = async () => { try { console.log('Fetching profile data...'); const response = await fetch('/api/mahasiswa/profile'); console.log('Profile response status:', response.status); if (response.status === 401) { toast({ variant: "destructive", title: "Akses Ditolak", description: "Silakan login terlebih dahulu untuk mengakses halaman ini.", }); router.push('/'); return; } if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.error || 'Failed to fetch profile data'); } const data = await response.json(); console.log('Profile data received:', data); setProfile(data); } catch (error) { console.error('Error fetching profile:', error); toast({ variant: "destructive", title: "Error", description: error instanceof Error ? error.message : "Gagal memuat data profil. Silakan coba lagi nanti.", }); } finally { setLoading(false); } }; fetchProfile(); }, [toast, router]); if (loading) { return (
Profil Mahasiswa
{[...Array(8)].map((_, i) => (
))}
); } if (!profile) { return (
Profil Mahasiswa
Data profil tidak tersedia
); } // Format IPK value const formatIPK = (ipk: number | null): string => { if (ipk === null || ipk === undefined) return '-'; return Number(ipk).toFixed(2); }; return (
{profile.nama}

{profile.nim}

Jenis Kelamin
{profile.jk}
Agama
{profile.agama}
Kabupaten
{profile.kabupaten}
Provinsi
{profile.provinsi}
Jenis Pendaftaran
{profile.jenis_pendaftaran}
Tahun Angkatan
{profile.tahun_angkatan}
IPK
{formatIPK(profile.ipk)}
Status Kuliah
{profile.status_kuliah || '-'}
); }