Skip to content

Commit

Permalink
Merge pull request #89 from deepali-chavhan/issue-fix
Browse files Browse the repository at this point in the history
fix: issues fixes in student and techer filter and csv upload
  • Loading branch information
ankush-maherwal authored Nov 23, 2024
2 parents eda05e2 + f8ec60d commit 9d37425
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 76 deletions.
21 changes: 16 additions & 5 deletions packages/admin/src/api/filterStudentDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export const fetchStates = async (token) => {
{ state: "" }, // Fetch all states
{ headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" }}
);
return response.data.data.map(item => item.state);
return response.data.data.map(item => ({
label: `${item.state}`
}));
} catch (error) {
console.error("Error fetching states:", error);
throw error;
Expand All @@ -24,7 +26,9 @@ export const fetchDistricts = async (token, state = "") => {
{ state }, // Send selected state as payload
{ headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" }}
);
return response.data.data.map(item => item.district);
return response.data.data.map(item => ({
label: `${item.district}`
}));
} catch (error) {
console.error("Error fetching districts:", error);
throw error;
Expand All @@ -39,7 +43,9 @@ export const fetchBlocks = async (token, state = "", district = "") => {
{ state, district }, // Send both state and district as payload
{ headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" }}
);
return response.data.data.map(item => item.block);
return response.data.data.map(item => ({
label: `${item.block}`
}));
} catch (error) {
console.error("Error fetching blocks:", error);
throw error;
Expand All @@ -54,7 +60,10 @@ export const fetchSchools = async (token, state = "", district = "", block = "")
{ state, district, block }, // Send state, district, and block as payload
{ headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" }}
);
return response.data.data.map(item => item.name);
return response.data.data.map(item => ({
label: `${item.name}`,
udiseCode: `${item.udiseCode}`
}));
} catch (error) {
console.error("Error fetching schools:", error);
throw error;
Expand All @@ -69,7 +78,9 @@ export const fetchClasses = async (token, schoolName = "") => {
{ schoolName }, // Send selected schoolName as payload
{ headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" }}
);
return response.data.data.map(item => item.name);
return response.data.data.map(item => ({
label: `${item.name}`
}));
} catch (error) {
console.error("Error fetching classes:", error);
throw error;
Expand Down
11 changes: 6 additions & 5 deletions packages/admin/src/api/teacherBulkAPI.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const teacherBulkAPI = async (teacher) => {
headers: headers,
})
.then((res) => {
const names = res.data.errors.map((error) => error.name).filter(Boolean);
if (res.data.errors && res.data.errors.length > 0) {
const firstError = res.data.errors[0];
const names = res?.data?.data?.errors.map((error) => error.name).filter(Boolean);
if (res?.data?.data?.errors && res?.data?.data?.errors.length > 0) {
const firstError = res?.data?.data?.errors[0];
if (firstError.teacherRes && firstError.teacherRes.errorMessage) {
const errorMessage = firstError.teacherRes.errorMessage;
localStorage.setItem("errorMessage", errorMessage);
Expand All @@ -32,9 +32,10 @@ const teacherBulkAPI = async (teacher) => {
console.log("No errors in the response data.");
}

localStorage.setItem("bulkErrors", res.data.errors.length - 1);
const errorCount = res?.data?.data?.errors?.length ?? 0;
localStorage.setItem("bulkErrors", errorCount > 0 ? errorCount - 1 : 0);
localStorage.setItem("bulkErrorsNames", names);
localStorage.setItem("successCount", res.data.successCount);
localStorage.setItem("successCount", res?.data?.data?.successCount);
if (res.status === 201) {
result = true;
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/admin/src/assets/images/FinalStudent.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name,username,email,mobile,gender,dateOfBirth,board,password,status,className,religion,school_udise,caste,annual_income,mother_education,father_education,mother_occupation,father_occupation,No_of_siblings,promotion
Test,retesttekdiwb600112121884,,9999999999,Female ,1990-01-03,West Bengal,NA ,TRUE,Class 7,Hindu,12345678,General,668,,,,,,,
name,username,email,mobile,gender,dateOfBirth,board,password,status,className,religion,school_udise,caste,annual_income,mother_education,father_education,mother_occupation,father_occupation,No_of_siblings,promotion,state,district,block
Test,retesttekdiwb600112121884,,9999999999,Female ,1990-01-03,West Bengal,NA ,TRUE,Class 7,Hindu,12345678,General,668,,,,,,,maharastra,pune,kothrud
60 changes: 30 additions & 30 deletions packages/admin/src/components/DownloadCsv.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ const DownloadCsv = ({ open, handleClose }) => {
// Update districts based on selected state
useEffect(() => {
const loadDistricts = async () => {
if (!dropdownValues.stateDropdown) return; // Check for selected state
if (!dropdownValues.stateDropdown?.label) return; // Check for selected state
try {
const token = sessionStorage.getItem("token");
const districts = await fetchDistricts(
token,
dropdownValues.stateDropdown
dropdownValues.stateDropdown?.label
);
setDistrictOptions(districts);

const blocks = await fetchBlocks(token, dropdownValues.stateDropdown);
const blocks = await fetchBlocks(token, dropdownValues.stateDropdown?.label);
setBlockOptions(blocks);

const schools = await fetchSchools(token, dropdownValues.stateDropdown);
const schools = await fetchSchools(token, dropdownValues.stateDropdown?.label);
setSchoolOptions(schools);
} catch (error) {
console.error("Error loading districts:", error);
Expand All @@ -121,14 +121,14 @@ const DownloadCsv = ({ open, handleClose }) => {
// Update blocks based on selected district
useEffect(() => {
const loadBlocks = async () => {
if (!dropdownValues.districtDropdown) return; // Check for selected district
if (!dropdownValues.districtDropdown?.label) return; // Check for selected district
try {
const token = sessionStorage.getItem("token");

const blocks = await fetchBlocks(
token,
dropdownValues.stateDropdown,
dropdownValues.districtDropdown
dropdownValues.stateDropdown?.label,
dropdownValues.districtDropdown?.label
);
setBlockOptions(blocks);
} catch (error) {
Expand All @@ -145,9 +145,9 @@ const DownloadCsv = ({ open, handleClose }) => {
const token = sessionStorage.getItem("token");
const schools = await fetchSchools(
token,
dropdownValues.stateDropdown,
dropdownValues.districtDropdown,
dropdownValues.blockDropdown
dropdownValues.stateDropdown?.label,
dropdownValues.districtDropdown?.label,
dropdownValues.blockDropdown?.label
);
setSchoolOptions(schools);
} catch (error) {
Expand Down Expand Up @@ -184,11 +184,11 @@ const DownloadCsv = ({ open, handleClose }) => {

// Ensure at least one dropdown value is selected before allowing download
if (
!dropdownValues.stateDropdown &&
!dropdownValues.districtDropdown &&
!dropdownValues.blockDropdown &&
!dropdownValues.schoolNameDropdown &&
!dropdownValues.classNameDropdown
!dropdownValues.stateDropdown?.label &&
!dropdownValues.districtDropdown?.label &&
!dropdownValues.blockDropdown?.label &&
!dropdownValues.schoolNameDropdown?.label &&
!dropdownValues.classNameDropdown?.label
) {
alert("Please select at least one filter before downloading.");
return false;
Expand All @@ -199,20 +199,20 @@ const DownloadCsv = ({ open, handleClose }) => {
filters: {},
};

if (dropdownValues.stateDropdown) {
payload.filters.state = { eq: dropdownValues.stateDropdown };
if (dropdownValues.stateDropdown?.label) {
payload.filters.state = { eq: dropdownValues.stateDropdown?.label };
}
if (dropdownValues.districtDropdown) {
payload.filters.district = { eq: dropdownValues.districtDropdown };
if (dropdownValues.districtDropdown?.label) {
payload.filters.district = { eq: dropdownValues.districtDropdown?.label };
}
if (dropdownValues.blockDropdown) {
payload.filters.block = { eq: dropdownValues.blockDropdown };
if (dropdownValues.blockDropdown?.label) {
payload.filters.block = { eq: dropdownValues.blockDropdown?.label };
}
if (dropdownValues.schoolNameDropdown) {
payload.filters.schoolName = { eq: dropdownValues.schoolNameDropdown };
if (dropdownValues.schoolNameDropdown?.label) {
payload.filters.schoolName = { eq: dropdownValues.schoolNameDropdown?.label };
}
if (dropdownValues.classNameDropdown) {
payload.filters.class = { eq: dropdownValues.classNameDropdown };
if (dropdownValues.classNameDropdown?.label) {
payload.filters.class = { eq: dropdownValues.classNameDropdown?.label };
}

try {
Expand Down Expand Up @@ -292,11 +292,11 @@ const DownloadCsv = ({ open, handleClose }) => {
};

const isDownloadDisabled =
!dropdownValues.stateDropdown &&
!dropdownValues.districtDropdown &&
!dropdownValues.blockDropdown &&
!dropdownValues.schoolNameDropdown &&
!dropdownValues.classNameDropdown;
!dropdownValues.stateDropdown?.label &&
!dropdownValues.districtDropdown?.label &&
!dropdownValues.blockDropdown?.label &&
!dropdownValues.schoolNameDropdown?.label &&
!dropdownValues.classNameDropdown?.label;

const handleChange = (event, value, name) => {
const dropdownOrder = [
Expand Down
3 changes: 2 additions & 1 deletion packages/admin/src/components/FilterTableData.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const FilterTableData = ({ open, handleClose, rowData, onApplyFilter }) => {
<Autocomplete
disablePortal
options={filterOptions}
getOptionLabel={(option) => option.label}
getOptionLabel={(option) => option.label || ""}
value={filterOption}
onChange={(event, value) => setFilterOption(value)}
sx={{ width: "100%", marginBottom: 2 }}
Expand All @@ -88,6 +88,7 @@ const FilterTableData = ({ open, handleClose, rowData, onApplyFilter }) => {
<Autocomplete
disablePortal
options={filterValues}
getOptionLabel={(option) => option.label || ""}
value={selectedFilterValue}
onChange={(event, value) => setSelectedFilterValue(value)}
disabled={!filterOption} // Disable until a filter option is selected
Expand Down
29 changes: 17 additions & 12 deletions packages/admin/src/components/StudentFilters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ const StudentFilters = ({ handleFiltersChange }) => {

useEffect(() => {
const loadDistricts = async () => {
if (!dropdownValues.stateDropdown) return;
if (!dropdownValues.stateDropdown.label) return;
try {
const token = sessionStorage.getItem("token");
const districts = await fetchDistricts(
token,
dropdownValues.stateDropdown
dropdownValues.stateDropdown.label
);
setDistrictOptions(districts);

const blocks = await fetchBlocks(token, dropdownValues.stateDropdown);
const blocks = await fetchBlocks(token, dropdownValues.stateDropdown.label);
setBlockOptions(blocks);

const schools = await fetchSchools(token, dropdownValues.stateDropdown);
const schools = await fetchSchools(token, dropdownValues.stateDropdown.label);
setSchoolOptions(schools);
} catch (error) {
console.error("Error loading districts:", error);
Expand All @@ -98,14 +98,14 @@ const StudentFilters = ({ handleFiltersChange }) => {
// Update blocks based on selected district
useEffect(() => {
const loadBlocks = async () => {
if (!dropdownValues.districtDropdown) return;
if (!dropdownValues.districtDropdown.label) return;
try {
const token = sessionStorage.getItem("token");

const blocks = await fetchBlocks(
token,
dropdownValues.stateDropdown,
dropdownValues.districtDropdown
dropdownValues.stateDropdown.label,
dropdownValues.districtDropdown.label
);
setBlockOptions(blocks);
} catch (error) {
Expand All @@ -122,9 +122,9 @@ const StudentFilters = ({ handleFiltersChange }) => {
const token = sessionStorage.getItem("token");
const schools = await fetchSchools(
token,
dropdownValues.stateDropdown,
dropdownValues.districtDropdown,
dropdownValues.blockDropdown
dropdownValues.stateDropdown.label,
dropdownValues.districtDropdown.label,
dropdownValues.blockDropdown.label
);
setSchoolOptions(schools);
} catch (error) {
Expand All @@ -137,12 +137,12 @@ const StudentFilters = ({ handleFiltersChange }) => {
// Fetch classes based on selected school
useEffect(() => {
const loadClasses = async () => {
if (!dropdownValues.schoolNameDropdown) return;
if (!dropdownValues.schoolNameDropdown.label) return;
try {
const token = sessionStorage.getItem("token");
const classes = await fetchClasses(
token,
dropdownValues.schoolNameDropdown
dropdownValues.schoolNameDropdown.label
);
setClassOptions(classes);
} catch (error) {
Expand Down Expand Up @@ -186,6 +186,7 @@ const StudentFilters = ({ handleFiltersChange }) => {
<Autocomplete
disablePortal
options={stateOptions}
getOptionLabel={(option) => option.label || ""}
value={dropdownValues.stateDropdown}
onChange={(event, value) =>
handleChange(event, value, "stateDropdown")
Expand All @@ -207,6 +208,7 @@ const StudentFilters = ({ handleFiltersChange }) => {
<Autocomplete
disablePortal
options={districtOptions}
getOptionLabel={(option) => option.label || ""}
value={dropdownValues.districtDropdown}
onChange={(event, value) =>
handleChange(event, value, "districtDropdown")
Expand All @@ -228,6 +230,7 @@ const StudentFilters = ({ handleFiltersChange }) => {
<Autocomplete
disablePortal
options={blockOptions}
getOptionLabel={(option) => option.label || ""}
value={dropdownValues.blockDropdown}
onChange={(event, value) =>
handleChange(event, value, "blockDropdown")
Expand All @@ -249,6 +252,7 @@ const StudentFilters = ({ handleFiltersChange }) => {
<Autocomplete
disablePortal
options={schoolOptions}
getOptionLabel={(option) => `${option.label} (${option.udiseCode})`}
value={dropdownValues.schoolNameDropdown}
onChange={(event, value) =>
handleChange(event, value, "schoolNameDropdown")
Expand All @@ -270,6 +274,7 @@ const StudentFilters = ({ handleFiltersChange }) => {
<Autocomplete
disablePortal
options={classOptions}
getOptionLabel={(option) => option.label || ""}
value={dropdownValues.classNameDropdown}
onChange={(event, value) =>
handleChange(event, value, "classNameDropdown")
Expand Down
Loading

0 comments on commit 9d37425

Please sign in to comment.