From 9e1734e3950ce45778f7e7df4d42c6cc22eda422 Mon Sep 17 00:00:00 2001 From: Harikrishnan Gopal <118685394+hk151109@users.noreply.github.com> Date: Wed, 22 Jan 2025 11:22:42 +0530 Subject: [PATCH] filter --- client/src/App.js | 3 ++ client/src/Pages/ConsolidatedTable.jsx | 58 +++++++++++++++++++++ client/src/Pages/CourseForm.jsx | 5 ++ client/src/Pages/FilterPage.jsx | 26 ++++++++- server/controller/csvGeneratorController.js | 2 +- server/routes/consolidatedRoutes.js | 34 ++++++++++++ server/server.js | 14 ++--- 7 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 client/src/Pages/ConsolidatedTable.jsx create mode 100644 server/routes/consolidatedRoutes.js diff --git a/client/src/App.js b/client/src/App.js index 102ed67..e2242bb 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -13,6 +13,8 @@ import WelcomeWithFilter from "./Pages/WelcomeWithFilter"; import "react-toastify/dist/ReactToastify.css"; import CourseTable from "./Pages/CourseTable"; import GenerateCSV from "./Pages/GenerateCSV"; +import ConsolidatedTable from "./Pages/ConsolidatedTable"; + function App() { return ( @@ -30,6 +32,7 @@ function App() { }> } /> } /> + } /> ); diff --git a/client/src/Pages/ConsolidatedTable.jsx b/client/src/Pages/ConsolidatedTable.jsx new file mode 100644 index 0000000..3d2d38e --- /dev/null +++ b/client/src/Pages/ConsolidatedTable.jsx @@ -0,0 +1,58 @@ +import React, { useState, useEffect } from "react"; +import axios from "axios"; + +const ConsolidatedTable = () => { + const [data, setData] = useState([]); + const [loading, setLoading] = useState(true); + + useEffect(() => { + const fetchData = async () => { + try { + const response = await axios.get("http://localhost:8080/api/data/consolidated"); + setData(response.data); + setLoading(false); + } catch (error) { + console.error("Error fetching consolidated data:", error); + setLoading(false); + } + }; + + fetchData(); + }, []); + + if (loading) { + return
Loading...
; + } + + return ( +
+

Consolidated Data

+ + + + + + + + + + + + {data.map((faculty) => + faculty.tasks.map((task, index) => ( + + + + + + + + )) + )} + +
Faculty IDFaculty NameCourse IDCourse NameTask
{faculty.facultyId}{faculty.facultyName}{task.courseId}{task.courseName}{task.task}
+
+ ); +}; + +export default ConsolidatedTable; diff --git a/client/src/Pages/CourseForm.jsx b/client/src/Pages/CourseForm.jsx index d593359..72c0416 100644 --- a/client/src/Pages/CourseForm.jsx +++ b/client/src/Pages/CourseForm.jsx @@ -99,15 +99,20 @@ const CourseForm = () => { await updateCourseStatus(course?.courseId || id); console.log("Form submitted successfully:", payload); + const filteredCourses = JSON.parse(localStorage.getItem("filteredCourses")) || []; + // Redirect to courses page after successful submission navigate("/courses", { state: { + courses: filteredCourses, updatedCourse: { ...course, status: "Submitted", }, }, }); + + } catch (error) { console.error("Failed to save appointment:", error); } diff --git a/client/src/Pages/FilterPage.jsx b/client/src/Pages/FilterPage.jsx index 88c56e5..2b2572b 100644 --- a/client/src/Pages/FilterPage.jsx +++ b/client/src/Pages/FilterPage.jsx @@ -23,16 +23,39 @@ const FilterPage = () => { } }; + // const handleApplyFilter = async () => { + // if (!formData.scheme || !formData.semester || !formData.department || !formData.program) { + // alert("Please fill all the fields before applying the filter."); + // return; + // } + + // try { + // const filteredCourses = await fetchCourses(formData); + // console.log(formData); + // if (filteredCourses.length > 0) { + // navigate("/courses", { state: { courses: filteredCourses } }); + // } else { + // alert("No courses found for the selected filters."); + // } + // } catch (error) { + // console.error("Error fetching courses:", error); + // alert("Failed to fetch courses. Please try again later."); + // } + // }; + const handleApplyFilter = async () => { if (!formData.scheme || !formData.semester || !formData.department || !formData.program) { alert("Please fill all the fields before applying the filter."); return; } - + try { const filteredCourses = await fetchCourses(formData); console.log(formData); if (filteredCourses.length > 0) { + // Save filteredCourses in localStorage + localStorage.setItem("filteredCourses", JSON.stringify(filteredCourses)); + navigate("/courses", { state: { courses: filteredCourses } }); } else { alert("No courses found for the selected filters."); @@ -42,6 +65,7 @@ const FilterPage = () => { alert("Failed to fetch courses. Please try again later."); } }; + const getSemesters = () => { if (!formData.program) return []; diff --git a/server/controller/csvGeneratorController.js b/server/controller/csvGeneratorController.js index 8b94f47..b0b0455 100644 --- a/server/controller/csvGeneratorController.js +++ b/server/controller/csvGeneratorController.js @@ -40,7 +40,7 @@ const generateAppointmentCSV = async (req, res) => { }); await csvWriter.writeRecords(csvData); - res.status(200).json({ message: "CSV generated successfully", path: "/generated_csv/appointments.csv" }); + res.status(200).json({ message: "CSV generated successfully", path: "./generated_csv/appointments.csv" }); } catch (error) { console.error("Error generating CSV:", error); res.status(500).json({ message: "Failed to generate CSV" }); diff --git a/server/routes/consolidatedRoutes.js b/server/routes/consolidatedRoutes.js new file mode 100644 index 0000000..89acaaf --- /dev/null +++ b/server/routes/consolidatedRoutes.js @@ -0,0 +1,34 @@ +const express = require("express"); +const router = express.Router(); +const Appointment = require("../models/Appointment"); + +router.get("/consolidated", async (req, res) => { + try { + const appointments = await Appointment.find(); + + // Group data by faculty + const groupedData = appointments.reduce((acc, appointment) => { + if (!acc[appointment.facultyId]) { + acc[appointment.facultyId] = { + facultyId: appointment.facultyId, + facultyName: appointment.facultyName, + tasks: [], + }; + } + acc[appointment.facultyId].tasks.push({ + courseId: appointment.courseId, + courseName: appointment.courseName, + task: appointment.task, + }); + return acc; + }, {}); + + const result = Object.values(groupedData); + res.status(200).json(result); + } catch (error) { + console.error("Error fetching consolidated data:", error); + res.status(500).json({ message: "Failed to fetch consolidated data" }); + } +}); + +module.exports = router; diff --git a/server/server.js b/server/server.js index 90f2b70..ffa4845 100644 --- a/server/server.js +++ b/server/server.js @@ -14,6 +14,7 @@ const courseRoutes = require("./routes/courseRoutes"); const facultyRoutes = require("./routes/facultyRoutes"); const appointmentRoutes = require("./routes/appointmentRoutes"); const optionsRoutes = require("./routes/optionsRoutes"); +const consolidatedRoutes = require("./routes/consolidatedRoutes"); const Course = require("./models/Course"); // MongoDB Connection @@ -50,8 +51,9 @@ require("./config/passport"); app.use("/password", authRoutes); app.use("/api/courses", courseRoutes); app.use("/api/faculty", facultyRoutes); -app.use("/api/appointments", appointmentRoutes); // Appointment route handles the updated structure +app.use("/api/appointments", appointmentRoutes); app.use("/api/options", optionsRoutes); +app.use("/api/data", consolidatedRoutes); // Moved after `app` initialization // Google OAuth Routes app.get( @@ -157,8 +159,8 @@ app.patch("/api/courses/:courseId", async (req, res) => { const { courseId } = req.params; const { status } = req.body; - console.log('Request params:', req.params); - console.log('Request body:', req.body); + console.log("Request params:", req.params); + console.log("Request body:", req.body); if (!status) { console.error("Status is missing in the request body."); @@ -168,8 +170,8 @@ app.patch("/api/courses/:courseId", async (req, res) => { try { const updatedCourse = await Course.findOneAndUpdate( { courseId: courseId }, // Use courseId field for finding the course - { status }, // Update the status field - { new: true }// Return the updated document + { status }, // Update the status field + { new: true } // Return the updated document ); if (!updatedCourse) { @@ -184,8 +186,6 @@ app.patch("/api/courses/:courseId", async (req, res) => { } }); - - // Serve React Build Files app.use(express.static(path.join(__dirname, "../client/build"))); app.get("*", (req, res) =>