# React UI Tutorial: Buttons, Edit Boxes, Comboboxes, And Backend API Data This tutorial focuses on the user interface part of React. You will learn: - What a React component is - How to create a button - How to create an edit box - How to create a combobox - How to store UI values with React state - How to send data to a backend API - How to read data from a backend API The examples use your Tank Monitor backend: ```text http://127.0.0.1:5000 ``` ## 1. What React Does React helps you build a user interface with small pieces called components. Example component: ```jsx function App() { return (

Tank Monitor

); } ``` A component is just a JavaScript function that returns UI. ## 2. Important React Idea: State State means data that can change while the page is open. Examples: - Text typed into an edit box - Selected value in a combobox - Data loaded from PostgreSQL through the backend API - Loading or error messages React uses `useState` for this: ```jsx import { useState } from "react"; function App() { const [tankName, setTankName] = useState(""); return ( setTankName(event.target.value)} /> ); } ``` Meaning: ```text tankName ``` is the current value. ```text setTankName ``` changes the value. ## 3. Button Example A React button looks like normal HTML, but you attach a JavaScript function with `onClick`. ```jsx function App() { function handleButtonClick() { console.log("Button was pressed"); } return ( ); } ``` When the user presses the button, React runs: ```javascript handleButtonClick() ``` ## 4. Edit Box Example An edit box is usually an ``. ```jsx import { useState } from "react"; function App() { const [operatorName, setOperatorName] = useState(""); return (
setOperatorName(event.target.value)} />

Current value: {operatorName}

); } ``` Important parts: ```jsx value={operatorName} ``` The input displays the React state value. ```jsx onChange={(event) => setOperatorName(event.target.value)} ``` When the user types, React updates the state. This is called a controlled input. ## 5. Number Edit Box Example For numbers: ```jsx import { useState } from "react"; function App() { const [tankLevel, setTankLevel] = useState(0); return (
setTankLevel(Number(event.target.value))} />

Tank level: {tankLevel}

); } ``` `event.target.value` is normally text, so `Number(...)` converts it to a number. ## 6. Date And Time Edit Box For tank full and empty time: ```jsx import { useState } from "react"; function App() { const [tankFullTime, setTankFullTime] = useState(""); return (
setTankFullTime(event.target.value)} />

{tankFullTime}

); } ``` The browser shows a date/time picker. ## 7. Combobox Example In HTML, a combobox is a ` setTankStatus(event.target.value)} >

Selected status: {tankStatus}

); } ``` The state value is: ```javascript tankStatus ``` The function that changes it is: ```javascript setTankStatus ``` ## 8. Loading Data From Backend API To read tank data from your backend: ```text GET http://127.0.0.1:5000/api/tank-times ``` React code: ```jsx import { useEffect, useState } from "react"; function App() { const [tankRows, setTankRows] = useState([]); const [error, setError] = useState(""); async function loadTankRows() { try { const response = await fetch("http://127.0.0.1:5000/api/tank-times"); const data = await response.json(); if (!response.ok) { throw new Error(data.error || "Could not load tank rows"); } setTankRows(data); } catch (error) { setError(error.message); } } useEffect(() => { loadTankRows(); }, []); return (

Tank Rows

{error &&

{error}

}
{JSON.stringify(tankRows, null, 2)}
); } ``` Important: ```jsx useEffect(() => { loadTankRows(); }, []); ``` This runs once when the page opens. ## 9. Showing API Data In A Table Instead of showing JSON, show a table: ```jsx function TankTable({ rows }) { return ( {rows.map((row) => ( ))}
ID Tank Full Time Tank Empty Time Duration Minutes
{row.id} {row.tankFullTime} {row.tankEmptyTime || "Open"} {row.durationMinutes ?? "Open"}
); } ``` Use it like this: ```jsx ``` ## 10. Sending Data To Backend API To save a completed tank row, call: ```text POST http://127.0.0.1:5000/api/tank-times ``` React example: ```jsx import { useState } from "react"; function App() { const [tankFullTime, setTankFullTime] = useState(""); const [tankEmptyTime, setTankEmptyTime] = useState(""); const [message, setMessage] = useState(""); async function saveTankTime() { try { const response = await fetch("http://127.0.0.1:5000/api/tank-times", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tankFullTime: tankFullTime, tankEmptyTime: tankEmptyTime }) }); const result = await response.json(); if (!response.ok) { throw new Error(result.error || "Could not save tank time"); } setMessage("Saved successfully"); } catch (error) { setMessage(error.message); } } return (
setTankFullTime(event.target.value)} /> setTankEmptyTime(event.target.value)} />

