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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
import React, {useState, useEffect, useRef} from "react";
import {Route, Routes, useLocation, useNavigate} from "react-router-dom";
import Button from "components/button/Button.jsx";
import Submission from "components/submission/Submission.jsx";
import Post from "components/post/Post.jsx";
import {getSubreactFromLocation} from "helpers/Location.jsx";
import styles from "./Posts.css";
async function makePostsRequest(location, setPosts, setError, mounted) {
const init = {
method: "GET",
referrer: "same-origin",
}
const url = "/api/posts?" + new URLSearchParams({
page: 0,
amount: 25,
subreact: getSubreactFromLocation(location),
});
return fetch(url, init)
.then((response) => {
return response.json()
.catch(() => {
throw new Error("unexpected response from server")
})
})
.then((json) => {
if ("error" in json) {
throw new Error(json.error);
}
if (!mounted) {
return;
}
setPosts(json.posts);
})
.catch((error) => {
console.log(error);
if (!mounted) {
return;
}
setError(error.message);
});
}
function PostsError({navigate, error}) {
return (
<div className={styles.container + " " + styles.containerFade}>
<h1>Something's wrong!</h1>
<h2>Sorry, but an unexpected issue has forced us to stop early. Here are the technical details:</h2>
<h3>Error: {error}</h3>
<Button onClick={() => {navigate(0);}}>
Try again
</Button>
</div>
);
}
function PostsLoading() {
return (
<div className={styles.container + " " + styles.containerLoading}>
{[...Array(3)].map((_, i) =>
<Post placeholder key={i} />
)}
</div>
);
}
function PostsEmpty({navigate}) {
return (
<div className={styles.container + " " + styles.containerFade}>
<h1>There's nothing here?!?</h1>
<h2>Try changing subreacts.</h2>
<Button onClick={() => {navigate("/");}}>
Take me home
</Button>
</div>
);
}
function PostsStates({error, posts, navigate}) {
if (error !== null) { // Error message.
return (
<PostsError navigate={navigate} error={error} />
);
}
if (posts == null) {
return (
<PostsLoading />
)
}
if (posts.length == 0) {
return (
<PostsEmpty navigate={navigate} />
);
}
return (
posts.map(({uid, title, contents, time_updated, thumbnail, subreact}, i) =>
<Post key={i}
uid={uid}
title={title}
contents={contents}
timeUpdated={time_updated}
image={thumbnail}
subreact={subreact} />)
);
}
export default function Posts() {
const navigate = useNavigate();
const location = useLocation();
const locationRef = useRef(location);
const [posts, setPosts] = useState(null);
const [error, setError] = useState(null);
const [submissionActive, setSubmissionActive] = useState(false);
// We make a request if we're at the homepage or if we're changing subreacts.
const shouldMakeRequest = () => {
if (posts === null) {
return true;
}
const [previous, current] =
[locationRef.current, location].map((loc) => getSubreactFromLocation(loc));
return previous != current;
};
// This is a bit ugly, we're caching off our previous location so that we know
// if we should update or not.
useEffect(() => {
let mounted = true;
if (shouldMakeRequest()) {
setPosts(null);
makePostsRequest(location, setPosts, setError, mounted);
}
locationRef.current = location;
return () => {
setError(null);
mounted = false;
}
}, [location]);
return (
<div className={styles.container}>
<div className={styles.containerPadding}>
<div className={styles.postContainer + (submissionActive ? " " + styles.postContainerActive : "")}
onClick={(submissionActive ? () => {setSubmissionActive(false);} : null)}>
<div className={(submissionActive ? styles.blockEvents : "")}>
<PostsStates error={error} posts={posts} navigate={navigate}/>
</div>
</div>
</div>
<Submission active={submissionActive} setActive={setSubmissionActive} />
</div>
);
}
|