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 application helps users write detailed reviews of coffee shops. It fetches coffee shop data
* from the OpenStreetMap Nominatim API, allows users to add custom details, and stores the augmented
* information in a SQLite database. The app provides a user interface to view, add, and edit coffee shop reviews.
*
* It uses React for the frontend, the Nominatim API for initial coffee shop data,
* and Val Town's SQLite for data persistence.
*/
/** @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 [coffeeShops, setCoffeeShops] = useState([]);
const [reviews, setReviews] = useState([]);
const [searchTerm, setSearchTerm] = useState("");
const [selectedShop, setSelectedShop] = useState(null);
const [reviewForm, setReviewForm] = useState({
coffeeMachineBrand: "",
latteArtConsistency: 0,
roastOwnBeans: false,
additionalNotes: "",
});
useEffect(() => {
fetchReviews();
}, []);
const fetchCoffeeShops = async () => {
try {
const response = await fetch(`/api/coffee-shops?search=${encodeURIComponent(searchTerm)}`);
if (!response.ok) throw new Error("Failed to fetch coffee shops");
const data = await response.json();
setCoffeeShops(data);
} catch (error) {
console.error("Error fetching coffee shops:", error);
}
};
const fetchReviews = async () => {
try {
const response = await fetch("/api/reviews");
if (!response.ok) throw new Error("Failed to fetch reviews");
const data = await response.json();
setReviews(data);
} catch (error) {
console.error("Error fetching reviews:", error);
}
};
const handleSubmitReview = async (e) => {
e.preventDefault();
if (!selectedShop) return;
const reviewData = {
...selectedShop,
...reviewForm,
};
try {
const response = await fetch("/api/reviews", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(reviewData),
});
if (!response.ok) throw new Error("Failed to submit review");
setSelectedShop(null);
setReviewForm({
coffeeMachineBrand: "",
latteArtConsistency: 0,
roastOwnBeans: false,
additionalNotes: "",
});
fetchReviews();
} catch (error) {
console.error("Error submitting review:", error);
}
};
return (
<div className="container">
<h1>Coffee Shop Reviewer</h1>
<div className="search-section">
<input
type="text"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
placeholder="Search for coffee shops"
/>
<button onClick={fetchCoffeeShops}>Search</button>
</div>
<div className="results-section">
<h2>Search Results</h2>
<ul>
{coffeeShops.map((shop) => (
<li key={shop.id} onClick={() => setSelectedShop(shop)}>
{shop.name} - {shop.address}, {shop.city}
gr8gatsby-hungrywhiteleopon.web.val.run
August 27, 2024