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 */}
{selectedOption ? ( - - + + Fire Up πŸš€ ) : ( )} + {!selectedOption && ( +

Select a question type to continue.

+ )}
diff --git a/eduaid_web/src/pages/Text_Input.jsx b/eduaid_web/src/pages/Text_Input.jsx index e341d331..f1c37648 100644 --- a/eduaid_web/src/pages/Text_Input.jsx +++ b/eduaid_web/src/pages/Text_Input.jsx @@ -7,6 +7,7 @@ import { FaClipboard } from "react-icons/fa"; import Switch from "react-switch"; import { Link,useNavigate } from "react-router-dom"; import apiClient from "../utils/apiClient"; +import { getEndpointForQuestionType, saveQuizHistory } from "../utils/quizUtils"; const Text_Input = () => { const navigate = useNavigate(); @@ -73,6 +74,8 @@ const Text_Input = () => { difficulty, localStorage.getItem("selectedQuestionType") ); + } else { + setLoading(false); } }; @@ -88,19 +91,8 @@ const Text_Input = () => { setNumQuestions((prev) => (prev > 0 ? prev - 1 : 0)); }; - const getEndpoint = (difficulty, questionType) => { - if (difficulty !== "Easy Difficulty") { - if (questionType === "get_shortq") { - return "get_shortq_hard"; - } else if (questionType === "get_mcq") { - return "get_mcq_hard"; - } - } - return questionType; - }; - const sendToBackend = async (data, difficulty, questionType) => { - const endpoint = getEndpoint(difficulty, questionType); + const endpoint = getEndpointForQuestionType(difficulty, questionType); try { const requestData = { input_text: data, @@ -110,22 +102,7 @@ const Text_Input = () => { const responseData = await apiClient.post(`/${endpoint}`, requestData); localStorage.setItem("qaPairs", JSON.stringify(responseData)); - - // Save quiz details to local storage - const quizDetails = { - difficulty, - numQuestions, - date: new Date().toLocaleDateString(), - qaPair: responseData, - }; - - let last5Quizzes = - JSON.parse(localStorage.getItem("last5Quizzes")) || []; - last5Quizzes.push(quizDetails); - if (last5Quizzes.length > 5) { - last5Quizzes.shift(); // Keep only the last 5 quizzes - } - localStorage.setItem("last5Quizzes", JSON.stringify(last5Quizzes)); + saveQuizHistory({ difficulty, numQuestions, qaPair: responseData }); navigate("/output"); } catch (error) { @@ -167,10 +144,11 @@ const Text_Input = () => { {/* Textarea */}
- +