-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArticle.jsx
109 lines (103 loc) · 2.91 KB
/
Article.jsx
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { formatDate, wordCount, capitaliseFirstLetter } from "./utils";
import LoadingSpinner from "./LoadingSpinner";
import { getArticle, updateVoteForArticle } from "./apiFunctions";
import Comment from "./Comment";
import { useContext } from "react";
import { UserContext } from "./contexts/UserContext";
const Article = ({
setArticleWordCount,
setNumItems,
setCommentPageNumber,
commentPageNumber,
}) => {
const { articleid } = useParams();
const { user } = useContext(UserContext);
const [article, setArticle] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [commentCount, setCommentCount] = useState(null);
const [articleVotes, setArticleVotes] = useState(0);
const [articleFetchError, setArticleFetchError] = useState(null);
useEffect(() => {
setIsLoading(true);
setNumItems(null);
setCommentPageNumber(1);
setArticleWordCount(null);
setArticleFetchError(null);
getArticle(articleid)
.then((article) => {
setArticle(article);
setCommentCount(article.comment_count);
setArticleVotes(article.votes);
setArticleWordCount(wordCount(article.body));
setIsLoading(false);
})
.catch((error) => {
setArticleFetchError(error.response.data.msg);
setIsLoading(false);
});
}, [articleid]);
const adjustVote = (inc) => {
setArticleVotes((currentVotes) => currentVotes + inc);
updateVoteForArticle(articleid, inc).catch(() => {
setArticleVotes((currentVotes) => currentVotes - inc);
});
};
const isUserAuthor = () => article.author === user.username;
return isLoading ? (
<div>
<p id="articleLoadingText">Loading Article</p>
<LoadingSpinner />
</div>
) : articleFetchError ? (
<div>
<p id="articleLoadingText">
{capitaliseFirstLetter(articleFetchError)}
</p>
</div>
) : (
<article id="articleView">
<img
id="articleViewImage"
src={article.article_img_url}
alt={article.title}
/>
<h2 id="articleViewTitle">{article.title}</h2>
<p id="articleViewAuthor">{article.author}</p>
<p id="articleViewDate">{formatDate(article.created_at)}</p>
<div id="articleViewVotes">{articleVotes} likes</div>
<div id="articleViewVoteControls">
{user && (
<div>
<button
className="voteButton"
id="voteUp"
disabled={isUserAuthor() ? true : false}
onClick={() => adjustVote(1)}
>
ᐱ
</button>
<button
className="voteButton"
id="voteDown"
disabled={isUserAuthor() ? true : false}
onClick={() => adjustVote(-1)}
>
ᐯ
</button>
</div>
)}
</div>
<p id="articleViewBody">{article.body}</p>
<Comment
articleid={articleid}
commentCount={commentCount}
setNumItems={setNumItems}
setCommentCount={setCommentCount}
commentPageNumber={commentPageNumber}
/>
</article>
);
};
export default Article;