Skip to content

Commit

Permalink
Merge branch 'main' into christikaes-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
binamkayastha authored Aug 4, 2024
2 parents 1939fc7 + ed54b52 commit a1e299d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 53 deletions.
11 changes: 1 addition & 10 deletions nepalingo-web/src/components/randomQuotes.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import React from "react";
import { useLanguage } from "@/hooks/Langauge";
import useQuotes from "@/hooks/useQuotes";

const RandomQuoteComponent: React.FC = () => {
const { selectedLanguage } = useLanguage();

if (!selectedLanguage) {
return <div>Please select a language.</div>;
}

const { randomQuote } = useQuotes({
language: selectedLanguage,
});
const { randomQuote } = useQuotes();

if (!randomQuote) {
return <div>Loading...</div>;
Expand Down
78 changes: 44 additions & 34 deletions nepalingo-web/src/hooks/useQuotes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState, useEffect } from "react";
import { useLanguage } from "./Langauge";

interface Quote {
text: string;
Expand All @@ -9,19 +10,52 @@ export interface QuotesResponse {
randomQuote: Quote | null;
}

interface useQuotesProps {
language: string;
}

const useQuotes = ({ language }: useQuotesProps): QuotesResponse => {
const useQuotes = (): QuotesResponse => {
const [quotes, setQuotes] = useState<Quote[]>([]);
const [quotesText, setQuotesText] = useState("");
const [randomQuote, setRandomQuote] = useState<Quote | null>(null);
const { selectedLanguage } = useLanguage();

function parse(row: string) {
let insideQuote = false,
entry: Array<string> = [];
const entries = [];
row.split("").forEach(function (character) {
if (character === '"') {
insideQuote = !insideQuote;
} else {
if (character == "," && !insideQuote) {
entries.push(entry.join(""));
entry = [];
} else {
entry.push(character);
}
}
});
entries.push(entry.join(""));
return entries;
}
const loadQuotes = (quotesText: string) => {
const quotesArray = quotesText.split(/\r\n|\n/).map((line: string) => {
console.log(line);
const [quote, englishTranslation] = parse(line);
return {
text: quote ? quote.trim().replace(/(^"|"$)/g, "") : "",
translation: englishTranslation
? englishTranslation.trim().replace(/(^"|"$)/g, "")
: "",
};
});

const randomIndex = Math.floor(Math.random() * quotesArray.length);
const randomQuote = quotesArray[randomIndex];

setQuotes(quotesArray);
setRandomQuote(randomQuote);
};

useEffect(() => {
let sourceFile = "";

switch (language) {
switch (selectedLanguage) {
case "Newari":
sourceFile = "/quotes/newari.csv";
break;
Expand All @@ -39,37 +73,13 @@ const useQuotes = ({ language }: useQuotesProps): QuotesResponse => {
fetch(sourceFile)
.then((r) => r.text())
.then((text) => {
setQuotesText(text);
loadQuotes(text);
})
.catch((error) => {
console.error("Error fetching quotes:", error);
});
}
}, [language]);

useEffect(() => {
if (quotesText) {
const loadQuotes = () => {
const quotesArray = quotesText.split("\n").map((line: string) => {
const [quote, englishTranslation] = line.split(",");
return {
text: quote ? quote.trim().replace(/(^"|"$)/g, "") : "",
translation: englishTranslation
? englishTranslation.trim().replace(/(^"|"$)/g, "")
: "",
};
});

const randomIndex = Math.floor(Math.random() * quotesArray.length);
const randomQuote = quotesArray[randomIndex];

setQuotes(quotesArray);
setRandomQuote(randomQuote);
};

loadQuotes();
}
}, [quotesText]);
}, [selectedLanguage]);

return { quotes, randomQuote };
};
Expand Down
12 changes: 3 additions & 9 deletions nepalingo-web/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,9 @@ const Home: React.FC = () => {
</div>

<div className="mb-5 pt-5">
<ActivityCard
backgroundImageUrl="/CardOverlayBlur.png"
quizYourselfText="Random Quote!"
descriptionText={`Here's a random quote!`}
>
<div className="flex justify-center">
<RandomQuoteComponent />
</div>
</ActivityCard>
<div className="flex justify-center">
<RandomQuoteComponent />
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit a1e299d

Please sign in to comment.