From 7ebfa58cd88b7cb7e9cf3420703755e09e72a9de Mon Sep 17 00:00:00 2001 From: Sagar Gupta Date: Tue, 30 Jan 2024 21:18:14 +0530 Subject: [PATCH] Refactor StudentTable component and userController This commit refactors the StudentTable component by removing the unnecessary useEffect hook and splitting the fetchData function into fetchStudentData and fetchCompaniesData. It also updates the fetchStudentData function to sort the students by rollNo and sets the students state accordingly. The fetchCompaniesData function fetches the companies and sets the companies state. In the userController, the updateCompany function is updated to handle the case when the companyId is 'np' (Not Placed). It sets the placedAt object accordingly and updates the selectedStudentsRollNo array in the company if necessary. --- .../src/components/Student/StudentTable.jsx | 74 +++++++++---------- server/controllers/userController.js | 22 ++++-- 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/client/src/components/Student/StudentTable.jsx b/client/src/components/Student/StudentTable.jsx index d32450f..f478ff9 100644 --- a/client/src/components/Student/StudentTable.jsx +++ b/client/src/components/Student/StudentTable.jsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useState } from 'react'; +import { useCallback, useState } from 'react'; import { GrValidate } from 'react-icons/gr'; import { MdDelete } from 'react-icons/md'; import { toast } from 'react-toastify'; @@ -18,21 +18,36 @@ const StudentTable = () => { const [companies, setCompanies] = useState([]); const user = getUser(); - useEffect(() => { - const fetchCompanies = async () => { - try { - const res = await getCompanies(); - res.data.forEach((company) => { - company.id = company._id; - }); - setCompanies(res.data); - } catch (error) { - console.error('Error fetching companies:', error); - } - }; - fetchCompanies(); + const fetchStudentData = useCallback(async () => { + try { + const response = await getStudents(); + response.data.users.forEach((student) => { + student.id = student._id; + }); + response.data.users.sort((a, b) => a.rollNo.localeCompare(b.rollNo)); + setStudents(response.data.users); + } catch (error) { + console.error('Error fetching students:', error); + } + }, []); + + const fetchCompaniesData = useCallback(async () => { + try { + const res = await getCompanies(); + res.data.forEach((company) => { + company.id = company._id; + }); + setCompanies(res.data); + } catch (error) { + console.error('Error fetching companies:', error); + } }, []); + const fetchData = useCallback(async () => { + await fetchStudentData(); + await fetchCompaniesData(); + }, [fetchStudentData, fetchCompaniesData]); + const closeModal = () => { setIsModalOpen(false); setSelectedStudent(null); @@ -55,7 +70,7 @@ const StudentTable = () => { toast.success(); setIsModalOpen(false); setSelectedStudent(null); - fetchData(); + fetchStudentData(); } catch (error) { toast.error(); } @@ -67,7 +82,7 @@ const StudentTable = () => { toast.success(); setSelectedStudentDelete(null); setIsModalOpen(false); - fetchData(); + fetchStudentData(); } catch (error) { toast.error(); } @@ -77,7 +92,7 @@ const StudentTable = () => { try { const res = await updateUserRole(params.id, event.target.value); toast.success(); - fetchData(); + fetchStudentData(); } catch (error) { toast.error(); } @@ -87,7 +102,7 @@ const StudentTable = () => { try { const res = await updateStudentCompany(params.id, event.target.value); toast.success(); - fetchData(); + fetchStudentData(); } catch (error) { toast.error(); } @@ -126,14 +141,12 @@ const StudentTable = () => { const companyDropdownRenderer = (params) => { return ( - handleCompanyChange(event, params.data)}> {companies.map((company) => ( - + ))} ); @@ -143,19 +156,6 @@ const StudentTable = () => { return closeModal()} onConfirm={onConfirm} message={message} buttonTitle={buttonTitle} />; }; - const fetchData = useCallback(async () => { - try { - const response = await getStudents(); - response.data.users.forEach((student) => { - student.id = student._id; - }); - response.data.users.sort((a, b) => a.rollNo.localeCompare(b.rollNo)); - setStudents(response.data.users); - } catch (error) { - console.error('Error fetching students:', error); - } - }, []); - const generateColumn = (field, headerName, width, pinned = null, sortable = true, resizable = true, cellRenderer = null) => ({ field, headerName, diff --git a/server/controllers/userController.js b/server/controllers/userController.js index 1655772..48aef3a 100644 --- a/server/controllers/userController.js +++ b/server/controllers/userController.js @@ -158,8 +158,8 @@ exports.updateCompany = async (req, res) => { location: 'N/A', bond: 0 }; - if(req.body.companyId !== 'np'){ - let company = await Company.findById(req.body.companyId); + if (req.body.companyId !== 'np') { + const company = await Company.findById(req.body.companyId); placedAt = { companyId: company._id, companyName: company.name, @@ -170,13 +170,25 @@ exports.updateCompany = async (req, res) => { location: company.locations[0], bond: company.bond }; + // Update in Selected Students Roll No if it already not present + if (!company.selectedStudentsRollNo.includes(user.rollNo)) { + company.selectedStudentsRollNo.push(user.rollNo); + await company.save(); + } + } else { + const company = await Company.findById(user.placedAt.companyId); + const index = company.selectedStudentsRollNo.indexOf(user.rollNo); + if (index > -1) { + company.selectedStudentsRollNo.splice(index, 1); + } + await company.save(); } - + const updatedUser = await User.findByIdAndUpdate( req.params.id, { - placedAt: placedAt, - placed: req.body.companyId!=='np' ? true : false + placedAt, + placed: req.body.companyId !== 'np' ? true : false }, { new: true } );