AdminFacultyPage- added dropdown for course input, display course name instead of ID,

This commit is contained in:
Harshitha Shetty
2025-03-14 20:52:58 +05:30
parent 7304efa069
commit 19b94b1a9f

View File

@@ -18,10 +18,12 @@ export const AdminFacultyPage = () => {
program: '', program: '',
courses: [''], courses: [''],
}); });
const [courses, setcourses] = useState({});
// Fetch all faculties // Fetch all faculties
useEffect(() => { useEffect(() => {
fetchFaculties(); fetchFaculties();
fetchCourses();
}, []); }, []);
const fetchFaculties = async () => { const fetchFaculties = async () => {
@@ -29,6 +31,19 @@ export const AdminFacultyPage = () => {
setFaculties(res.data); 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 // Add or Update Faculty
const handleAddOrUpdateFaculty = async (e) => { const handleAddOrUpdateFaculty = async (e) => {
e.preventDefault(); e.preventDefault();
@@ -180,7 +195,6 @@ export const AdminFacultyPage = () => {
style={inputStyle} style={inputStyle}
/> />
<input <input
placeholder="Department" placeholder="Department"
value={currentFaculty.department} value={currentFaculty.department}
onChange={(e) => setCurrentFaculty({ ...currentFaculty, department: e.target.value })} onChange={(e) => setCurrentFaculty({ ...currentFaculty, department: e.target.value })}
@@ -197,16 +211,23 @@ export const AdminFacultyPage = () => {
/> />
</div> </div>
<h4 style={subHeadingStyle}>Courses:</h4>
{currentFaculty.courses.map((course, index) => ( {currentFaculty.courses.map((course, index) => (
<div key={index} style={courseContainerStyle}> <div key={index} style={courseContainerStyle}>
<input
placeholder="Course Name" <select
value={course} value={course}
onChange={(e) => handleCourseChange(index, e.target.value)} onChange={(e) => handleCourseChange(index, e.target.value)}
style={{ ...inputStyle, flex: 1 }} style={{ ...inputStyle, flex: 1 }}
/> >
<option value="">Select Course</option>
{Object.entries(courses).map(([courseId, courseName]) => (
<option key={courseId} value={courseId}>
{courseName}
</option>
))}
</select>
<button type="button" onClick={() => handleRemoveCourse(index)} style={removeButtonStyle}></button> <button type="button" onClick={() => handleRemoveCourse(index)} style={removeButtonStyle}></button>
</div> </div>
))} ))}
@@ -239,13 +260,23 @@ export const AdminFacultyPage = () => {
{filteredFaculties.length > 0 ? ( {filteredFaculties.length > 0 ? (
filteredFaculties.map((f) => ( filteredFaculties.map((f) => (
<tr key={f._id}> <tr key={f._id}>
<td>{f.facultyId}</td> <td style={tdStyle}>{f.facultyId}</td>
<td>{f.name}</td> <td style={{
<td>{f.email}</td> maxWidth: '150px',
<td>{f.department}</td> wordWrap: 'break-word',
<td>{f.program}</td> whiteSpace: 'normal'
<td>{f.courses.join(', ')}</td> }}>{f.name}</td>
<td> <td style={tdStyle}>{f.email}</td>
<td style={tdStyle}>{f.department}</td>
<td style={tdStyle}>{f.program}</td>
<td style={tdStyle}><ul style={{ padding: 0, margin: 0, listStyleType: 'none' }}>
{f.courses.map((courseId, index) => (
<li key={index} style={{ marginBottom: '5px' }}>
{courses[courseId] || courseId}
</li>
))}
</ul></td>
<td style={tdStyle}>
<button style={{ <button style={{
backgroundColor: '#4CAF50', backgroundColor: '#4CAF50',
color: 'white', color: 'white',
@@ -283,18 +314,23 @@ export const AdminFacultyPage = () => {
</tr> </tr>
)} )}
</tbody> </tbody>
</table> </table >
</> </>
); );
}; };
const tableStyle = { const tableStyle = {
width: '100%', width: '100%',
borderCollapse: 'collapse', borderCollapse: 'collapse',
marginTop: '20px', marginTop: '20px',
}; };
const tdStyle = {
maxWidth: '350px',
wordWrap: 'break-word',
whiteSpace: 'normal'
}
const formStyle = { const formStyle = {
backgroundColor: '#f9f9f9', backgroundColor: '#f9f9f9',
padding: '20px', padding: '20px',