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 } );