diff --git a/eduaid_web/src/pages/Home.jsx b/eduaid_web/src/pages/Home.jsx index e00a0d23..385d04c6 100644 --- a/eduaid_web/src/pages/Home.jsx +++ b/eduaid_web/src/pages/Home.jsx @@ -6,38 +6,18 @@ import arrow from "../assets/arrow.png"; import gitStar from "../assets/gitStar.png"; import { FaGithub } from "react-icons/fa"; import { Link } from "react-router-dom"; +import { getCachedGithubStars } from "../utils/githubStars"; const Home = () => { const [stars, setStars] = useState(null); const [error, setError] = useState(""); - async function fetchGitHubStars() { - const response = await fetch("https://api.github.com/repos/AOSSIE-Org/EduAid"); - if (!response.ok) throw new Error("Failed to fetch stars"); - const data = await response.json(); - return data.stargazers_count; - } - - function isMoreThanOneDayOld(timestamp) { - const oneDay = 24 * 60 * 60 * 1000; - return Date.now() - timestamp > oneDay; - } - useEffect(() => { - const storedStars = localStorage.getItem("stars"); - const storedTime = localStorage.getItem("fetchTime"); - - if (storedStars && storedTime && !isMoreThanOneDayOld(parseInt(storedTime))) { - setStars(parseInt(storedStars)); - } else { - fetchGitHubStars() - .then((starCount) => { - setStars(starCount); - localStorage.setItem("stars", starCount); - localStorage.setItem("fetchTime", Date.now().toString()); - }) - .catch(() => setError("Failed to fetch stars")); - } + getCachedGithubStars() + .then((starCount) => { + setStars(starCount); + }) + .catch(() => setError("Failed to fetch stars")); }, []); return ( diff --git a/eduaid_web/src/pages/Output.jsx b/eduaid_web/src/pages/Output.jsx index 03b4cbc8..a958a3fb 100644 --- a/eduaid_web/src/pages/Output.jsx +++ b/eduaid_web/src/pages/Output.jsx @@ -4,6 +4,7 @@ import logoPNG from "../assets/aossie_logo_transparent.png"; import { Link } from "react-router-dom"; import apiClient from "../utils/apiClient"; import { FiShuffle, FiEdit2, FiCheck, FiX } from "react-icons/fi"; +import { parseQaPairs, shuffleArray } from "../utils/quizUtils"; const Output = () => { const [qaPairs, setQaPairs] = useState([]); @@ -29,15 +30,6 @@ const Output = () => { return () => document.removeEventListener('mousedown', handleClickOutside); }, []); - function shuffleArray(array) { - const shuffledArray = [...array]; - for (let i = shuffledArray.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]]; - } - return shuffledArray; - } - const shuffledOptionsMap = useMemo(() => { return qaPairs.map((qaPair) => { const combinedOptions = qaPair.options @@ -91,70 +83,9 @@ const Output = () => { }; useEffect(() => { - const qaPairsFromStorage = - JSON.parse(localStorage.getItem("qaPairs")) || {}; - if (qaPairsFromStorage) { - const combinedQaPairs = []; - - if (qaPairsFromStorage["output_boolq"]) { - qaPairsFromStorage["output_boolq"]["Boolean_Questions"].forEach( - (question, index) => { - combinedQaPairs.push({ - question, - question_type: "Boolean", - context: qaPairsFromStorage["output_boolq"]["Text"], - }); - } - ); - } - - if (qaPairsFromStorage["output_mcq"]) { - qaPairsFromStorage["output_mcq"]["questions"].forEach((qaPair) => { - combinedQaPairs.push({ - question: qaPair.question_statement, - question_type: "MCQ", - options: qaPair.options, - answer: qaPair.answer, - context: qaPair.context, - }); - }); - } - - if (qaPairsFromStorage["output_mcq"] || questionType === "get_mcq") { - qaPairsFromStorage["output"].forEach((qaPair) => { - combinedQaPairs.push({ - question: qaPair.question_statement, - question_type: "MCQ", - options: qaPair.options, - answer: qaPair.answer, - context: qaPair.context, - }); - }); - } - - if (questionType == "get_boolq") { - qaPairsFromStorage["output"].forEach((qaPair) => { - combinedQaPairs.push({ - question: qaPair, - question_type: "Boolean", - }); - }); - } else if (qaPairsFromStorage["output"] && questionType !== "get_mcq") { - qaPairsFromStorage["output"].forEach((qaPair) => { - combinedQaPairs.push({ - question: - qaPair.question || qaPair.question_statement || qaPair.Question, - options: qaPair.options, - answer: qaPair.answer || qaPair.Answer, - context: qaPair.context, - question_type: "Short", - }); - }); - } - - setQaPairs(combinedQaPairs); - } - }, []); + const qaPairsFromStorage = JSON.parse(localStorage.getItem("qaPairs") || "{}"); + setQaPairs(parseQaPairs(qaPairsFromStorage, questionType)); + }, [questionType]); const generateGoogleForm = async () => { try { diff --git a/eduaid_web/src/pages/Question_Type.jsx b/eduaid_web/src/pages/Question_Type.jsx index 8cd586fb..a7b7514d 100644 --- a/eduaid_web/src/pages/Question_Type.jsx +++ b/eduaid_web/src/pages/Question_Type.jsx @@ -35,17 +35,14 @@ const Question_Type = () => { - + ))} {/* Action Button */}
Select a question type to continue.
+ )}