diff --git a/package-lock.json b/package-lock.json index 384ba66..634daef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", + "html-to-react": "^1.5.1", "javascript-time-ago": "^2.5.9", "react": "^18.2.0", "react-dom": "^18.2.0", @@ -8757,6 +8758,86 @@ "node": ">=12" } }, + "node_modules/html-to-react": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/html-to-react/-/html-to-react-1.5.1.tgz", + "integrity": "sha512-dFLZRBjpMk89Ukwa6Fq7oApinn3TEZD0gGFUkmI9DqNQxTjN7gF9owhyu+t8h+bpEZrX2DMxZLYjEfw0C/iL7A==", + "dependencies": { + "domhandler": "^5.0", + "htmlparser2": "^8.0", + "lodash.camelcase": "^4.3.0", + "react": "^18.0" + } + }, + "node_modules/html-to-react/node_modules/dom-serializer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-2.0.0.tgz", + "integrity": "sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==", + "dependencies": { + "domelementtype": "^2.3.0", + "domhandler": "^5.0.2", + "entities": "^4.2.0" + }, + "funding": { + "url": "https://github.com/cheeriojs/dom-serializer?sponsor=1" + } + }, + "node_modules/html-to-react/node_modules/domhandler": { + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-5.0.3.tgz", + "integrity": "sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==", + "dependencies": { + "domelementtype": "^2.3.0" + }, + "engines": { + "node": ">= 4" + }, + "funding": { + "url": "https://github.com/fb55/domhandler?sponsor=1" + } + }, + "node_modules/html-to-react/node_modules/domutils": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/domutils/-/domutils-3.0.1.tgz", + "integrity": "sha512-z08c1l761iKhDFtfXO04C7kTdPBLi41zwOZl00WS8b5eiaebNpY00HKbztwBq+e3vyqWNwWF3mP9YLUeqIrF+Q==", + "dependencies": { + "dom-serializer": "^2.0.0", + "domelementtype": "^2.3.0", + "domhandler": "^5.0.1" + }, + "funding": { + "url": "https://github.com/fb55/domutils?sponsor=1" + } + }, + "node_modules/html-to-react/node_modules/entities": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz", + "integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==", + "engines": { + "node": ">=0.12" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, + "node_modules/html-to-react/node_modules/htmlparser2": { + "version": "8.0.2", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-8.0.2.tgz", + "integrity": "sha512-GYdjWKDkbRLkZ5geuHs5NY1puJ+PXwP7+fHPRz06Eirsb9ugf6d8kkXav6ADhcODhFFPMIXyxkxSuMf3D6NCFA==", + "funding": [ + "https://github.com/fb55/htmlparser2?sponsor=1", + { + "type": "github", + "url": "https://github.com/sponsors/fb55" + } + ], + "dependencies": { + "domelementtype": "^2.3.0", + "domhandler": "^5.0.3", + "domutils": "^3.0.1", + "entities": "^4.4.0" + } + }, "node_modules/html-webpack-plugin": { "version": "5.5.1", "resolved": "https://registry.npmjs.org/html-webpack-plugin/-/html-webpack-plugin-5.5.1.tgz", @@ -11773,6 +11854,11 @@ "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" }, + "node_modules/lodash.camelcase": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", + "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" + }, "node_modules/lodash.debounce": { "version": "4.0.8", "resolved": "https://registry.npmjs.org/lodash.debounce/-/lodash.debounce-4.0.8.tgz", diff --git a/package.json b/package.json index 6b5ee99..5f014ea 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", + "html-to-react": "^1.5.1", "javascript-time-ago": "^2.5.9", "react": "^18.2.0", "react-dom": "^18.2.0", diff --git a/src/App.css b/src/App.css index 73839ac..d63cf42 100644 --- a/src/App.css +++ b/src/App.css @@ -515,3 +515,49 @@ nav ul li a:hover { .Footer-list ul li a:hover { color: var(--accent); } + +.Comment { + flex-direction: column; + padding: 1.5rem; + max-width: 800px; + align-items: start; + color: var(--text); +} + +/* .Comment * { */ +/* } */ + +.Comment a { + color: var(--accent); + text-decoration: none; + transition: all 0.2s; +} + +.Comment a:hover { + color: var(--accent_light); + text-decoration: underline; +} + +.Comment__title { + color: var(--accent); + margin-top: 1.5rem; +} + +.btn--back { + position: fixed; + top: 5.5rem; + left: 3rem; + border: 2px solid var(--accent_border); + padding: 0.5rem 2rem; + border-radius: 1rem; + cursor: pointer; + color: var(--text); + font-weight: 600; + font-family: inherit; +} + +.btn--back:hover { + background: var(--accent_light); + color: var(--text_light); + border: 3px solid var(--text_light); +} diff --git a/src/App.jsx b/src/App.jsx index a24f99a..15160cb 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?hitsPerPage=1000&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 (
+

+ {fetchedComments.hits[0].story_title} +

+ {currentItems.map((comment, index) => ( + ))} - - - - ); + + + ); + } else { + return ( + <> +
    + {currentItems.map((item, index) => ( +
    + ))} +
+ + + ); + } } else { return ; } diff --git a/src/components/Search.jsx b/src/components/Search.jsx index 9050155..e3b7338 100644 --- a/src/components/Search.jsx +++ b/src/components/Search.jsx @@ -1,6 +1,6 @@ import { useState } from "react"; -export default function Search({ setQuery, setLoading }) { +export default function Search({ setQuery, setLoading, setObjectID }) { const [inputText, setInputText] = useState(""); let inputHandler = (e) => { @@ -15,6 +15,7 @@ export default function Search({ setQuery, setLoading }) { e.preventDefault(); setLoading(true); setQuery(inputText); + setObjectID(""); setInputText(""); };