|
| 1 | +# HealthCheck Component |
| 2 | + |
| 3 | +## Stage 1 — What we are building |
| 4 | + |
| 5 | +A `HealthCheck` component that: |
| 6 | +- Shows a "Check Health" button |
| 7 | +- On click, calls `GET /health` on the FastAPI API |
| 8 | +- Displays the response: status, database, version |
| 9 | + |
| 10 | +This is the React equivalent of the health check we built in vanilla JS. |
| 11 | + |
| 12 | +### Vanilla JS equivalent (for comparison) |
| 13 | +```javascript |
| 14 | +document.getElementById("check-health-btn").addEventListener("click", async () => { |
| 15 | + const response = await fetch("/health"); |
| 16 | + const data = await response.json(); |
| 17 | + result.textContent = `Status: ${data.status} | DB: ${data.database}`; |
| 18 | +}); |
| 19 | +``` |
| 20 | + |
| 21 | +### React version (what we will write) |
| 22 | +```jsx |
| 23 | +function HealthCheck() { |
| 24 | + const [status, setStatus] = useState(null); |
| 25 | + |
| 26 | + async function checkHealth() { |
| 27 | + const response = await fetch(`${API_URL}/health`); |
| 28 | + const data = await response.json(); |
| 29 | + setStatus(data); |
| 30 | + } |
| 31 | + |
| 32 | + return ( |
| 33 | + <div> |
| 34 | + <button onClick={checkHealth}>Check Health</button> |
| 35 | + {status && <p>Status: {status.status} | DB: {status.database}</p>} |
| 36 | + </div> |
| 37 | + ); |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### Files |
| 42 | +- `vault/frontend/src/components/HealthCheck.jsx` ← new |
| 43 | +- `vault/frontend/.env` ← new (API base URL) |
| 44 | +- `vault/frontend/src/App.jsx` ← updated (import HealthCheck) |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Stage 2 — KT: New concepts in this component |
| 49 | + |
| 50 | +### 1. VITE_API_URL — frontend environment variable |
| 51 | + |
| 52 | +We never hardcode the API URL in code because: |
| 53 | +- In development the API is at `http://localhost:8000` |
| 54 | +- In production it will be at `https://yourdomain.com` |
| 55 | + |
| 56 | +Vite reads from a `.env` file in the frontend folder: |
| 57 | +``` |
| 58 | +VITE_API_URL=http://localhost:8000 |
| 59 | +``` |
| 60 | + |
| 61 | +In code, access it with: |
| 62 | +```javascript |
| 63 | +import.meta.env.VITE_API_URL |
| 64 | +``` |
| 65 | + |
| 66 | +Rules: |
| 67 | +- Must start with `VITE_` — Vite only exposes variables with this prefix to the browser |
| 68 | +- Never put secrets here — this gets bundled into the JS the browser downloads |
| 69 | + |
| 70 | +### 2. Conditional rendering — `{status && <p>...</p>}` |
| 71 | + |
| 72 | +```jsx |
| 73 | +{status && <p>Status: {status.status}</p>} |
| 74 | +``` |
| 75 | + |
| 76 | +If `status` is `null` (before the button is clicked), nothing renders. |
| 77 | +Once `status` has data, the `<p>` appears. |
| 78 | + |
| 79 | +This is the React equivalent of: |
| 80 | +```javascript |
| 81 | +if (status) { |
| 82 | + element.textContent = status.status; |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### 3. onClick on a button |
| 87 | + |
| 88 | +```jsx |
| 89 | +<button onClick={checkHealth}>Check Health</button> |
| 90 | +``` |
| 91 | + |
| 92 | +`onClick` is React's event handler — equivalent to `addEventListener("click", ...)`. |
| 93 | +You pass the function reference directly — no need to find the element by id first. |
| 94 | + |
| 95 | +### 4. DevTools to watch during this component |
| 96 | + |
| 97 | +| Tab | What to look for | |
| 98 | +|---|---| |
| 99 | +| Network | `GET /health` request — status 200, response JSON | |
| 100 | +| Components | Click `HealthCheck` in tree — watch `status` state change from null to object after button click | |
| 101 | +| Console | Any fetch errors will appear here | |
0 commit comments