import React, { useState, useEffect } from 'react'; import axios from 'axios'; import Navbar from './Navbar'; import { toast, ToastContainer } from "react-toastify"; import 'react-toastify/dist/ReactToastify.css'; import { useNavigate } from 'react-router-dom'; export const AdminFacultyPage = () => { const [faculties, setFaculties] = useState([]); const [showForm, setShowForm] = useState(false); const [isEditMode, setIsEditMode] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const [isLoading, setIsLoading] = useState(true); const [currentFaculty, setCurrentFaculty] = useState({ facultyId: '', name: '', email: '', department: '', program: '', courses: [''], }); const [courses, setcourses] = useState({}); const navigate = useNavigate(); // Fetch all faculties useEffect(() => { const checkAdmin = async () => { try { const res = await axios.get("http://localhost:8080/api/me", { withCredentials: true }); console.log(res.data); if (!res.data.isAdmin) { handleUnauthorizedAccess(); } else { setIsLoading(false); } } catch (error) { console.error("Unauthorized access:", error); handleUnauthorizedAccess(); } }; fetchFaculties(); fetchCourses(); checkAdmin(); }, []); const handleUnauthorizedAccess = async () => { try { toast.warning("Unauthorized access. Logging out...", { position: "top-center" }); // Attempt to log out await axios.get("http://localhost:8080/auth/logout", { withCredentials: true }); // Delay redirection to show the toast message setTimeout(() => { navigate("/"); // Redirect to login }, 1500); } catch (error) { console.error("Error during unauthorized access handling:", error); } }; useEffect(() => { if (!isLoading) { fetchCourses(); } }, [isLoading]); const fetchFaculties = async () => { const res = await axios.get('http://localhost:8080/api/faculty'); setFaculties(res.data); }; const fetchCourses = async () => { try { const res = await axios.get("http://localhost:8080/api/courses"); const courseMap = res.data.reduce((acc, course) => { acc[course.courseId] = course.name; return acc; }, {}); setcourses(courseMap); } catch (error) { console.error("Error fetching courses: ", error); } }; // Add or Update Faculty const handleAddOrUpdateFaculty = async (e) => { e.preventDefault(); if (isEditMode) { // Update Faculty (PUT request) await axios.put(`http://localhost:8080/api/faculty/${currentFaculty._id}`, currentFaculty); toast.success('Faculty updated successfully!', { position: 'top-center' }); } else { // Add Faculty (POST request) await axios.post('http://localhost:8080/api/faculty', currentFaculty); toast.success('Faculty added successfully!', { position: 'top-center' }); } // Reset form and refresh data resetForm(); fetchFaculties(); }; // Delete Faculty const handleDeleteFaculty = async (id) => { const confirm = window.confirm('⚠️ Are you sure you want to delete this faculty?'); if (confirm) { await axios.delete(`http://localhost:8080/api/faculty/${id}`); fetchFaculties(); toast.success('Faculty deleted successfully!', { position: 'top-center' }); } }; // Open Form for Edit const handleOpenEditForm = (faculty) => { setCurrentFaculty(faculty); setIsEditMode(true); setShowForm(true); }; // Open Form for Add const handleOpenAddForm = () => { resetForm(); setIsEditMode(false); setShowForm(true); }; // Handle Course Input Change const handleCourseChange = (index, value) => { const newCourses = [...currentFaculty.courses]; newCourses[index] = value; setCurrentFaculty({ ...currentFaculty, courses: newCourses }); }; // Add a new course field const handleAddCourse = () => { setCurrentFaculty({ ...currentFaculty, courses: [...currentFaculty.courses, ''], }); }; // Remove a course field const handleRemoveCourse = (index) => { const newCourses = currentFaculty.courses.filter((_, i) => i !== index); setCurrentFaculty({ ...currentFaculty, courses: newCourses }); }; // Reset form const resetForm = () => { setCurrentFaculty({ facultyId: '', name: '', email: '', department: '', program: '', courses: [''], }); setShowForm(false); }; const filteredFaculties = faculties.filter((faculty) => faculty.name.toLowerCase().includes(searchQuery.toLowerCase()) ); if (isLoading) { return
Loading...
; } return ( <>| Faculty ID | Name | Department | Program | Courses | Actions | |
|---|---|---|---|---|---|---|
| {f.facultyId} | {f.name} | {f.email} | {f.department} | {f.program} |
|
|
| No faculty found | ||||||