Public
HTTP
Forked from stevekrouse/reactClientDemo
stevekrouse-blobcommentsreact.web.val.run
July 3, 2024
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
/** @jsxImportSource https://esm.sh/react */
import { Hono } from "https://esm.sh/hono";
import React, { useEffect, useState } from "https://esm.sh/react";
import { hydrateRoot } from "https://esm.sh/react-dom/client";
import { renderToReadableStream } from "https://esm.sh/react-dom/server";
import { blob } from "https://esm.town/v/std/blob?v=10";
function App() {
const [comments, setComments] = useState();
const [newComment, setNewComment] = useState("");
useEffect(() => {
fetch("/comments")
.then(response => response.json())
.then(data => setComments(data));
}, []);
const handleSubmit = async (e) => {
e.preventDefault();
if (newComment.trim() !== "") {
const response = await fetch("/comments", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ comment: newComment }),
});
const data = await response.json();
setComments(data);
setNewComment("");
}
};
const handleDelete = async (id) => {
const response = await fetch(`/comments/${id}/delete`, {
method: "POST",
});
const data = await response.json();
setComments(data);
};
return (
<html>
<head>
<title>Comments</title>
<link rel="stylesheet" href="https://unpkg.com/tailwindcss@2.2.19/dist/tailwind.min.css" />
</head>
<body>
<div className="max-w-md mx-auto mt-8 p-4 bg-gray-100 rounded-lg">
<h1 className="text-2xl font-bold mb-4">Comments</h1>
<form onSubmit={handleSubmit} className="mb-4">
<div className="flex">
<input
type="text"
value={newComment}
onChange={(e) => setNewComment(e.target.value)}
placeholder="Add a comment..."
className="flex-grow mr-2 p-2 border rounded"
/>
<button type="submit" className="p-2 bg-blue-500 text-white rounded">Post</button>
</div>
</form>
<div className="space-y-2">
{comments
? comments.map((comment) => (
<div key={comment.id} className="bg-white p-2 rounded flex justify-between items-center">
{comment.text}
<button
onClick={() =>
handleDelete(comment.id)}
className="text-red-500 ml-2"
>
x
</button>
</div>
))
: "Loading..."}
</div>
</div>
</body>
</html>
);
}
if (typeof document !== "undefined") {
hydrateRoot(document, <App />);
}
const app = new Hono();
app.get("/", async (c) => {
const comments = await blob.getJSON("comments") || [];
const stream = await renderToReadableStream(<App />, { bootstrapModules: [import.meta.url] });
return new Response(stream, { headers: { "content-type": "text/html" } });
});
app.get("/comments", async (c) => {
const comments = await blob.getJSON("comments") || [];
return c.json(comments);
});
app.post("/comments", async (c) => {
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 3, 2024