import { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import SidebarAdmin from "../Layout/SidebarAdmin"; import NavbarAdmin from "../Layout/NavbarAdmin"; import axios from "axios"; type Application = { id: number; nama_depan: string; posisi: string; status: string; }; const InternshipList = () => { const [applications, setApplications] = useState([]); const [currentPage, setCurrentPage] = useState(1); const [searchTerm, setSearchTerm] = useState(""); const itemsPerPage = 10; const navigate = useNavigate(); useEffect(() => { const fetchApplications = () => { const token = document.cookie .split("; ") .find((row) => row.startsWith("token=")) ?.split("=")[1]; axios .get(`${import.meta.env.VITE_API_BASE_URL}/lamaran-magang-api/get`, { headers: { Authorization: `Bearer ${token}`, }, }) .then((res) => { setApplications(res.data.data || []); }) .catch((err) => { console.error("Gagal mengambil data lamaran:", err); }); }; fetchApplications(); const interval = setInterval(fetchApplications, 10000); return () => clearInterval(interval); }, []); const getStatusStyle = (status: string) => { switch (status) { case "diterima": return "bg-green-500 text-white"; case "ditolak": return "bg-red-500 text-white"; case "diproses": default: return "bg-yellow-400 text-black"; } }; // Pagination Logic const indexOfLastItem = currentPage * itemsPerPage; const indexOfFirstItem = indexOfLastItem - itemsPerPage; const filteredApplications = applications.filter( (app) => app.nama_depan.toLowerCase().includes(searchTerm.toLowerCase()) || app.posisi.toLowerCase().includes(searchTerm.toLowerCase()) ); const currentItems = filteredApplications.slice( indexOfFirstItem, indexOfLastItem ); const totalPages = Math.ceil(filteredApplications.length / itemsPerPage); const handleExportExcel = async (): Promise => { const token = document.cookie .split("; ") .find((row) => row.startsWith("token=")) ?.split("=")[1]; try { const response = await axios.get( `${import.meta.env.VITE_API_BASE_URL}/lamaran-magang-api/export`, { responseType: "blob", headers: { Authorization: `Bearer ${token}`, }, } ); const blob = new Blob([response.data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", }); const url = window.URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.setAttribute("download", "data.xlsx"); document.body.appendChild(link); link.click(); link.remove(); window.URL.revokeObjectURL(url); return true; } catch (error) { console.error("Gagal mengekspor data:", error); alert("Gagal mengekspor data. Silakan coba lagi."); return false; } }; const handleUpdateBatch = async () => { const exportSuccess = await handleExportExcel(); if (!exportSuccess) { return; } const confirmDelete = window.confirm( "Apakah Anda yakin ingin menghapus seluruh data pelamar setelah melakukan ekspor?" ); if (!confirmDelete) return; const token = document.cookie .split("; ") .find((row) => row.startsWith("token=")) ?.split("=")[1]; try { await axios.delete( `${import.meta.env.VITE_API_BASE_URL}/lamaran-magang-api/delete`, { headers: { Authorization: `Bearer ${token}`, }, } ); setApplications([]); alert("Data pelamar berhasil dihapus setelah diekspor."); } catch (error) { console.error("Gagal menghapus data pelamar:", error); alert("Gagal menghapus data pelamar. Silakan coba lagi."); } }; return (

Daftar Pelamar

setSearchTerm(e.target.value)} className="px-4 py-2 rounded-2xl w-[20rem] border border-slate-400" />
{currentItems.map((app, idx) => ( ))}
Nama Posisi Status Aksi
{app.nama_depan} {app.posisi} {app.status.charAt(0).toUpperCase() + app.status.slice(1)}
{applications.length === 0 && (

Tidak ada data.

)} {/* Pagination Controls */}
{Array.from({ length: totalPages }, (_, i) => ( ))}
); }; export default InternshipList;