Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
// This app creates a community bulletin board where users can post messages with titles and view all posts.
// Posts are automatically deleted after 24 hours. A basic spam filter is implemented to prevent abuse.
// The app uses SQLite for data storage and includes both server-side and client-side components.
/** @jsxImportSource https://esm.sh/react */
import React, { useEffect, useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
function App() {
const [posts, setPosts] = useState([]);
const [newTitle, setNewTitle] = useState("");
const [newPost, setNewPost] = useState("");
const [error, setError] = useState("");
useEffect(() => {
fetchPosts();
const interval = setInterval(fetchPosts, 30000); // Refresh every 30 seconds
return () => clearInterval(interval);
}, []);
const fetchPosts = async () => {
const response = await fetch("/posts");
const data = await response.json();
setPosts(data);
};
const handleSubmit = async (e) => {
e.preventDefault();
setError("");
const response = await fetch("/post", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ title: newTitle, content: newPost }),
});
if (response.ok) {
setNewTitle("");
setNewPost("");
fetchPosts();
} else {
const error = await response.text();
setError(error);
}
};
return (
<div className="container">
<h1>📰 Bulletin Board</h1>
<form onSubmit={handleSubmit}>
<input
type="text"
value={newTitle}
onChange={(e) => setNewTitle(e.target.value)}
placeholder="Title"
required
/>
<textarea
value={newPost}
onChange={(e) => setNewPost(e.target.value)}
placeholder="Write here..."
required
/>
<button type="submit">Post</button>
</form>
{error && <p className="error">{error}</p>}
<div className="posts">
{posts.map((post) => (
<div key={post.id} className="post">
<h2>{post.title}</h2>
<p className="content">{post.content}</p>
<small>{new Date(post.timestamp).toLocaleString()}</small>
</div>
))}
</div>
<footer>
<a href={import.meta.url.replace("esm.town", "val.town")}>View Source</a>
</footer>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") { client(); }
async function server(request: Request): Promise<Response> {
const { sqlite } = await import("https://esm.town/v/stevekrouse/sqlite");
const SCHEMA_VERSION = 1;
const KEY = new URL(import.meta.url).pathname.split("/").at(-1);
const TABLE_NAME = `${KEY}_posts_${SCHEMA_VERSION}`;
await sqlite.execute(`
CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
roramigator-bulletinboard.web.val.run
September 2, 2024