diff --git a/packages/admin/src/api/studentAPI.jsx b/packages/admin/src/api/studentAPI.jsx index 3a3abc14..9fa0ae0e 100644 --- a/packages/admin/src/api/studentAPI.jsx +++ b/packages/admin/src/api/studentAPI.jsx @@ -17,15 +17,15 @@ const studentAPI = async (data) => { }; const jsonData = { - name: data.firstName, - username: "", + name: data.name, + username:data.username ? data.username: "", email: data.email, mobile: data.mobile, gender: data.gender, - dateOfBirth: data.dob, + dateOfBirth: data.dateOfBirth, board: data.board, password: data.password, - status: "true", + // status: "true", className: data.group, groups: [], religion: data.religion, @@ -36,8 +36,11 @@ const studentAPI = async (data) => { fatherEducation: data.fatherEducation, motherOccupation: data.motherOccupation, fatherOccupation: data.fatherOccupation, - noOfSiblings: data.siblings, - studentEnrollId: data.studentEnrollId, + noOfSiblings: data.noOfSiblings, + motherName : data.motherName, + fatherName : data.fatherName, + medium : data.medium + // studentEnrollId: data.studentEnrollId, }; let result; diff --git a/packages/admin/src/api/studentUpdateAPI.jsx b/packages/admin/src/api/studentUpdateAPI.jsx new file mode 100644 index 00000000..c7d97ca5 --- /dev/null +++ b/packages/admin/src/api/studentUpdateAPI.jsx @@ -0,0 +1,67 @@ +import axios from "axios"; +import { studentRegister } from "../routes/links"; + +const studentUpdateAPI = async (data) => { + const token = sessionStorage.getItem("token"); + + const schoolUdise = data.udise.split(","); + + // Extracting the udise value in separate variable + const udiseCode = schoolUdise[0]; + + const headers = { + "Accept-Language": "en-GB,en;q=0.9", + Authorization: `Bearer ${token}`, + Connection: "keep-alive", + "Content-Type": "application/json", + }; + + const jsonData = { + name: data.name, + username:data.username ? data.username: "", + email: data.email, + mobile: data.mobile, + gender: data.gender, + dateOfBirth: data.dateOfBirth, + board: data.board, + // status: "true", + className: data.group, + groups: [], + religion: data.religion, + schoolUdise: udiseCode, + caste: data.caste, + annualIncome: data.annualIncome, + motherEducation: data.motherEducation, + fatherEducation: data.fatherEducation, + motherOccupation: data.motherOccupation, + fatherOccupation: data.fatherOccupation, + noOfSiblings: data.noOfSiblings, + motherName : data.motherName, + fatherName : data.fatherName, + medium : data.medium + // studentEnrollId: data.studentEnrollId, + }; + + let result; + await axios({ + method: "PUT", + url: studentRegister +"/"+ data.userId, + data: jsonData, + headers: headers, + }) + .then((res) => { + if (res.data && res.data.errorCode) { + result = false; + } else { + result = true; + } + }) + .catch(function (error) { + let err = 0; + return err; + }); + + return result; +}; + +export default studentUpdateAPI; diff --git a/packages/admin/src/components/StudentForm.jsx b/packages/admin/src/components/StudentForm.jsx index 456a526f..06d96c11 100644 --- a/packages/admin/src/components/StudentForm.jsx +++ b/packages/admin/src/components/StudentForm.jsx @@ -1,16 +1,16 @@ -import React from "react"; import { useForm } from "react-hook-form"; import { H2 } from "@shiksha/common-lib"; import { Button } from "native-base"; import { yupResolver } from "@hookform/resolvers/yup"; import studentAPI from "api/studentAPI"; import StudentSchema from "schema/StudentSchema"; -import { useState, useEffect, useLayoutEffect } from "react"; +import React, { useEffect, useState} from "react"; import axios from "axios"; import styles from "./StudentForm.module.css"; import { groupSearch, schoolSearch } from "routes/links"; +import studentUpdateAPI from "api/studentUpdateAPI"; -function StudentForm() { +function StudentForm({ studentData }) { const [data, setData] = useState([]); const [token, setToken] = useState([]); const [selectedUdiseCode, setSelectedUdiseCode] = useState([]); @@ -20,12 +20,47 @@ function StudentForm() { const [groups, setGroups] = useState([]); + const { + register, + handleSubmit, + reset, + formState: { errors }, + } = useForm({ resolver: yupResolver(StudentSchema) }); + useEffect(() => { const token = sessionStorage.getItem("token"); setToken(token); }, []); useEffect(() => { + if (studentData && data.length > 0) { + const udiseCodeOnly = studentData?.schoolUdise; + + // Find the school in data by matching UDISE code + const selectedSchool = data.find(school => school?.udiseCode === udiseCodeOnly); + + if (selectedSchool) { + setSelectedUdiseCode(selectedSchool?.udiseCode); // Set the UDISE code only (not the combined string) + } + + const selectedGroup = groups.find(group => group?.name === studentData?.className); + if (selectedGroup) { + setSelectedgroup(selectedGroup?.name); + } + + + // Set the other form data, like dateOfBirth + const formattedDateOfBirth = studentData?.dateOfBirth?.split("T")[0]; + reset({ + ...studentData, + dateOfBirth: formattedDateOfBirth, + }); + } + }, [studentData, data, groups, reset]); + + + useEffect(() => { + if (token) { const headers = { Accept: "*/*", Authorization: `Bearer ${token}`, @@ -33,7 +68,6 @@ function StudentForm() { }; const requestData = { - limit: "20", page: 0, filters: {}, }; @@ -47,9 +81,11 @@ function StudentForm() { // Handle any errors here console.error("Error fetching data:", error); }); + } }, [token]); useEffect(() => { + if(selectedUdiseCode.length > 0){ if (Object.keys(selectedUdiseCode).length) { // Splitting string into an array using the comma const valuesArray = selectedUdiseCode.split(","); @@ -63,10 +99,11 @@ function StudentForm() { // Values in separate variables } else { console.error("selectedUdiseCode is not a string."); - } + }} }, [selectedUdiseCode]); useEffect(async () => { + if (extractedUdise && token) { const headers = { Accept: "*/*", Authorization: `Bearer ${token}`, @@ -92,20 +129,33 @@ function StudentForm() { // Handle any errors here console.error("Error fetching data:", error); }); - }, [extractedUdise]); + } + }, [extractedUdise, token]); - const { - register, - handleSubmit, - formState: { errors }, - } = useForm({ resolver: yupResolver(StudentSchema) }); const onSubmit = async (data) => { - const result = await studentAPI(data); - if (result == true) { - alert("Registration Successful."); - window.location.reload(); - } else { - alert("Registeration failed"); + try { + if (studentData?.studentId) { + // Update old student + const result = await studentUpdateAPI(data); + if (result === true) { + alert("Updated Data Successful."); + window.location.reload(); + } else { + alert("Update failed"); + } + } else { + // Create new student + const result = await studentAPI(data); + if (result === true) { + alert("Registration Successful."); + window.location.reload(); + } else { + alert("Registration failed"); + } + } + } catch (error) { + console.error("Error saving student data:", error); + alert("An error occurred while saving the data."); } }; @@ -120,24 +170,23 @@ function StudentForm() {
{errors.firstName.message}
} + {errors.name &&{errors.name.message}
}{errors.userName.message}
} + {errors.username &&{errors.username.message}
}