-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommentsPage.re
executable file
·59 lines (53 loc) · 1.55 KB
/
CommentsPage.re
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
open Utils;
requireCSS("src/CommentsPage.css");
type state = {story_with_comments: option(StoryData.story_with_comments)};
type action =
| Loaded(StoryData.story_with_comments);
[@react.component]
let make = (~id) => {
let (state, dispatch) = React.useReducer((_state, action) =>
switch (action) {
| Loaded(data) =>{story_with_comments: Some(data)}
}, {story_with_comments: None});
let renderTitle = (story: StoryData.story_with_comments) => {
let title =
<h2 className="CommentsPage_title">
{React.string(story.title)}
</h2>;
<div>
{switch (story.url) {
| Some(url) =>
<a href=url className="CommentsPage_titleLink"> title </a>
| None => title
}}
</div>;
};
let renderByline = (story: StoryData.story_with_comments) =>
<div>
<span> {React.string(string_of_int(story.score))} </span>
{React.string(" points")}
<span>
<span>
{let time = story.time
let by = story.by
React.string({j| submitted $time by $by|j})}
</span>
</span>
</div>;
React.useEffect0(() => {
StoryData.fetchStoryWithComments(id, data => dispatch(Loaded(data)))
|> ignore;
None;
});
<div className="CommentsPage_container">
{switch (state.story_with_comments) {
| Some(story) =>
<div>
{renderTitle(story)}
{renderByline(story)}
<CommentList story />
</div>
| None => React.string("loading")
}}
</div>;
};