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 (
{errors.motherName.message}
} -{errors.fatherName.message}
} -{errors.state.message}
}{errors.block.message}
} + {errors.district &&{errors.district.message}
}{errors.serialNo.message}
} -{errors.district.message}
} + {errors.block &&{errors.block.message}
}{errors.medium.message}
}