Skip to content

Commit

Permalink
Add item form
Browse files Browse the repository at this point in the history
  • Loading branch information
bakaq committed Oct 5, 2024
1 parent 2590476 commit 50cfc2c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
76 changes: 73 additions & 3 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function App() {

function ItemList() {
const [items, setItems] = useState([]);
const [showAddForm, setShowAddForm] = useState(false);

async function getItems() {
const response = await fetch(`${API_ROOT}/item/get_all`, {
Expand All @@ -23,6 +24,7 @@ function ItemList() {
setItems(content);
}

// Get the items on first render.
useEffect(() => {
async function getItems() {
const response = await fetch(`${API_ROOT}/item/get_all`, {
Expand All @@ -36,7 +38,6 @@ function ItemList() {
getItems();
}, []);


const itemList = items.map((item) =>
<li key={item.id}>
<ItemCard itemData={item.item} />
Expand All @@ -47,8 +48,77 @@ function ItemList() {
<div className="item-list">
<h2>Items</h2>
<button onClick={getItems}>Reload</button>
<button onClick={getItems}>Add (TODO)</button>
<ul className="item-list">{itemList}</ul>
{
showAddForm ?
<AddForm show={showAddForm} getItems={getItems} /> :
<button onClick={() => setShowAddForm(true)}>Add</button>
}
<ul>{itemList}</ul>
</div>
);
}

function AddForm({ getItems }) {
const fields = {
name: "Name",
weight: "Weight",
price: "Price",
portionWeight: "Portion weight",
calories: "Calories",
protein: "Protein",
};

const [inputs, setInputs] = useState({});

function changeHandler(event) {
const name = event.target.name;
const value = event.target.value;
setInputs(old => ({ ...old, [name]: value }));
// Reload the list.
getItems();
}

const fieldsArray = Object.entries(fields).map(([name, label]) => (
<div key={name}>
<label>{label}:
<input type="text" name={name} onChange={changeHandler} />
</label>
<br />
</div>
));

async function submitHandler(event) {
event.preventDefault();

const itemDescription = {
name: inputs.name,
price: parseFloat(inputs.price),
weight: parseFloat(inputs.weight),
nutrition: {
portion_weight: parseFloat(inputs.portionWeight),
calories: parseFloat(inputs.calories),
protein: parseFloat(inputs.protein),
},
};
await fetch(`${API_ROOT}/item/add`, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(itemDescription),
});

// Reload the list.
getItems();
}

return (
<div className="add-form">
<form onSubmit={submitHandler}>
{fieldsArray}
<input type="submit" id="submit-add" value="Add item" />
</form>
</div>
);
}
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ body {
}

.item-list {
margin: 0;
background-color: cyan;
padding: var(--padding);
border-radius: var(--padding);
}

.item-list > ul {
margin: 0;
padding: 0;
list-style: none;
}

Expand All @@ -44,4 +45,8 @@ body {
border-radius: var(--padding);
}


.add-form {
background-color: pink;
padding: var(--padding);
border-radius: var(--padding);
}

0 comments on commit 50cfc2c

Please sign in to comment.