From adbc7e09920b96f4de65bc48ec3fd3b76f0f7e37 Mon Sep 17 00:00:00 2001 From: NancyAanchal Date: Wed, 31 Jul 2024 14:52:05 +0545 Subject: [PATCH 01/13] changes language according to dropdown --- nepalingo-web/src/App.tsx | 2 +- .../src/components/{ => header}/About.tsx | 2 +- nepalingo-web/src/components/randomQuotes.tsx | 4 +- nepalingo-web/src/hooks/Auth.tsx | 4 +- nepalingo-web/src/lib/getNewariWord.tsx | 4 +- nepalingo-web/src/pages/Home.tsx | 22 ++--- nepalingo-web/src/pages/TestYourself.tsx | 96 +++++++++++++++---- 7 files changed, 97 insertions(+), 37 deletions(-) rename nepalingo-web/src/components/{ => header}/About.tsx (99%) diff --git a/nepalingo-web/src/App.tsx b/nepalingo-web/src/App.tsx index 724c6ee..b26f8b2 100644 --- a/nepalingo-web/src/App.tsx +++ b/nepalingo-web/src/App.tsx @@ -5,7 +5,7 @@ import FlashcardPage from "@/pages/FlashcardPage"; import DictionaryPage from "@/pages/DictionaryPage"; import ResetPassword from "@/pages/ResetPassword"; import PasswordEmail from "@/pages/PasswordEmail"; -import About from "@/components/About"; +import About from "@/components/header/About"; import { BrowserRouter as Router, Route, Routes } from "react-router-dom"; import ReactGA from "react-ga4"; import { PrivateRoutes } from "@/components/PrivateRoutes"; diff --git a/nepalingo-web/src/components/About.tsx b/nepalingo-web/src/components/header/About.tsx similarity index 99% rename from nepalingo-web/src/components/About.tsx rename to nepalingo-web/src/components/header/About.tsx index 326b041..75b1846 100644 --- a/nepalingo-web/src/components/About.tsx +++ b/nepalingo-web/src/components/header/About.tsx @@ -1,5 +1,5 @@ import React from "react"; -import Header from "./header/Header"; +import Header from "./Header"; const teamMembers = [ { diff --git a/nepalingo-web/src/components/randomQuotes.tsx b/nepalingo-web/src/components/randomQuotes.tsx index a855cd6..fb03f06 100644 --- a/nepalingo-web/src/components/randomQuotes.tsx +++ b/nepalingo-web/src/components/randomQuotes.tsx @@ -1,6 +1,6 @@ import React from "react"; -import { useLanguage } from "../hooks/Langauge"; -import useQuotes from "../hooks/useQuotes"; +import { useLanguage } from "@/hooks/Langauge"; +import useQuotes from "@/hooks/useQuotes"; const RandomQuoteComponent: React.FC = () => { const { selectedLanguage } = useLanguage(); diff --git a/nepalingo-web/src/hooks/Auth.tsx b/nepalingo-web/src/hooks/Auth.tsx index 28e50b8..2b57cdb 100644 --- a/nepalingo-web/src/hooks/Auth.tsx +++ b/nepalingo-web/src/hooks/Auth.tsx @@ -15,7 +15,7 @@ type AuthContextProps = { signOut: () => void; signUp: (data: SignUpWithPasswordCredentials) => Promise; signIn: ( - data: SignUpWithPasswordCredentials + data: SignUpWithPasswordCredentials, ) => Promise; resetPasswordEmail: (email: string) => Promise<{ error: Error | null }>; resetPassword: (password: string) => Promise<{ error: Error | null }>; @@ -55,7 +55,7 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => { setSession(session); setUser(session?.user); setLoading(false); - } + }, ); setData(); diff --git a/nepalingo-web/src/lib/getNewariWord.tsx b/nepalingo-web/src/lib/getNewariWord.tsx index 1f16f12..e82cd9e 100644 --- a/nepalingo-web/src/lib/getNewariWord.tsx +++ b/nepalingo-web/src/lib/getNewariWord.tsx @@ -4,7 +4,7 @@ export async function getNewariWord(word: string): Promise { const api_endpoint = `/dict/en/search/${word}`; const data = await fetch( import.meta.env.VITE_NEPALBHASA_API_URL + api_endpoint, - {} + {}, ).then((r) => r.json()); if (data?.errors.length) { @@ -50,7 +50,7 @@ export async function getNewariWord(word: string): Promise { deva: meaning.transliterations?.deva, original: meaning.transliterations?.newa, }, - }) + }), ), }; return response; diff --git a/nepalingo-web/src/pages/Home.tsx b/nepalingo-web/src/pages/Home.tsx index 494aa82..f5ad6a5 100644 --- a/nepalingo-web/src/pages/Home.tsx +++ b/nepalingo-web/src/pages/Home.tsx @@ -51,6 +51,17 @@ const Home: React.FC = () => { }} /> +
+ { + navigate("/test-yourself"); + }} + /> +
{
-
- { - navigate("/test-yourself"); - }} - /> -
); diff --git a/nepalingo-web/src/pages/TestYourself.tsx b/nepalingo-web/src/pages/TestYourself.tsx index eb5676b..64dba57 100644 --- a/nepalingo-web/src/pages/TestYourself.tsx +++ b/nepalingo-web/src/pages/TestYourself.tsx @@ -4,10 +4,14 @@ import { generate } from "random-words"; import Header from "@/components/header/Header"; import Button from "@/components/Button"; import { useStreak } from "@/hooks/StreakContext"; +import { useLanguage } from "@/hooks/Langauge"; const TestYourself: React.FC = () => { const { updateStreak } = useStreak(); + const { selectedLanguage } = useLanguage(); const [word, setWord] = useState("today"); + const [index, setIndex] = useState(0); + const [options, setOptions] = useState([ "today", "rice", @@ -16,50 +20,106 @@ const TestYourself: React.FC = () => { ]); const [selectedOption, setSelectedOption] = useState(null); const [isCorrect, setIsCorrect] = useState(null); - const [isIncorrect, setIsIncorrect] = useState(null); - const { data, isLoading, error } = useDictionary({ - language: "newari", + const { data, isLoading } = useDictionary({ + language: selectedLanguage || "newar", word, }); useEffect(() => { updateStreak(); // Trigger streak update on flashcard page load - }, []); + }, [selectedLanguage]); const getOptions = (word: string) => { const randomWords = generate({ exactly: 3 }) as string[]; randomWords.push(word); - const shuffledOptions = randomWords.sort(() => Math.random() - 0.5); setOptions(shuffledOptions); }; - const handleNextQuestion = () => { - const NewWord = generate() as string; - setWord(NewWord); - setSelectedOption(null); - setIsCorrect(null); - getOptions(NewWord); - }; - if (error) { - handleNextQuestion(); + function getNextIndex(wordArray: Array) { + const newIndex = (index + 1) % wordArray.length; + return newIndex; } + const handleNextQuestion = async () => { + let wordArray: Array = []; + if (selectedLanguage === "Newari") { + wordArray = [ + "hello", + "call", + "can", + "do", + "how", + "I", + "my", + "what", + "where", + "a", + "do", + "not", + "for", + "from", + "fun", + "have", + "help", + "language", + "me", + "name", + "need", + "please", + "police", + "sick", + "speak", + "understand", + "well", + "you", + "your", + "salt", + ]; + } else if (selectedLanguage === "Tajpuriya") { + const wordText = await fetch("./dictionaries/TajpuriyaDictionary.csv") + .then((r) => r.text()) + .catch((error) => { + console.error("Error fetching words:", error); + }); + + if (wordText) { + wordArray = wordText.split("\n").map((line: string) => + line + .split(",")[0] + .trim() + .replace(/(^"|"$)/g, ""), + ); + } + } + + if (wordArray.length > 0) { + const newIndex = getNextIndex(wordArray); + setIndex(newIndex); + setWord(wordArray[newIndex]); + setSelectedOption(null); + setIsCorrect(null); + getOptions(wordArray[newIndex]); + } else { + console.error("Word array is empty"); + } + }; + const handleOptionSelect = (option: string) => { setSelectedOption(option); setIsCorrect(option === word); - setIsIncorrect(option !== word); }; - if (isLoading) + if (isLoading) { return (
Loading...
); + } const meaning = data && data.meanings[0]; - const NewariWord = meaning?.meaningOriginal || ""; + const displayedWord = meaning?.meaningOriginal || ""; return (
@@ -69,7 +129,7 @@ const TestYourself: React.FC = () => {

What is this word in English?

-

{NewariWord}

+

{displayedWord}

{options.map((option, index) => (