From ce73a591c5f0dcd9e2fe7cbeb3a846e553464fe8 Mon Sep 17 00:00:00 2001 From: Harshitha Shetty <141444342+HarshithaShetty27@users.noreply.github.com> Date: Mon, 9 Dec 2024 07:26:57 +0530 Subject: [PATCH] CourseForm backend done --- client/src/Pages/CourseForm.jsx | 245 +++++++++++++++++++++++-- client/src/Pages/CourseTable.jsx | 142 +++++++++----- client/src/Pages/Welcom.jsx | 4 +- client/src/Pages/WelcomeWithFilter.jsx | 4 +- client/src/api.js | 27 +++ server/controller/facultyController.js | 13 ++ server/routes/facultyRoutes.js | 8 +- server/routes/optionsRoutes.js | 37 ++++ server/server.js | 4 + 9 files changed, 408 insertions(+), 76 deletions(-) create mode 100644 server/controller/facultyController.js create mode 100644 server/routes/optionsRoutes.js diff --git a/client/src/Pages/CourseForm.jsx b/client/src/Pages/CourseForm.jsx index 8ace412..9236809 100644 --- a/client/src/Pages/CourseForm.jsx +++ b/client/src/Pages/CourseForm.jsx @@ -1,11 +1,200 @@ +// import React, { useState, useEffect } from "react"; +// import { useLocation, useParams, useNavigate } from "react-router-dom"; +// import "./CourseForm.css"; + +// const CourseForm = () => { +// const { id } = useParams(); // Get the course ID from the URL params +// const location = useLocation(); +// const navigate = useNavigate(); // Updated for navigation +// const { course } = location.state || {}; + +// const [options, setOptions] = useState({ +// assessment: [], +// reassessment: [], +// paperSetting: [], +// moderation: [], +// pwdPaperSetter: [], +// oralsPracticals: [], // New field for Orals/Practicals +// }); + +// const [formData, setFormData] = useState({ +// assessment: "", +// reassessment: "", +// paperSetting: "", +// moderation: "", +// pwdPaperSetter: "", +// oralsPracticals: "", // New field for Orals/Practicals +// }); + +// const [errors, setErrors] = useState({}); // To track validation errors + +// // Fetch data for search bars +// useEffect(() => { +// const fetchOptions = async () => { +// try { +// const response = await fetch("/api/options"); // Replace with your API endpoint +// const data = await response.json(); +// setOptions(data); +// } catch (error) { +// console.error("Failed to fetch options:", error); +// } +// }; + +// fetchOptions(); +// }, []); + +// const handleInputChange = (e) => { +// const { name, value } = e.target; +// setFormData({ ...formData, [name]: value }); +// }; + +// const validateForm = () => { +// const newErrors = {}; +// Object.keys(formData).forEach((field) => { +// if (!formData[field]) { +// newErrors[field] = "This field is required"; +// } +// }); +// setErrors(newErrors); +// return Object.keys(newErrors).length === 0; +// }; + +// const handleSubmit = (e) => { +// e.preventDefault(); +// if (validateForm()) { +// console.log("Form submitted:", formData); + +// navigate("/courses", { state: { updatedCourse: { ...course, status: "Submitted" } } }); +// } +// }; + +// return ( +//
+//

Course Info