{message}

); } ``` ## 11. Form Submit Example For real forms, prefer `onSubmit`. ```jsx function App() { const [tankFullTime, setTankFullTime] = useState(""); const [tankEmptyTime, setTankEmptyTime] = useState(""); async function handleSubmit(event) { event.preventDefault(); await fetch("http://127.0.0.1:5000/api/tank-times", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tankFullTime, tankEmptyTime }) }); } return (
setTankFullTime(event.target.value)} /> setTankEmptyTime(event.target.value)} />
); } ``` This line is important: ```javascript event.preventDefault(); ``` It stops the browser from refreshing the page when the form is submitted. ## 12. Full UI Example This example includes: - Edit boxes - Combobox - Button - Backend GET - Backend POST - Table ```jsx import { useEffect, useState } from "react"; function App() { const [tankFullTime, setTankFullTime] = useState(""); const [tankEmptyTime, setTankEmptyTime] = useState(""); const [tankStatus, setTankStatus] = useState("completed"); const [rows, setRows] = useState([]); const [message, setMessage] = useState(""); async function loadRows() { try { const response = await fetch("http://127.0.0.1:5000/api/tank-times"); const data = await response.json(); if (!response.ok) { throw new Error(data.error || "Could not load rows"); } setRows(data); } catch (error) { setMessage(error.message); } } async function saveRow(event) { event.preventDefault(); try { const response = await fetch("http://127.0.0.1:5000/api/tank-times", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tankFullTime, tankEmptyTime, tankStatus }) }); const result = await response.json(); if (!response.ok) { throw new Error(result.error || "Could not save row"); } setMessage("Saved successfully"); setTankFullTime(""); setTankEmptyTime(""); await loadRows(); } catch (error) { setMessage(error.message); } } useEffect(() => { loadRows(); }, []); return (

Tank Monitor

setTankFullTime(event.target.value)} /> setTankEmptyTime(event.target.value)} />

{message}

{rows.map((row) => ( ))}
ID Full Empty Duration
{row.id} {row.tankFullTime} {row.tankEmptyTime || "Open"} {row.durationMinutes ?? "Open"}
); } export default App; ``` ## 13. How UI Data Links To Backend API The UI does not talk directly to PostgreSQL. The UI talks to Node.js: ```javascript fetch("http://127.0.0.1:5000/api/tank-times") ``` Node.js talks to PostgreSQL: ```javascript pool.query("SELECT ... FROM tank_times") ``` The flow is: ```text React UI input values | v fetch() | v Node.js API | v PostgreSQL ``` ## 14. Common UI Pattern Most React UI screens follow this pattern: ```text 1. Create state variables 2. Show input controls 3. Update state when the user types or selects 4. Press a button 5. Send state values to backend API with fetch() 6. Show result or error message 7. Reload table data ``` Example: ```jsx const [name, setName] = useState(""); setName(event.target.value)} /> ``` ## 15. Quick Summary Button: ```jsx ``` Edit box: ```jsx setName(event.target.value)} /> ``` Combobox: ```jsx ``` GET from backend: ```javascript const response = await fetch("http://127.0.0.1:5000/api/tank-times"); const data = await response.json(); ``` POST to backend: ```javascript await fetch("http://127.0.0.1:5000/api/tank-times", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tankFullTime, tankEmptyTime }) }); ```