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}