diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 9d045e1..20e5f1f 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,11 +1,9 @@ -import { useState } from "react"; import reactLogo from "./assets/react.svg"; import viteLogo from "/vite.svg"; import "./App.css"; +import { CountButton } from "./Components/CountButton"; function App(): JSX.Element { - const [count, setCount] = useState(0); - return ( <>
@@ -18,13 +16,7 @@ function App(): JSX.Element {

Vite + React

- +

Edit src/App.tsx and save to test HMR

diff --git a/frontend/src/Components/CountButton.tsx b/frontend/src/Components/CountButton.tsx new file mode 100644 index 0000000..cce55af --- /dev/null +++ b/frontend/src/Components/CountButton.tsx @@ -0,0 +1,39 @@ +import { useState, useEffect } from "react"; + +// Interface for the response from the server +// This should eventually reside in a dedicated file +interface CountResponse { + pressCount: number; +} + +// Some constants for the button +const BUTTON_ENDPOINT = "/api/button"; + +// A simple button that counts how many times it's been pressed +export function CountButton(): JSX.Element { + const [count, setCount] = useState(NaN); + + // useEffect with a [] dependency array runs only once + useEffect(() => { + async function getCount(): Promise { + const response = await fetch(BUTTON_ENDPOINT); + const data = (await response.json()) as CountResponse; + setCount(data.pressCount); + } + void getCount(); + }, []); + + // This is what runs on every button click + function press(): void { + async function pressPost(): Promise { + await fetch(BUTTON_ENDPOINT, { method: "POST" }); + const response = await fetch(BUTTON_ENDPOINT); + const data = (await response.json()) as CountResponse; + setCount(data.pressCount); + } + void pressPost(); + } + + // Return some JSX with the button and associated handler + return ; +} diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index d366e8c..f8ec812 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -4,4 +4,19 @@ import react from "@vitejs/plugin-react-swc"; // https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], + // build: { + // outDir: '../server/public' // Override default outDir('dist') + // }, + server: { + port: 3000, + open: true, + proxy: { + "/api": { + target: "http://localhost:8080/api", + changeOrigin: true, + secure: false, + rewrite: (path): string => path.replace(/^\/api/, ""), + }, + }, + }, });