-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.tsx
138 lines (129 loc) · 3.3 KB
/
index.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
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
import { FC, useState } from "react";
import { render } from "react-dom";
import { TwirpError } from "twirpscript";
import { CurrentUser, Login } from "../protos/authentication.pb";
import { MakeHat, Hat, Size } from "../protos/haberdasher.pb";
import { client } from "twirpscript";
client.baseURL = "http://localhost:8080";
client.use((context, next) => {
const auth = localStorage.getItem("auth");
if (auth) {
context.headers["authorization"] = `bearer ${auth}`;
}
return next(context);
});
function formatHat(hat: Hat): string {
return `${hat.color} ${hat.name}, ${hat.inches} inches`;
}
const App: FC = () => {
const [size, setSize] = useState<Size>();
const [hats, setHats] = useState<Hat[]>([]);
const [error, setError] = useState<TwirpError>();
const [username, setUsername] = useState<string>();
const [password, setPassword] = useState<string>();
const [user, setUser] = useState<CurrentUser>();
async function login() {
if (username && password) {
try {
setError(undefined);
const currentUser = await Login({
username,
password,
});
setUser(currentUser);
localStorage.setItem("auth", currentUser.token);
setUsername(undefined);
setPassword(undefined);
} catch (e) {
setError(e);
}
}
}
function logout() {
setUser(undefined);
localStorage.setItem("auth", "");
}
async function makeHat() {
if (size) {
try {
setError(undefined);
const hat = await MakeHat(size);
setHats((hats) => [...hats, hat]);
setSize(undefined);
} catch (e) {
setError(e);
}
}
}
return (
<div>
<h1>Haberdasher </h1>
<h3>Current User: </h3>
{user ? (
<div>
<p>{user.username}</p>
<button onClick={logout}>Logout</button>
</div>
) : (
<div>
<form
onSubmit={(e) => {
login();
e.preventDefault();
}}
>
<label>
Username
<input
type="string"
placeholder="example"
onChange={(e) => setUsername(e.target.value)}
/>
</label>
<label>
Password
<input
type="password"
placeholder="1234"
onChange={(e) => setPassword(e.target.value)}
/>
</label>
<button type="submit" disabled={!username || !password}>
Login
</button>
</form>
</div>
)}
<h3>Hats: </h3>
<ul>
{hats.map((hat) => (
<li>{formatHat(hat)}</li>
))}
</ul>
<form
onSubmit={(e) => {
makeHat();
e.preventDefault();
}}
>
<input
type="number"
onChange={(e) => setSize({ inches: parseInt(e.target.value) })}
/>
<button type="submit" disabled={!size}>
Make Hat!
</button>
</form>
<h3>Errors: </h3>
{error ? (
<div>
<p>Code: {error.code}</p>
<p>Message: {error.msg}</p>
</div>
) : (
<p>No Errors</p>
)}
</div>
);
};
render(<App />, document.getElementById("app"));