diff --git a/src/pages/stateDetails.tsx b/src/pages/stateDetails.tsx index 789ead83..d78f99e8 100644 --- a/src/pages/stateDetails.tsx +++ b/src/pages/stateDetails.tsx @@ -18,6 +18,8 @@ const StateDetails = () => { const [boards, setBoards] = useState([]); useEffect(() => { + const stateValue = Array.isArray(state) ? state.join(",") : state || ""; + localStorage.setItem("selectedState", stateValue); const fetchBoards = () => { let boardsData = []; if ( diff --git a/src/pages/subjectDetails.tsx b/src/pages/subjectDetails.tsx index ec0eef82..9e40dc66 100644 --- a/src/pages/subjectDetails.tsx +++ b/src/pages/subjectDetails.tsx @@ -34,6 +34,8 @@ import { import { TelemetryEventType } from "@/utils/app.constant"; import { telemetryFactory } from "@/utils/telemetry"; import theme from "@/components/theme/theme"; +import { FRAMEWORK_ID } from "../../app.config"; +import axios from "axios"; // Define Card interface interface Card { @@ -77,6 +79,7 @@ const SubjectDetails = () => { const [selectedgrade, setSelectedgrade] = useState(); const [gradeOptions, setGradeOptions] = useState([]); const [typeOptions, setTypeOptions] = useState([]); + const [newAssociations, setNewAssociations] = useState([]); const [type, setType] = useState([]); const [selectedtype, setSelectedtype] = useState(); const setTaxanomySubject = coursePlannerStore( @@ -86,6 +89,216 @@ const SubjectDetails = () => { const setTaxonomyGrade = taxonomyStore((state) => state.setTaxonomyGrade); const setTaxonomyType = taxonomyStore((state) => state.setTaxonomyType); const setTaxonomySubject = taxonomyStore((state) => state.setTaxonomySubject); + const [framework, setFramework] = useState([]); + const [selectedBoard, setSelectedBoard] = useState([]); + const setStateassociations = coursePlannerStore( + (state) => state.setStateassociations + ); + const setBoards = coursePlannerStore((state) => state.setBoards); + + // useEffect(() => { + // const handleBMGS = async () => { + // try { + // const StateName = "Rajasthan"; + // const medium = "Hindi"; + // const grade = "Grade 10"; + // const board = "NIOS"; + + // if (StateName && medium && grade && board) { + // const url = `/api/framework/v1/read/${FRAMEWORK_ID}`; + // const boardData = await fetch(url).then((res) => res.json()); + // const frameworks = boardData?.result?.framework; + + // const getStates = getOptionsByCategory(frameworks, "state"); + // const matchState = getStates.find( + // (item: any) => + // item?.name?.toLowerCase() === StateName?.toLocaleLowerCase() + // ); + + // const getBoards = getOptionsByCategory(frameworks, "board"); + // console.log("getBoards", getBoards); + // const matchBoard = getBoards.find((item: any) => item.name === board); + // console.log("matchBoard", matchBoard); + // const getMedium = getOptionsByCategory(frameworks, "medium"); + // const matchMedium = getMedium.find( + // (item: any) => item.name === medium + // ); + + // const getGrades = getOptionsByCategory(frameworks, "gradeLevel"); + // const matchGrade = getGrades.find((item: any) => item.name === grade); + + // const getCourseTypes = getOptionsByCategory(frameworks, "courseType"); + // const courseTypes = getCourseTypes?.map((type: any) => type.name); + // // setCourseTypes(courseTypes); + + // const courseTypesAssociations = getCourseTypes?.map((type: any) => { + // return { + // code: type.code, + // name: type.name, + // associations: type.associations, + // }; + // }); + + // const courseSubjectLists = courseTypesAssociations.map( + // (courseType: any) => { + // const commonAssociations = courseType?.associations.filter( + // (assoc: any) => + // matchState?.associations.filter( + // (item: any) => item.code === assoc.code + // )?.length && + // matchBoard?.associations.filter( + // (item: any) => item.code === assoc.code + // )?.length && + // matchMedium?.associations.filter( + // (item: any) => item.code === assoc.code + // )?.length && + // matchGrade?.associations.filter( + // (item: any) => item.code === assoc.code + // )?.length + // ); + // console.log(commonAssociations); + // const getSubjects = getOptionsByCategory(frameworks, "subject"); + // const subjectAssociations = commonAssociations?.filter( + // (assoc: any) => + // getSubjects.map((item: any) => assoc.code === item?.code) + // ); + // console.log(subjectAssociations); + // return { + // courseTypeName: courseType?.name, + // courseType: courseType?.code, + // subjects: subjectAssociations?.map( + // (subject: any) => subject?.name + // ), + // }; + // } + // ); + + // console.log(courseSubjectLists); + // // setSubjectLists(courseSubjectLists); + // } + // } catch (error) { + // console.error("Error fetching board data:", error); + // } + // }; + // handleBMGS(); + // }, []); + + useEffect(() => { + const fetchTaxonomyResultsOne = async () => { + try { + const url = `/api/framework/v1/read/${FRAMEWORK_ID}`; + + // Use axios to fetch data from the API + const response = await axios.get(url); + const boardData = response.data; + + console.log(boardData?.result?.framework); + const frameworks = boardData?.result?.framework; + + // Get states options + const getStates = getOptionsByCategory(frameworks, "state"); + + const matchingState = getStates.find( + (state: any) => state.name === localStorage.getItem("selectedState") + ); + + if (matchingState) { + setStateassociations(matchingState?.associations); + const getBoards = await getOptionsByCategory(frameworks, "board"); + if (getBoards && matchingState) { + const commonBoardsNew = await getBoards + .filter((item1: { code: any }) => + matchingState.associations.some( + (item2: { code: any; category: string }) => + item2.code === item1.code && item2.category === "board" + ) + ) + .map((item1: { name: any; code: any; associations: any }) => ({ + name: item1.name, + code: item1.code, + associations: item1.associations, + })); + + setNewAssociations(commonBoardsNew); + + console.log("FIRST TIME API", getBoards); + + const commonBoards = await getBoards + .filter((item1: { code: any }) => + matchingState?.associations?.some( + (item2: { code: any; category: string }) => + item2.code === item1.code && item2.category === "board" + ) + ) + .map((item1: { name: any; code: any; associations: any }) => ({ + name: item1.name, + code: item1.code, + associations: item1.associations, + })); + + console.log("FIRST TIME API", commonBoards); + + const stateBoardMapping = getStates.map((state: any) => { + const stateAssociations = state.associations || []; + const boards = getOptionsByCategory(frameworks, "board"); + + const associatedBoards = boards + .filter((board: { code: any }) => + stateAssociations.some( + (assoc: { code: any; category: string }) => + assoc.code === board.code && assoc.category === "board" + ) + ) + .map((board: { name: any; code: any }) => ({ + name: board.name, + code: board.code, + })); + + return { + stateName: state.name, + boards: associatedBoards, + associations: stateAssociations, + }; + }); + + console.log("State-Board Mapping:", stateBoardMapping); + const selectedState = localStorage.getItem("selectedState"); + + const filteredState = stateBoardMapping.filter( + (state: any) => state.stateName === selectedState + ); + + // Log the result + if (filteredState) { + console.log("Filtered State Data:", filteredState); + + // Set the frameworks state + setFramework(frameworks); + + // const getBoardsByName = (commonBoards: any, boardName: any) => { + // return commonBoards.filter( + // (commonBoards: any) => commonBoards.name === boardName + // ); + // }; + + // const selectedBoard = getBoardsByName(commonBoards, boardName); + // console.log(selectedBoard); + setBoards(filteredState); + setSelectedBoard(filteredState); + } else { + console.log("State not found in the mapping."); + } + // } + // } + } + } + } catch (error) { + console.error("Failed to fetch cohort search results:", error); + } + }; + + fetchTaxonomyResultsOne(); + }, []); useEffect(() => { const savedMedium = localStorage.getItem("selectedMedium") || ""; @@ -120,6 +333,8 @@ const SubjectDetails = () => { store?.framedata, "medium" ); + console.log(store?.boards); + const normalizedBoards = normalizeData(store?.boards || []); const boardAssociations = getAssociationsByCodeNew( normalizedBoards, @@ -267,86 +482,98 @@ const SubjectDetails = () => { setType(commonType3Data); }; - const fetchAndSetSubData = (type: any) => { - const typeAssociations = getAssociationsByCodeNew(typeOptions, type); - setTypeAssociations(typeAssociations); - const subject = getOptionsByCategory(store?.framedata, "subject"); - - console.log(subject); + const fetchAndSetSubData = async (type: any) => { + try { + const StateName = localStorage.getItem("selectedState"); + const medium = selectedmedium; + const grade = selectedgrade; + const board = boardName; - const commonTypeInState = filterAndMapAssociations( - "subject", - subject, - store?.stateassociations, - "code" - ); - const commonTypeInBoard = filterAndMapAssociations( - "subject", - type, - boardAssociations, - "code" - ); - const commonTypeInMedium = filterAndMapAssociations( - "subject", - subject, - mediumAssociations, - "code" - ); - const commonTypeInGrade = filterAndMapAssociations( - "subject", - subject, - gradeAssociations, - "code" - ); - const commonTypeInType = filterAndMapAssociations( - "subject", - subject, - typeAssociations, - "code" - ); + if (StateName && medium && grade && board) { + console.log(StateName, medium, grade, board); - const findCommonAssociations = (array1: any[], array2: any[]) => { - return array1.filter((item1: { code: any }) => - array2.some((item2: { code: any }) => item1.code === item2.code) - ); - }; + const url = `/api/framework/v1/read/${FRAMEWORK_ID}`; + const boardData = await fetch(url).then((res) => res.json()); + const frameworks = boardData?.result?.framework; - const findOverallCommonSubjects = (arrays: any[]) => { - const nonEmptyArrays = arrays.filter( - (array: string | any[]) => array && array.length > 0 - ); + const getStates = getOptionsByCategory(frameworks, "state"); + const matchState = getStates.find( + (item: any) => + item?.name?.toLowerCase() === StateName?.toLocaleLowerCase() + ); - if (nonEmptyArrays.length === 0) return []; + const getBoards = getOptionsByCategory(frameworks, "board"); + console.log("getBoards", getBoards); + const matchBoard = getBoards.find((item: any) => item.name === board); + console.log("matchBoard", matchBoard); + const getMedium = getOptionsByCategory(frameworks, "medium"); + const matchMedium = getMedium.find((item: any) => item.name === medium); + + const getGrades = getOptionsByCategory(frameworks, "gradeLevel"); + const matchGrade = getGrades.find((item: any) => item.name === grade); + + const getCourseTypes = getOptionsByCategory(frameworks, "courseType"); + const courseTypes = getCourseTypes?.map((type: any) => type.name); + // setCourseTypes(courseTypes); + + const courseTypesAssociations = getCourseTypes?.map((type: any) => { + return { + code: type.code, + name: type.name, + associations: type.associations, + }; + }); + + const courseSubjectLists = courseTypesAssociations.map( + (courseType: any) => { + const commonAssociations = courseType?.associations.filter( + (assoc: any) => + matchState?.associations.filter( + (item: any) => item.code === assoc.code + )?.length && + matchBoard?.associations.filter( + (item: any) => item.code === assoc.code + )?.length && + matchMedium?.associations.filter( + (item: any) => item.code === assoc.code + )?.length && + matchGrade?.associations.filter( + (item: any) => item.code === assoc.code + )?.length + ); + console.log(commonAssociations); + const getSubjects = getOptionsByCategory(frameworks, "subject"); + const subjectAssociations = commonAssociations?.filter( + (assoc: any) => + getSubjects.map((item: any) => assoc.code === item?.code) + ); + console.log(subjectAssociations); + return { + courseTypeName: courseType?.name, + courseType: courseType?.code, + subjects: subjectAssociations?.map( + (subject: any) => subject?.name + ), + }; + } + ); + const matchedCourse = courseSubjectLists.find( + (course: any) => course.courseTypeName === type + ); - let commonSubjects = nonEmptyArrays[0]; + const matchingSubjects = matchedCourse ? matchedCourse.subjects : []; - for (let i = 1; i < nonEmptyArrays.length; i++) { - commonSubjects = findCommonAssociations( - commonSubjects, - nonEmptyArrays[i] + console.log(matchingSubjects); + setSubject(matchingSubjects); + localStorage.setItem( + "overallCommonSubjects", + JSON.stringify(matchingSubjects) ); - - if (commonSubjects.length === 0) return []; + // setSubjectLists(courseSubjectLists); } - - return commonSubjects; - }; - - const arrays = [ - commonTypeInState, - commonTypeInBoard, - commonTypeInMedium, - commonTypeInGrade, - commonTypeInType, - ]; - - const overallCommonSubjects = findOverallCommonSubjects(arrays); - - setSubject(overallCommonSubjects); - localStorage.setItem( - "overallCommonSubjects", - JSON.stringify(overallCommonSubjects) - ); + } catch (error) { + console.error("Error fetching board data:", error); + } }; useEffect(() => { @@ -647,11 +874,33 @@ const SubjectDetails = () => { align="center" sx={{ marginTop: "24px", color: "#6B7280" }} > - Select Medium, Grade, and Type - - )} - + {/* Left Section: Folder Icon and Subject Name */} + + + + {subj || "Untitled Subject"} + + + + )) + ) : ( + + Select Medium, Grade, and Type + + )} + + );