Wire live Spring Boot API fetch to Faculty page with local fallback support

This commit is contained in:
Shanmuga Krishnan S M
2026-08-19 09:06:18 +05:30
parent f937efce5c
commit 82c6444c28

View File

@@ -5,7 +5,7 @@ import FacultyCard from '@/components/FacultyCard/FacultyCard';
import { StaggerContainer, StaggerItem } from '@/components/AnimatedContainer/AnimatedContainer'; import { StaggerContainer, StaggerItem } from '@/components/AnimatedContainer/AnimatedContainer';
import { FACULTY_DATA, DEPARTMENTS } from '@/constants'; import { FACULTY_DATA, DEPARTMENTS } from '@/constants';
import { Link, useSearchParams } from 'react-router-dom'; import { Link, useSearchParams } from 'react-router-dom';
import { getStoredImsSession } from '@/services/imsService'; import { getBackendUrl } from '@/config/api';
const DEPT_MAP: Record<string, { abrv: string; full: string }> = { const DEPT_MAP: Record<string, { abrv: string; full: string }> = {
'Computer Science & Engineering': { abrv: 'CSE', full: 'Computer Science & Engineering' }, 'Computer Science & Engineering': { abrv: 'CSE', full: 'Computer Science & Engineering' },
@@ -75,7 +75,7 @@ export default function Faculty() {
searchParams.has('dept') searchParams.has('dept')
); );
const activeFacultyList = useMemo(() => { const [facultyList, setFacultyList] = useState<any[]>(() => {
try { try {
const saved = localStorage.getItem('RIT_LOCAL_FACULTY'); const saved = localStorage.getItem('RIT_LOCAL_FACULTY');
if (saved) { if (saved) {
@@ -84,10 +84,25 @@ export default function Faculty() {
} }
} catch {} } catch {}
return FACULTY_DATA; return FACULTY_DATA;
});
useEffect(() => {
fetch(getBackendUrl('/api/faculty'))
.then((res) => {
if (!res.ok) throw new Error();
return res.json();
})
.then((data) => {
if (Array.isArray(data) && data.length > 0) {
setFacultyList(data);
localStorage.setItem('RIT_LOCAL_FACULTY', JSON.stringify(data));
}
})
.catch(() => {});
}, []); }, []);
const filteredAndSortedFaculty = useMemo(() => { const filteredAndSortedFaculty = useMemo(() => {
const filtered = activeFacultyList.filter((f) => { const filtered = facultyList.filter((f) => {
const searchLower = searchFaculty.trim().toLowerCase(); const searchLower = searchFaculty.trim().toLowerCase();
const matchSearch = const matchSearch =
(f.name && f.name.toLowerCase().includes(searchLower)) || (f.name && f.name.toLowerCase().includes(searchLower)) ||
@@ -106,7 +121,7 @@ export default function Faculty() {
if (sortBy === 'Designation') return (a.designation || '').localeCompare(b.designation || ''); if (sortBy === 'Designation') return (a.designation || '').localeCompare(b.designation || '');
return 0; return 0;
}); });
}, [searchFaculty, selectedDept, sortBy]); }, [facultyList, searchFaculty, selectedDept, sortBy]);
const groupedFaculty = useMemo(() => { const groupedFaculty = useMemo(() => {
const groups: Record<string, typeof filteredAndSortedFaculty> = {}; const groups: Record<string, typeof filteredAndSortedFaculty> = {};