diff --git a/packages/admin/src/api/studentAPI.jsx b/packages/admin/src/api/studentAPI.jsx index 9fa0ae0e..47b15056 100644 --- a/packages/admin/src/api/studentAPI.jsx +++ b/packages/admin/src/api/studentAPI.jsx @@ -39,7 +39,10 @@ const studentAPI = async (data) => { noOfSiblings: data.noOfSiblings, motherName : data.motherName, fatherName : data.fatherName, - medium : data.medium + medium : data.medium, + state : data.state, + block : data.block, + district : data.district, // studentEnrollId: data.studentEnrollId, }; diff --git a/packages/admin/src/api/studentUpdateAPI.jsx b/packages/admin/src/api/studentUpdateAPI.jsx index c7d97ca5..ae918aa0 100644 --- a/packages/admin/src/api/studentUpdateAPI.jsx +++ b/packages/admin/src/api/studentUpdateAPI.jsx @@ -38,7 +38,10 @@ const studentUpdateAPI = async (data) => { noOfSiblings: data.noOfSiblings, motherName : data.motherName, fatherName : data.fatherName, - medium : data.medium + medium : data.medium, + state : data.state, + block : data.block, + district : data.district, // studentEnrollId: data.studentEnrollId, }; diff --git a/packages/admin/src/components/StudentForm.jsx b/packages/admin/src/components/StudentForm.jsx index 06d96c11..f5b11ae1 100644 --- a/packages/admin/src/components/StudentForm.jsx +++ b/packages/admin/src/components/StudentForm.jsx @@ -7,7 +7,7 @@ import StudentSchema from "schema/StudentSchema"; import React, { useEffect, useState} from "react"; import axios from "axios"; import styles from "./StudentForm.module.css"; -import { groupSearch, schoolSearch } from "routes/links"; +import { groupSearch, schoolSearch,getStateList,getBlockList, getBlockList } from "routes/links"; import studentUpdateAPI from "api/studentUpdateAPI"; function StudentForm({ studentData }) { @@ -19,7 +19,13 @@ function StudentForm({ studentData }) { const [selectedgroup, setSelectedgroup] = useState(""); // Initialize with an empty string const [groups, setGroups] = useState([]); - + const [stateData,setStateData] = useState([]) + const [districtData, setDistrictData] = useState([]) + const [blockData,setBlockData] = useState([]) + const [selectedState, setSelectedState] = useState(""); + const [selectedDistrict, setSelectedDistrict] = useState(""); + const [selectedBlock, setSelectedBlock] = useState(""); + const [loading, setLoading] = useState(false); const { register, handleSubmit, @@ -48,6 +54,17 @@ function StudentForm({ studentData }) { setSelectedgroup(selectedGroup?.name); } + if(studentData.state){ + setSelectedState(studentData?.state); + } + + if(studentData.block){ + setSelectedBlock(studentData?.block) + } + + if(studentData.district){ + setSelectedDistrict(studentData?.district) + } // Set the other form data, like dateOfBirth const formattedDateOfBirth = studentData?.dateOfBirth?.split("T")[0]; @@ -57,33 +74,119 @@ function StudentForm({ studentData }) { }); } }, [studentData, data, groups, reset]); - + const MAX_RETRIES = 1; + + const retryApiCall = async (apiCall, retries = MAX_RETRIES) => { + let attempt = 0; + while (attempt < retries) { + try { + const response = await apiCall(); + return response; + } catch (error) { + if (error.response && error.response.status === 500) { + attempt += 1; + console.error(`Retrying API call... Attempt ${attempt}`); + if (attempt >= retries) throw error; // Throw error after max retries + } else { + throw error; + } + } + } + }; + useEffect(() => { - if (token) { - const headers = { - Accept: "*/*", - Authorization: `Bearer ${token}`, - "Content-Type": "application/json", - }; + if (token && studentData) { + setLoading(true); + + const headers = { + Accept: "*/*", + Authorization: `Bearer ${token}`, + "Content-Type": "application/json", + }; + + const requestDataSchool = { page: 0, filters: {} }; + const requestDataDistrict = { state: studentData.state }; + const requestDataBlock = { district: studentData.district }; + + const apiCalls = [ + () => axios.post(schoolSearch, requestDataSchool, { headers }), + () => axios.post(getStateList, {}, { headers }), + studentData.state + ? () => axios.post(getDistrictList, requestDataDistrict, { headers }) + : null, + studentData.district + ? () => axios.post(getBlockList, requestDataBlock, { headers }) + : null, + ].filter(Boolean); + + Promise.all(apiCalls.map((call) => retryApiCall(call))) + .then(([schoolResponse, stateResponse, districtResponse, blockResponse]) => { + if (schoolResponse) setData(schoolResponse.data.data); + if (stateResponse) setStateData(stateResponse.data.data); + if (districtResponse) setDistrictData(districtResponse.data.data); + if (blockResponse) setBlockData(blockResponse.data.data); + }) + .catch((error) => { + console.error("Error fetching data:", error); + }) + .finally(() => { + setLoading(false); + }); + } + }, [token,studentData]); - const requestData = { - page: 0, - filters: {}, - }; + //when change in state + const handleStateChangeApi = (selectedState) => { + if (token && selectedState) { + const headers = { + Accept: "*/*", + Authorization: `Bearer ${token}`, + "Content-Type": "application/json", + }; + const requestData = { state: selectedState }; + + axios.post(getDistrictList, requestData, { headers }) + .then((response) => { + setDistrictData(response.data.data); + setLoading(false); + }) + .catch((error) => { + console.error("Error fetching district data:", error); + }); + } + }; - axios - .post(schoolSearch, requestData, { headers }) - .then((response) => { - setData(response.data.data); - }) - .catch((error) => { - // Handle any errors here - console.error("Error fetching data:", error); - }); + const handleDistrictChangeApi = (selectedDistrict) => { + if (token && selectedDistrict) { + const headers = { + Accept: "*/*", + Authorization: `Bearer ${token}`, + "Content-Type": "application/json", + }; + const requestData = { district: selectedDistrict }; + axios.post(getBlockList, requestData, { headers }) + .then((response) => { + setBlockData(response.data.data); + setLoading(false); + }) + .catch((error) => { + console.error("Error fetching block data:", error); + }); } - }, [token]); + } + const handleStateChange = (e) => { + const newState = e.target.value; + setSelectedState(newState); + handleStateChangeApi(newState); + }; + const handleDistrictChange = (e) => { + const newDistrict = e.target.value; + setSelectedDistrict(newDistrict); + handleDistrictChangeApi(newDistrict); + }; + useEffect(() => { if(selectedUdiseCode.length > 0){ if (Object.keys(selectedUdiseCode).length) { @@ -161,6 +264,9 @@ function StudentForm({ studentData }) { return (
+ {loading ? ( +
Loading...
+ ) : (
setSelectedgroup(e.target.value)} + disabled={studentData} // Disable the field in edit mode > - {" "} - {/* Placeholder option */} + {groups.map((school) => (


-
- - {/* */} -
- {errors.motherName &&

{errors.motherName.message}

} -
-
-

-
- - {/* */} -
- {errors.fatherName &&

{errors.fatherName.message}

} -
-
-



*/}
- - {/* */} + onChange={handleStateChange} + > + + {stateData.map((state, index) => ( + + ))} +
{errors.state &&

{errors.state.message}

}


- - {/* */} + +
- {errors.block &&

{errors.block.message}

} + {errors.district &&

{errors.district.message}

}
+

- {/*
- -
- {errors.serialNo &&

{errors.serialNo.message}

} -
-
-

*/}
- - {/* */} + +
- {errors.district &&

{errors.district.message}

} + {errors.block &&

{errors.block.message}

}


@@ -719,7 +794,7 @@ function StudentForm({ studentData }) {


*/} -
+ {/*
- {/* */}
{errors.medium &&

{errors.medium.message}

}
-

+

*/} {/*
+ )}
); } diff --git a/packages/admin/src/components/StudentForm.module.css b/packages/admin/src/components/StudentForm.module.css index 0b198125..56e3fa85 100644 --- a/packages/admin/src/components/StudentForm.module.css +++ b/packages/admin/src/components/StudentForm.module.css @@ -103,3 +103,13 @@ gap: 25px; margin-top: 10px; } + +.loader { + display: flex; + justify-content: center; + align-items: center; + width: 100%; + height: 100vh; + color: rgb(100, 97, 210); + font-size: 1.5rem; +} diff --git a/packages/admin/src/routes/links.jsx b/packages/admin/src/routes/links.jsx index 101aaff9..c4479095 100644 --- a/packages/admin/src/routes/links.jsx +++ b/packages/admin/src/routes/links.jsx @@ -9,11 +9,11 @@ export const teacherBulk = baseLink + "/teacher/bulkupload"; export const studentReset = baseLink + "/user/reset-password"; export const studentSearch = baseLink + "/student/search"; export const teacherSearch = baseLink + "/teacher/search"; -export const schoolSearch = baseLink + "/school/search"; -export const groupSearch = baseLink + "/group/search"; +export const schoolSearch = baseLink + "/school/search";export const groupSearch = baseLink + "/group/search"; + // Add new endpoints export const getStateList = baseLink + "/student/getStatesList"; export const getDistrictList = baseLink + "/student/getDistrictList"; export const getBlockList = baseLink + "/student/getBlockList"; export const getSchoolList = baseLink + "/student/getSchoolList"; -export const getClassList = baseLink + "/student/getClass"; +export const getClassList = baseLink + "/student/getClass"; \ No newline at end of file