forked from CSI-KJSCE/appointment_to_examiner
AdminCoursePage added..... admin routes protection added
This commit is contained in:
@@ -3,6 +3,7 @@ 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 = () => {
|
||||
@@ -10,6 +11,7 @@ export const AdminFacultyPage = () => {
|
||||
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: '',
|
||||
@@ -19,13 +21,51 @@ export const AdminFacultyPage = () => {
|
||||
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);
|
||||
@@ -125,6 +165,10 @@ export const AdminFacultyPage = () => {
|
||||
faculty.name.toLowerCase().includes(searchQuery.toLowerCase())
|
||||
);
|
||||
|
||||
if (isLoading) {
|
||||
return <p style={{ textAlign: 'center', marginTop: '50px', fontSize: '18px' }}>Loading...</p>;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Navbar />
|
||||
@@ -211,7 +255,7 @@ export const AdminFacultyPage = () => {
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
{currentFaculty.courses.map((course, index) => (
|
||||
<div key={index} style={courseContainerStyle}>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user