-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArticlePage.js
62 lines (54 loc) · 1.84 KB
/
ArticlePage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { useState, useEffect } from "react";
import articleContent from "./article-content";
import NotFoundPage from "./NotFoundPage";
import ArticleList from "../components/ArticleList";
import CommentsList from "../components/CommentsList";
import UpvoteSection from "../components/UpvoteSection";
import AddCommentForm from "../components/AddCommentForm";
const ArticlePage = ({ match }) => {
//Consuming value from the URL and displaying the relative article from the article-content file
const name = match.params.name;
const displayArticle = articleContent.find(
(displayArticle) => displayArticle.name === name
);
const [articleInfo, setArticleInfo] = useState({ upvotes: 0, comments: [] });
//fetching from data
useEffect(() => {
//fecting data from server
const fetchData = async () => {
const result = await fetch(`/api/articles/${name}`);
const body = await result.json();
//console.log(body);
setArticleInfo(body);
};
fetchData();
}, [name]);
//if article does not exist
if (!displayArticle) return <NotFoundPage />;
//filter out the other articles to pass thorugh the props in articleList
const otherArticles = articleContent.filter(
(displayArticle) => displayArticle.name !== name
);
return (
<>
<h1>{displayArticle.title} </h1>
<br />
{displayArticle.content.map((item, key) => (
<p key={key}>{item}</p>
))}
<UpvoteSection
articleName={name}
upvotes={articleInfo.upvotes}
setArticleInfo={setArticleInfo}
/>
<div>
<CommentsList comments={articleInfo.comments} />
<AddCommentForm articleName={name} setArticleInfo={setArticleInfo} />
</div>
<hr />
<h3>Other Articles:</h3>
<ArticleList articlesprops={otherArticles} />
</>
);
};
export default ArticlePage;