Skip to content

Commit

Permalink
Merge pull request #49 from je-poy/bugfix/fetch-collections
Browse files Browse the repository at this point in the history
Fixed asynchronously fetching of qoutes
  • Loading branch information
dkhd authored Oct 17, 2022
2 parents 8b61310 + 9c9dd6e commit 9e7c2c3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 91 deletions.
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 60 additions & 50 deletions src/components/quotes.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,68 @@
import React, { useState, useEffect } from "react";
import { Transition } from "@headlessui/react";

import { quotesCollection } from "../util/fetchquotes"

import { fetchQuotes } from "../util/fetchquotes";

function Quote(props) {
const [collections, setCollections] = useState([]);
const [quote, setQuote] = useState({});
const [quoteText, setQuoteText] = useState("");
const [quoteAuthor, setQuoteAuthor] = useState("");
const [authorActive, setAuthorActive] = useState(false);

const showAuthor = () => {
setAuthorActive(true);
};

const hideAuthor = () => {
setAuthorActive(false);
};

useEffect(() => {
fetchQuotes().then((data) => {
setCollections(data);
});
}, []);

useEffect(() => {
const generateQoute = () => {
if (collections.length < 1) return;
let random = Math.floor(Math.random() * collections.length);
setQuote(collections[random]);
};

generateQoute();

const quotesTimer = setInterval(generateQoute, 15 * 60 * 1000);

return () => {
clearInterval(quotesTimer);
};
}, [collections]);

useEffect(() => {
setQuoteText(quote.text);
setQuoteAuthor(quote.author);
}, [quote]);

const [quote, setQuote] = useState(quotesCollection[(Math.floor(Math.random() * quotesCollection.length))]);
const [quoteText, setQuoteText] = useState(quote.text);
const [quoteAuthor, setQuoteAuthor] = useState(quote.author);
const [authorActive, setAuthorActive] = useState(false)

const showAuthor = () => {
setAuthorActive(true);
}

const hideAuthor = () => {
setAuthorActive(false);
}

useEffect(() => {
const quotesTimer = setInterval(() => {
let random = Math.floor(Math.random() * quotesCollection.length);
setQuote(quotesCollection[random]);

}, 15*60*1000)
return () => {
clearInterval(quotesTimer);
}
}, []);

useEffect(() => {
setQuoteText(quote.text);
}, [quote]);

useEffect(() => {
setQuoteAuthor(quote.author);
}, [quote]);

return (
<div className="p-3" onMouseEnter={showAuthor} onMouseLeave={hideAuthor}>
<p className="font-bold text-lg z-10">{quoteText}</p>
<Transition
show={authorActive}
enter="transition-all duration-500"
enterFrom="opacity-75 -mt-5"
enterTo="opacity-100 mt-1.5"
leave="transition-all duration-250"
leaveFrom="opacity-100"
leaveTo="opacity-0">
<p className="text-gray-100 font-medium m-1.5">{quoteAuthor || "Anonymous"}</p>
</Transition>
</div>
);
return (
<div className="p-3" onMouseEnter={showAuthor} onMouseLeave={hideAuthor}>
<p className="font-bold text-lg z-10">{quoteText}</p>
<Transition
show={authorActive}
enter="transition-all duration-500"
enterFrom="opacity-75 -mt-5"
enterTo="opacity-100 mt-1.5"
leave="transition-all duration-250"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<p className="text-gray-100 font-medium m-1.5">
{quoteAuthor || "Anonymous"}
</p>
</Transition>
</div>
);
}

export default Quote;
export default Quote;
38 changes: 18 additions & 20 deletions src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React, { useEffect, useState } from "react";
import { Blurhash } from "react-blurhash";
import { SiMicrosoftbing } from "react-icons/si";
import { RiGoogleLine } from "react-icons/ri";
import { FullScreen, useFullScreenHandle } from "react-full-screen";
import { retrieveImage } from "../util/index";
import InfoIcon from "@mui/icons-material/Info";
Expand Down Expand Up @@ -38,14 +36,14 @@ function Home(props) {
useEffect(() => {
switch (showSearch) {
case "yahoo":
toggleBaseURL("https://search.yahoo.com")
toggleBaseURL("https://search.yahoo.com");
break;
case "bing":
toggleBaseURL("https://www.bing.com")
toggleBaseURL("https://www.bing.com");
break;

default:
toggleBaseURL("https://www.google.com")
toggleBaseURL("https://www.google.com");
break;
}
}, [showSearch]);
Expand Down Expand Up @@ -77,31 +75,31 @@ function Home(props) {
const engines = ["google", "bing", "yahoo"];
let idx = 0;

const engineIdx = engines.findIndex(e => showSearch === e);
const engineIdx = engines.findIndex((e) => showSearch === e);

if (engineIdx > -1) idx = engineIdx + 1;

if (idx > 2) idx = 0;

if (engineIdx > -1) idx = engineIdx+1;

if(idx > 2) idx = 0;

toggleSearch(engines[idx])
}
toggleSearch(engines[idx]);
};

const renderSearchIcon = (engine) => {
let cmp;
switch (showSearch) {
case "bing":
cmp = <BingLogo className="h-full w-full" />
cmp = <BingLogo className="h-full w-full" />;
break;
case "yahoo":
cmp = <YahooLogo className="h-full w-full" />
cmp = <YahooLogo className="h-full w-full" />;
break;

default:
cmp = <GoogleLogo className="h-full w-full" />
cmp = <GoogleLogo className="h-full w-full" />;
break;
}
return cmp
}
return cmp;
};

return (
<div>
Expand All @@ -123,7 +121,7 @@ function Home(props) {
<div className="flex absolute top-0 left-1/2 transform -translate-x-1/2 p-3 my-48">
<div className="grid grid-cols-12 grid-rows-1 rounded-md bg-opacity-40 bg-black overflow-hidden">
<div className="pl-4 col-span-2 h-14 w-full">
{ renderSearchIcon(showSearch) }
{renderSearchIcon(showSearch)}
</div>
<div className="col-span-10 h-full w-full">
<input
Expand Down
23 changes: 10 additions & 13 deletions src/util/fetchquotes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@

let quotesCollection = [];

fetch('https://type.fit/api/quotes', {
mode: 'cors'
})
const fetchQuotes = () => {
return fetch("https://type.fit/api/quotes", {
mode: "cors",
})
.then(function (res) {
return res.json();
})
.then(function (res) {
quotesCollection = res
return res.json();
})
.catch(function (err) {
console.log(err);
})
console.log(err);
return err;
});
};

export { quotesCollection };
export { fetchQuotes };

0 comments on commit 9e7c2c3

Please sign in to comment.