Skip to content

Commit

Permalink
Merge pull request #33 from mrkyns/feature/comments
Browse files Browse the repository at this point in the history
Feature/comments
  • Loading branch information
AdamN89 authored Apr 23, 2023
2 parents 4657823 + f4741fb commit 2f4429a
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 43 deletions.
86 changes: 86 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 46 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
23 changes: 21 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -49,12 +58,19 @@ function App() {
setIsLoadingFront(false);
}, [isLoadingFront]);

// fetch Comments
useEffect(() => {
if (isLoadingFront) return;
fetchComments(objectID);
}, [objectID]);

return (
<div className="App">
<Header
setIsLoadingFront={setIsLoadingFront}
fetchedData={fetchedData}
setSortBy={setSortBy}
setObjectID={setObjectID}
/>

<Body
Expand All @@ -68,6 +84,9 @@ function App() {
filterBy={sortBy}
query={query}
setLoading={setLoading}
setObjectID={setObjectID}
objectID={objectID}
fetchedComments={fetchedComments}
/>
<Footer />
<Reagan />
Expand Down
12 changes: 11 additions & 1 deletion src/components/Article.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default function Article({
id,
setHits,
query,
setObjectID,
setLoading,
}) {
const [displayedPoints, setDisplayedPoints] = useState(points);
const [arePointsIncreased, setArePointsIncreased] = useState(false);
Expand Down Expand Up @@ -68,6 +70,11 @@ export default function Article({

const urlText = url?.split("/")[2];

const handleFetchComments = () => {
setObjectID(id);
setLoading(true);
};

return (
<li className="Article__wrapper">
<span className="Article__num">{articleNum}.</span>
Expand Down Expand Up @@ -104,7 +111,10 @@ export default function Article({
<span className="Article__hide" onClick={hide}>
hide
</span>{" "}
| <span>{comments}</span> Comments
|{" "}
<span className="Article__hide" onClick={handleFetchComments}>
{comments} Comments
</span>
</p>
</div>
<button className="Article__btn-up" onClick={increasePoints}>
Expand Down
10 changes: 9 additions & 1 deletion src/components/Body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default function Body({
query,
sortBy,
setLoading,
fetchedComments,
setObjectID,
objectID,
}) {
const [hits, setHits] = useState([]);

Expand Down Expand Up @@ -41,6 +44,7 @@ export default function Body({
fetchedData={fetchedData}
setQuery={setQuery}
setLoading={setLoading}
setObjectID={setObjectID}
/>
<ClimbingBoxLoader
color="var(--accent)"
Expand All @@ -53,9 +57,13 @@ export default function Body({
<div className="pagination">
<Pagination
setHits={setHits}
itemsPerPage={20}
itemsPerPage={objectID ? 6 : 20}
hits={hits}
query={query}
setObjectID={setObjectID}
objectID={objectID}
fetchedComments={fetchedComments}
setLoading={setLoading}
/>
</div>
)}
Expand Down
13 changes: 13 additions & 0 deletions src/components/Comments.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Parser } from "html-to-react";

export default function Comments({ author, text }) {
const parser = new Parser();
return (
<div className="Article__wrapper Comment">
<h3>
by <span className="Article__title">{author}</span>
</h3>
{parser.parse(text)}
</div>
);
}
11 changes: 8 additions & 3 deletions src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export default function Header({ setIsLoadingFront, setSortBy }) {
export default function Header({ setIsLoadingFront, setSortBy, setObjectID }) {
const handleFrontLoad = () => {
setIsLoadingFront(true);
setObjectID("");
};

return (
<div className="Header">
<div className="navigation">
<svg
onClick={() => setIsLoadingFront(true)}
onClick={handleFrontLoad}
version="1.1"
id="Layer_1"
xmlns="http://www.w3.org/2000/svg"
Expand All @@ -19,7 +24,7 @@ export default function Header({ setIsLoadingFront, setSortBy }) {
</g>
</svg>
<nav>
<span onClick={() => setIsLoadingFront(true)}>Hacker News</span>
<span onClick={handleFrontLoad}>Hacker News</span>
<ul>
<li>
<a href="#" onClick={() => setSortBy("created_at_i")}>
Expand Down
Loading

0 comments on commit 2f4429a

Please sign in to comment.