Change Audiens

This commit is contained in:
Randa Firman Putra
2025-06-28 06:15:50 +07:00
parent baf9965d64
commit 37d083ec31
18 changed files with 906 additions and 318 deletions

View File

@@ -1,217 +1,374 @@
'use client';
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Users, GraduationCap, Trophy, BookOpen } from "lucide-react";
import { useEffect, useState } from "react";
import { useTheme } from "next-themes";
import StatusMahasiswaChart from "@/components/StatusMahasiswaChart";
import StatistikMahasiswaChart from "@/components/StatistikMahasiswaChart";
import JenisPendaftaranChart from "@/components/JenisPendaftaranChart";
import AsalDaerahChart from "@/components/AsalDaerahChart";
import IPKChart from '@/components/IPKChart';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Loader2, Eye, EyeOff, LogIn, UserPlus } from "lucide-react";
import { ThemeProvider } from '@/components/theme-provider';
import { Toaster } from '@/components/ui/toaster';
interface MahasiswaTotal {
total_mahasiswa: number;
mahasiswa_aktif: number;
total_lulus: number;
pria_lulus: number;
wanita_lulus: number;
total_berprestasi: number;
prestasi_akademik: number;
prestasi_non_akademik: number;
ipk_rata_rata_aktif: number;
ipk_rata_rata_lulus: number;
total_mahasiswa_aktif_lulus: number;
}
// Skeleton loading component
const CardSkeleton = () => (
<Card className="bg-white dark:bg-slate-900 shadow-lg">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<div className="h-4 w-24 bg-gray-200 dark:bg-gray-700 rounded animate-pulse"></div>
<div className="h-4 w-4 bg-gray-200 dark:bg-gray-700 rounded-full animate-pulse"></div>
</CardHeader>
<CardContent>
<div className="h-8 w-16 bg-gray-200 dark:bg-gray-700 rounded animate-pulse mb-2"></div>
<div className="flex justify-between">
<div className="h-3 w-20 bg-gray-200 dark:bg-gray-700 rounded animate-pulse"></div>
<div className="h-3 w-20 bg-gray-200 dark:bg-gray-700 rounded animate-pulse"></div>
</div>
</CardContent>
</Card>
);
export default function HomePage() {
const { theme } = useTheme();
const [mahasiswaData, setMahasiswaData] = useState<MahasiswaTotal>({
total_mahasiswa: 0,
mahasiswa_aktif: 0,
total_lulus: 0,
pria_lulus: 0,
wanita_lulus: 0,
total_berprestasi: 0,
prestasi_akademik: 0,
prestasi_non_akademik: 0,
ipk_rata_rata_aktif: 0,
ipk_rata_rata_lulus: 0,
total_mahasiswa_aktif_lulus: 0
export default function LandingPage() {
const router = useRouter();
const [isLoginOpen, setIsLoginOpen] = useState(false);
const [isRegisterOpen, setIsRegisterOpen] = useState(false);
const [activeTab, setActiveTab] = useState('admin');
const [loading, setLoading] = useState(false);
const [showPassword, setShowPassword] = useState(false);
const [error, setError] = useState('');
// Admin form state
const [adminForm, setAdminForm] = useState({
username: '',
password: ''
});
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
// Dosen form state
const [dosenForm, setDosenForm] = useState({
nip: '',
password: ''
});
useEffect(() => {
const fetchData = async () => {
try {
// Menggunakan cache API untuk mempercepat loading
const cacheKey = 'mahasiswa-total-data';
const cachedData = sessionStorage.getItem(cacheKey);
const cachedTimestamp = sessionStorage.getItem(`${cacheKey}-timestamp`);
// Cek apakah data cache masih valid (kurang dari 60 detik)
const isCacheValid = cachedTimestamp &&
(Date.now() - parseInt(cachedTimestamp)) < 60000;
if (cachedData && isCacheValid) {
setMahasiswaData(JSON.parse(cachedData));
}
// Fetch data total mahasiswa
const totalResponse = await fetch('/api/mahasiswa/total', {
cache: 'no-store',
});
if (!totalResponse.ok) {
throw new Error('Failed to fetch total data');
}
const totalData = await totalResponse.json();
setMahasiswaData(totalData);
// Menyimpan data dan timestamp ke sessionStorage
sessionStorage.setItem(cacheKey, JSON.stringify(totalData));
sessionStorage.setItem(`${cacheKey}-timestamp`, Date.now().toString());
// Register form state
const [registerForm, setRegisterForm] = useState({
nip: '',
password: '',
confirmPassword: ''
});
const handleAdminLogin = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError('');
try {
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: adminForm.username,
password: adminForm.password,
role: 'admin'
}),
});
const data = await response.json();
if (response.ok) {
setIsLoginOpen(false);
router.push('/dashboard');
} else {
setError(data.error || 'Login gagal');
}
} catch (err) {
setError('Terjadi kesalahan saat login');
} finally {
setLoading(false);
}
};
const handleDosenLogin = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError('');
try {
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
nip: dosenForm.nip,
password: dosenForm.password,
role: 'dosen'
}),
});
const data = await response.json();
if (response.ok) {
setIsLoginOpen(false);
router.push('/dashboard');
} else {
setError(data.error || 'Login gagal');
}
} catch (err) {
setError('Terjadi kesalahan saat login');
} finally {
setLoading(false);
}
};
const handleRegister = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError('');
if (registerForm.password !== registerForm.confirmPassword) {
setError('Password dan konfirmasi password tidak cocok');
setLoading(false);
return;
}
try {
const response = await fetch('/api/auth/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
nip: registerForm.nip,
password: registerForm.password
}),
});
const data = await response.json();
if (response.ok) {
setIsRegisterOpen(false);
setIsLoginOpen(true);
setActiveTab('dosen');
setError('');
} else {
setError(data.error || 'Registrasi gagal');
}
} catch (err) {
setError(err instanceof Error ? err.message : 'An error occurred');
console.error('Error fetching data:', err);
setError('Terjadi kesalahan saat registrasi');
} finally {
setLoading(false);
}
};
fetchData();
}, []);
return (
<div className="container mx-auto p-4 space-y-6">
<h1 className="text-3xl font-bold mb-8">Dashboard Portal Data Informatika</h1>
{loading ? (
<div className="grid gap-6 md:grid-cols-2 lg:grid-cols-4">
<CardSkeleton />
<CardSkeleton />
<CardSkeleton />
<CardSkeleton />
<ThemeProvider
attribute="class"
defaultTheme="system"
enableSystem
disableTransitionOnChange
>
<div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 dark:from-gray-900 dark:to-gray-800">
<div className="container mx-auto px-4 py-16">
<div className="text-center mb-12">
<h1 className="text-4xl md:text-6xl font-bold text-gray-900 dark:text-white mb-4">
Portal Data Informatika
</h1>
<p className="text-xl text-gray-600 dark:text-gray-300 mb-8">
Visualisasi Data Akademik Mahasiswa Jurusan Informatika
</p>
<div className="flex flex-col sm:flex-row gap-4 justify-center">
<Dialog open={isLoginOpen} onOpenChange={setIsLoginOpen}>
<DialogTrigger asChild>
<Button size="lg" className="flex items-center gap-2">
<LogIn className="h-5 w-5" />
Login
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Login Portal Data Informatika</DialogTitle>
<DialogDescription>
Silakan login sesuai dengan role Anda
</DialogDescription>
</DialogHeader>
<Tabs value={activeTab} onValueChange={setActiveTab} className="w-full">
<TabsList className="grid w-full grid-cols-2">
<TabsTrigger value="admin">Admin</TabsTrigger>
<TabsTrigger value="dosen">Dosen</TabsTrigger>
</TabsList>
<TabsContent value="admin" className="space-y-4">
<form onSubmit={handleAdminLogin} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="username">Username</Label>
<Input
id="username"
type="text"
placeholder="Masukkan username"
value={adminForm.username}
onChange={(e) => setAdminForm({ ...adminForm, username: e.target.value })}
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="admin-password">Password</Label>
<div className="relative">
<Input
id="admin-password"
type={showPassword ? "text" : "password"}
placeholder="Masukkan password"
value={adminForm.password}
onChange={(e) => setAdminForm({ ...adminForm, password: e.target.value })}
required
/>
<Button
type="button"
variant="ghost"
size="sm"
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() => setShowPassword(!showPassword)}
>
{showPassword ? (
<EyeOff className="h-4 w-4" />
) : (
<Eye className="h-4 w-4" />
)}
</Button>
</div>
</div>
<Button type="submit" className="w-full" disabled={loading}>
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Login sebagai Admin
</Button>
</form>
</TabsContent>
<TabsContent value="dosen" className="space-y-4">
<form onSubmit={handleDosenLogin} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="nip">NIP</Label>
<Input
id="nip"
type="text"
placeholder="Masukkan NIP"
value={dosenForm.nip}
onChange={(e) => setDosenForm({ ...dosenForm, nip: e.target.value })}
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="dosen-password">Password</Label>
<div className="relative">
<Input
id="dosen-password"
type={showPassword ? "text" : "password"}
placeholder="Masukkan password"
value={dosenForm.password}
onChange={(e) => setDosenForm({ ...dosenForm, password: e.target.value })}
required
/>
<Button
type="button"
variant="ghost"
size="sm"
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() => setShowPassword(!showPassword)}
>
{showPassword ? (
<EyeOff className="h-4 w-4" />
) : (
<Eye className="h-4 w-4" />
)}
</Button>
</div>
</div>
<Button type="submit" className="w-full" disabled={loading}>
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Login sebagai Dosen
</Button>
</form>
</TabsContent>
</Tabs>
{error && (
<Alert className="mt-4">
<AlertDescription className="text-red-600">
{error}
</AlertDescription>
</Alert>
)}
</DialogContent>
</Dialog>
<Dialog open={isRegisterOpen} onOpenChange={setIsRegisterOpen}>
<DialogTrigger asChild>
<Button variant="outline" size="lg" className="flex items-center gap-2">
<UserPlus className="h-5 w-5" />
Register
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Registrasi Dosen</DialogTitle>
<DialogDescription>
Daftar akun baru untuk dosen Portal Data Informatika
</DialogDescription>
</DialogHeader>
<form onSubmit={handleRegister} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="register-nip">NIP</Label>
<Input
id="register-nip"
type="text"
placeholder="Masukkan NIP"
value={registerForm.nip}
onChange={(e) => setRegisterForm({ ...registerForm, nip: e.target.value })}
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="register-password">Password</Label>
<div className="relative">
<Input
id="register-password"
type={showPassword ? "text" : "password"}
placeholder="Masukkan password (min. 6 karakter)"
value={registerForm.password}
onChange={(e) => setRegisterForm({ ...registerForm, password: e.target.value })}
required
/>
<Button
type="button"
variant="ghost"
size="sm"
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() => setShowPassword(!showPassword)}
>
{showPassword ? (
<EyeOff className="h-4 w-4" />
) : (
<Eye className="h-4 w-4" />
)}
</Button>
</div>
</div>
) : error ? (
<div className="bg-red-50 border-l-4 border-red-500 p-4 mb-4">
<div className="flex">
<div className="flex-shrink-0">
<svg className="h-5 w-5 text-red-500" viewBox="0 0 20 20" fill="currentColor">
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
</svg>
<div className="space-y-2">
<Label htmlFor="confirm-password">Konfirmasi Password</Label>
<Input
id="confirm-password"
type="password"
placeholder="Konfirmasi password"
value={registerForm.confirmPassword}
onChange={(e) => setRegisterForm({ ...registerForm, confirmPassword: e.target.value })}
required
/>
</div>
<div className="ml-3">
<p className="text-sm text-red-700">{error}</p>
<Button type="submit" className="w-full" disabled={loading}>
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Daftar sebagai Dosen
</Button>
</form>
{error && (
<Alert className="mt-4">
<AlertDescription className="text-red-600">
{error}
</AlertDescription>
</Alert>
)}
</DialogContent>
</Dialog>
</div>
</div>
</div>
) : (
<>
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4 mb-8">
{/* Kartu Total Mahasiswa */}
<Card className="bg-white dark:bg-slate-900 shadow-lg">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium dark:text-white">
Total Mahasiswa
</CardTitle>
<Users className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold dark:text-white">{mahasiswaData.total_mahasiswa}</div>
<div className="flex justify-between mt-2 text-xs text-muted-foreground">
<span className="dark:text-white">Aktif: <span className="text-green-500">{mahasiswaData.mahasiswa_aktif}</span></span>
</div>
</CardContent>
</Card>
{/* Kartu Total Kelulusan */}
<Card className="bg-white dark:bg-slate-900 shadow-lg">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium dark:text-white">
Total Kelulusan
</CardTitle>
<GraduationCap className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold dark:text-white">{mahasiswaData.total_lulus}</div>
<div className="flex justify-between mt-2 text-xs text-muted-foreground">
<span className="dark:text-white">Laki-laki: <span className="text-blue-500">{mahasiswaData.pria_lulus}</span></span>
<span className="dark:text-white">Perempuan: <span className="text-pink-500">{mahasiswaData.wanita_lulus}</span></span>
</div>
</CardContent>
</Card>
{/* Kartu Total Prestasi */}
<Card className="bg-white dark:bg-slate-900 shadow-lg">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium dark:text-white">
Mahasiswa Berprestasi
</CardTitle>
<Trophy className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold dark:text-white">{mahasiswaData.total_berprestasi}</div>
<div className="flex justify-between mt-2 text-xs text-muted-foreground">
<span className="dark:text-white">Akademik: <span className="text-yellow-500">{mahasiswaData.prestasi_akademik}</span></span>
<span className="dark:text-white">Non-Akademik: <span className="text-purple-500">{mahasiswaData.prestasi_non_akademik}</span></span>
</div>
</CardContent>
</Card>
{/* Kartu Rata-rata IPK */}
<Card className="bg-white dark:bg-slate-900 shadow-lg">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium dark:text-white">
Rata-rata IPK
</CardTitle>
<BookOpen className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold dark:text-white">{mahasiswaData.mahasiswa_aktif}</div>
<div className="flex justify-between mt-2 text-xs text-muted-foreground">
<span className="dark:text-white">Aktif: <span className="text-green-500">{mahasiswaData.ipk_rata_rata_aktif}</span></span>
</div>
</CardContent>
</Card>
</div>
{/* Diagram Statistik Mahasiswa */}
<StatistikMahasiswaChart />
{/* Diagram Status Mahasiswa */}
<StatusMahasiswaChart />
{/* Diagram Jenis Pendaftaran */}
<JenisPendaftaranChart />
{/* Diagram Asal Daerah */}
<AsalDaerahChart />
{/* Diagram IPK */}
<IPKChart />
</>
)}
</div>
<Toaster />
</ThemeProvider>
);
}