diff --git a/src/App.jsx b/src/App.jsx
index a24f99a..14b1700 100644
--- a/src/App.jsx
+++ b/src/App.jsx
@@ -7,19 +7,21 @@ import Reagan from "./components/Reagan";
function App() {
const [fetchedData, setFetchedData] = useState([]);
+ const [fetchedComments, setFetchedComments] = useState([]);
const [query, setQuery] = useState("");
+ const [objectID, setObjectID] = useState("");
const [hitsPerPage, setHitsPerPage] = useState(1000);
const [pageNum, setPageNum] = useState(0);
const [isLoadingFront, setIsLoadingFront] = useState(true);
const [loading, setLoading] = useState(true);
const [sortBy, setSortBy] = useState("");
- const fetchData = async function (url) {
+ const fetchData = async function (url, loadingData = true) {
try {
const res = await fetch(url);
const data = await res.json();
setLoading(false);
- setFetchedData(data);
+ loadingData ? setFetchedData(data) : setFetchedComments(data);
} catch (err) {
console.error(err);
}
@@ -37,6 +39,13 @@ function App() {
);
};
+ const fetchComments = (objectID) => {
+ fetchData(
+ `http://hn.algolia.com/api/v1/search?tags=comment,story_${objectID}`,
+ false
+ );
+ };
+
useEffect(() => {
if (isLoadingFront) return;
if (query) fetchByQuery(query, hitsPerPage, pageNum);
@@ -49,12 +58,19 @@ function App() {
setIsLoadingFront(false);
}, [isLoadingFront]);
+ // fetch Comments
+ useEffect(() => {
+ if (isLoadingFront) return;
+ fetchComments(objectID);
+ }, [objectID]);
+
return (
diff --git a/src/components/Article.jsx b/src/components/Article.jsx
index 12f3441..08455a2 100644
--- a/src/components/Article.jsx
+++ b/src/components/Article.jsx
@@ -12,6 +12,7 @@ export default function Article({
id,
setHits,
query,
+ setObjectID,
}) {
const [displayedPoints, setDisplayedPoints] = useState(points);
const [arePointsIncreased, setArePointsIncreased] = useState(false);
@@ -68,6 +69,8 @@ export default function Article({
const urlText = url?.split("/")[2];
+ const handleFetchComments = () => setObjectID(id);
+
return (
{articleNum}.
@@ -104,7 +107,10 @@ export default function Article({
hide
{" "}
- | {comments} Comments
+ |{" "}
+
+ {comments} Comments
+