MCPcopy Create free account
hub / github.com/cloudflare/templates / App

Function App

react-postgres-fullstack-template/src/App.jsx:10–168  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

8import MockDataBanner from "./components/MockDataBanner";
9
10function App() {
11 const navigate = useNavigate();
12 const params = useParams();
13 const [bookDetail, setBookDetail] = useState(null);
14 const [loading, setLoading] = useState(false);
15 const [genres, setGenres] = useState([]);
16 const [dataSource, setDataSource] = useState(null);
17
18 // Get route parameters
19 const { bookId } = params;
20 const { genreId } = params;
21 const activeGenre = genreId ? decodeURIComponent(genreId) : null;
22
23 // Load genres for sidebar
24 useEffect(() => {
25 const loadGenres = async () => {
26 try {
27 const response = await fetch("/api/books");
28 if (!response.ok) {
29 throw new Error(`API returned status: ${response.status}`);
30 }
31 const data = await response.json();
32
33 if (!data.books?.length) {
34 console.error("No books data found:", typeof data);
35 return;
36 }
37
38 const booksArray = data.books;
39
40 // Check if using mock data or database
41 if (data.source) {
42 setDataSource(data.source);
43 }
44
45 const genreGroups = groupByGenre(booksArray);
46 setGenres(genreGroups);
47 } catch (error) {
48 console.error("Error loading genres:", error);
49 }
50 };
51
52 loadGenres();
53 }, []);
54
55 // Load book details when a book is selected via URL
56 useEffect(() => {
57 if (!bookId) return;
58
59 const fetchBookDetail = async () => {
60 setLoading(true);
61 try {
62 // First get basic book details
63 const bookResponse = await fetch(`/api/books/${bookId}`);
64
65 if (!bookResponse.ok) {
66 throw new Error(`API returned status: ${bookResponse.status}`);
67 }

Callers

nothing calls this directly

Calls 3

loadGenresFunction · 0.85
fetchBookDetailFunction · 0.85
handleSelectGenreFunction · 0.85

Tested by

no test coverage detected