Skip to content

Commit

Permalink
Refactor StudentTable component and userController
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Sagargupta16 committed Jan 30, 2024
1 parent 801c220 commit 7ebfa58
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
74 changes: 37 additions & 37 deletions client/src/components/Student/StudentTable.jsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand All @@ -55,7 +70,7 @@ const StudentTable = () => {
toast.success(<ToastContent res="success" messages={[res.data.message]} />);
setIsModalOpen(false);
setSelectedStudent(null);
fetchData();
fetchStudentData();
} catch (error) {
toast.error(<ToastContent res="error" messages={[error.response.data.message]} />);
}
Expand All @@ -67,7 +82,7 @@ const StudentTable = () => {
toast.success(<ToastContent res="success" messages={[res.data.message]} />);
setSelectedStudentDelete(null);
setIsModalOpen(false);
fetchData();
fetchStudentData();
} catch (error) {
toast.error(<ToastContent res="error" messages={[error.response.data.message]} />);
}
Expand All @@ -77,7 +92,7 @@ const StudentTable = () => {
try {
const res = await updateUserRole(params.id, event.target.value);
toast.success(<ToastContent res="success" messages={[res.data.message]} />);
fetchData();
fetchStudentData();
} catch (error) {
toast.error(<ToastContent res="error" messages={[error.response.data.message]} />);
}
Expand All @@ -87,7 +102,7 @@ const StudentTable = () => {
try {
const res = await updateStudentCompany(params.id, event.target.value);
toast.success(<ToastContent res="success" messages={[res.data.message]} />);
fetchData();
fetchStudentData();
} catch (error) {
toast.error(<ToastContent res="error" messages={[error.response.data.message]} />);
}
Expand Down Expand Up @@ -126,14 +141,12 @@ const StudentTable = () => {

const companyDropdownRenderer = (params) => {
return (
<select
className="render-dropdown"
value={params.data.placedAt.companyId}
onChange={(event) => handleCompanyChange(event, params.data)}
>
<select className="render-dropdown" value={params.data.placedAt.companyId} onChange={(event) => handleCompanyChange(event, params.data)}>
<option value="np">Not Placed</option>
{companies.map((company) => (
<option value={company.id}>{company.name}</option>
<option value={company.id} key={company.id}>
{company.name}
</option>
))}
</select>
);
Expand All @@ -143,19 +156,6 @@ const StudentTable = () => {
return <Modal isOpen={isModalOpen} onClose={() => 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,
Expand Down
22 changes: 17 additions & 5 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 }
);
Expand Down

0 comments on commit 7ebfa58

Please sign in to comment.