+//
+// +// +// +// +// +// +// +// +// +//
+//
+// ); +// }; + +// export default CourseForm; + +// CourseForm.jsx + import React, { useState, useEffect } from "react"; import { useLocation, useParams, useNavigate } from "react-router-dom"; +import { fetchFaculties } from "../api"; import "./CourseForm.css"; const CourseForm = () => { - const { id } = useParams(); // Get the course ID from the URL params + const { id } = useParams(); const location = useLocation(); - const navigate = useNavigate(); // Updated for navigation + const navigate = useNavigate(); const { course } = location.state || {}; const [options, setOptions] = useState({ @@ -14,7 +203,8 @@ const CourseForm = () => { paperSetting: [], moderation: [], pwdPaperSetter: [], - oralsPracticals: [], // New field for Orals/Practicals + oralsPracticals: [], + faculties: [], // New field for faculties }); const [formData, setFormData] = useState({ @@ -26,21 +216,23 @@ const CourseForm = () => { oralsPracticals: "", // New field for Orals/Practicals }); - const [errors, setErrors] = useState({}); // To track validation errors + const [errors, setErrors] = useState({}); - // Fetch data for search bars useEffect(() => { - const fetchOptions = async () => { + const fetchOptionsAndFaculties = async () => { try { - const response = await fetch("/api/options"); // Replace with your API endpoint - const data = await response.json(); - setOptions(data); + const facultiesData = await fetchFaculties(); // Fetch faculty names from the backend + console.log(facultiesData); + setOptions(prevOptions => ({ + ...prevOptions, + faculties: facultiesData, + })); } catch (error) { - console.error("Failed to fetch options:", error); + console.error("Failed to fetch faculties:", error); } }; - fetchOptions(); + fetchOptionsAndFaculties(); }, []); const handleInputChange = (e) => { @@ -59,14 +251,29 @@ const CourseForm = () => { return Object.keys(newErrors).length === 0; }; + // const handleSubmit = (e) => { + // e.preventDefault(); + // if (validateForm()) { + // console.log("Form submitted:", formData); + // navigate("/courses", { state: { updatedCourse: { ...course, status: "Submitted" } } }); + // } + // }; const handleSubmit = (e) => { e.preventDefault(); if (validateForm()) { console.log("Form submitted:", formData); - - navigate("/courses", { state: { updatedCourse: { ...course, status: "Submitted" } } }); + navigate("/courses", { + state: { + updatedCourse: { + ...course, + status: "Submitted", // Update status + ...formData, // Include form data if required + }, + }, + }); } }; + return (
@@ -91,7 +298,7 @@ const CourseForm = () => { /> {options.oralsPracticals.map((option, index) => ( - {errors.oralsPracticals && {errors.oralsPracticals}} @@ -107,7 +314,7 @@ const CourseForm = () => { /> {options.assessment.map((option, index) => ( - {errors.assessment && {errors.assessment}} @@ -123,7 +330,7 @@ const CourseForm = () => { /> {options.reassessment.map((option, index) => ( - {errors.reassessment && {errors.reassessment}} @@ -139,7 +346,7 @@ const CourseForm = () => { /> {options.paperSetting.map((option, index) => ( - {errors.paperSetting && {errors.paperSetting}} @@ -155,7 +362,7 @@ const CourseForm = () => { /> {options.moderation.map((option, index) => ( - {errors.moderation && {errors.moderation}} @@ -171,7 +378,7 @@ const CourseForm = () => { /> {options.pwdPaperSetter.map((option, index) => ( - {errors.pwdPaperSetter && {errors.pwdPaperSetter}} diff --git a/client/src/Pages/CourseTable.jsx b/client/src/Pages/CourseTable.jsx index 8bf9a44..7db3467 100644 --- a/client/src/Pages/CourseTable.jsx +++ b/client/src/Pages/CourseTable.jsx @@ -1,69 +1,105 @@ -// import React from "react"; -// import { useNavigate } from "react-router-dom"; +// import React, { useState, useEffect } from 'react'; +// import { useLocation, useNavigate } from "react-router-dom"; // import "./CourseTable.css"; +// import { fetchCourses } from '../api'; -// const CourseTable = ({courses}) => { -// const navigate = useNavigate(); +// const CourseTable = () => { +// const { state } = useLocation(); +// const courses = state?.courses; +// const navigate= useNavigate(); +// console.log(courses); + +// useEffect(() => { +// if (!courses || courses.length === 0) { +// alert("No courses available"); +// } +// }, [courses]); + +// if (!courses) { +// return
Loading...
; +// } // const handleRowClick = (course) => { -// navigate(`/course-form/${course.id}`, { state: { course } }); +// navigate(`/course-form/${course.courseId}`, { state: { course } }); // }; // return ( -//
-//

Courses

-// -// -// -// -// -// +//
Course IDCourse NameStatus
+// +// +// +// +// +// +// +// +// {courses.map(course => ( +// handleRowClick(course)}> +// +// +// // -// -// -// {courses.map((course) => ( -// handleRowClick(course)} -// style={{ cursor: "pointer" }} -// > -// -// -// -// -// ))} -// -//
CourseIDCourse NameStatus
{course.courseId}{course.name}{course.status}
{course.id}{course.name} -// -// {course.status} -// -//
-//
+// ))} +// +// // ); -// }; +// } // export default CourseTable; -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect } from "react"; import { useLocation, useNavigate } from "react-router-dom"; import "./CourseTable.css"; -import { fetchCourses } from '../api'; +import { fetchCourses } from "../api"; const CourseTable = () => { const { state } = useLocation(); - const courses = state?.courses; - console.log(courses); + const [courses, setCourses] = useState(state?.courses || []); + const [loading, setLoading] = useState(!state?.courses); + const navigate = useNavigate(); useEffect(() => { - if (!courses || courses.length === 0) { - alert("No courses available"); - } - }, [courses]); + const fetchAllCourses = async () => { + if (!state?.courses) { + try { + const fetchedCourses = await fetchCourses(); // Fetch courses from API + setCourses(fetchedCourses); + } catch (error) { + console.error("Failed to fetch courses:", error); + } finally { + setLoading(false); + } + } + }; - if (!courses) { + fetchAllCourses(); + }, [state?.courses]); + + useEffect(() => { + if (state?.updatedCourse) { + setCourses((prevCourses) => { + // Filter only the updated course + const filteredCourses = prevCourses.filter((course) => + course.courseId === state.updatedCourse.courseId + ); + + return filteredCourses.map((course) => + course.courseId === state.updatedCourse.courseId + ? { ...course, status: "Submitted" } // Update status + : course + ); + }); + } + }, [state?.updatedCourse]); + + if (loading) { return
Loading...
; } + const handleRowClick = (course) => { + navigate(`/course-form/${course.courseId}`, { state: { course } }); + }; + return ( @@ -74,16 +110,22 @@ const CourseTable = () => { - {courses.map(course => ( - - - - + {courses.length > 0 ? ( + courses.map((course) => ( + handleRowClick(course)}> + + + + + )) + ) : ( + + - ))} + )}
{course.courseId}{course.name}{course.status}
{course.courseId}{course.name}{course.status}
No courses available
); -} +}; export default CourseTable; diff --git a/client/src/Pages/Welcom.jsx b/client/src/Pages/Welcom.jsx index 1f8480f..d3cfacd 100644 --- a/client/src/Pages/Welcom.jsx +++ b/client/src/Pages/Welcom.jsx @@ -12,8 +12,8 @@ const Welcom = () => { return (
-

Welcome, John Doe

-

You're signed in as DEC/EIC

+

Welcome

+

You're signed in