-
Notifications
You must be signed in to change notification settings - Fork 3
/
ViewPage.tsx
65 lines (57 loc) · 1.59 KB
/
ViewPage.tsx
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
import React from "react";
import { Link, useLocation } from "react-router-dom";
import FooApi from "../foo/api/FooApi";
import Layout from "./Layout";
interface ViewPageProps {}
function useQuery() {
return new URLSearchParams(useLocation().search);
}
const ViewPage: React.FC<ViewPageProps> = (props) => {
const [isLoading, setIsLoading] = React.useState(true);
const [isError, setIsError] = React.useState(false);
const [message, setMessage] = React.useState("no message");
const query = useQuery();
const tag = query.get("tag");
React.useEffect(() => {
if (tag) {
FooApi.getMessageFromTag(tag)
.then((r) => {
setIsLoading(false);
setMessage(r.payload["message"]);
})
.catch((r) => {
setIsLoading(false);
setIsError(true);
});
} else {
setIsLoading(false);
setIsError(true);
}
}, [tag]);
let messageElement = null;
if (isError) {
messageElement = (
<div className="text-red-500">Error: Unable to load message!</div>
);
} else {
if (isLoading) {
messageElement = <div className="text-xl text-gray-500">Loading...</div>;
} else {
messageElement = (
<div className="text-xl font-bold text-gray-800">{message}</div>
);
}
}
return (
<Layout>
{messageElement}
<div className="border-t pt-4 flex mt-6 justify-between text-sm text-gray-500">
<div className="font-mono">{tag}</div>
<Link className="text-blue-500" to="/">
Back
</Link>
</div>
</Layout>
);
};
export default ViewPage;