Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/AddFoodForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { useState } from 'react';
import { Divider, Input, Button } from 'antd';

function AddFoodForm(props) {
const [newFood, setNewFood] = useState({
name: '',
image: '',
calories: '',
servings: '',
});

const handleInputChange = (e) => {
const { name, value } = e.target;
setNewFood({ ...newFood, [name]: value });
};

const handleSubmit = (e) => {
e.preventDefault();

// Check if all fields are filled before adding the new food
if (newFood.name && newFood.image && newFood.calories && newFood.servings) {
// Call the onAddFood function passed from the parent component
props.onAddFood(newFood);

// Reset the form after submitting
setNewFood({
name: '',
image: '',
calories: '',
servings: '',
});
}
};

return (
<form onSubmit={handleSubmit}>
<Divider>Add Food Entry</Divider>

<label>Name</label>
<Input
name="name"
value={newFood.name}
type="text"
onChange={handleInputChange}
/>

<label>Image</label>
<Input
name="image"
value={newFood.image}
type="text"
onChange={handleInputChange}
/>

<label>Calories</label>
<Input
name="calories"
value={newFood.calories}
type="number"
onChange={handleInputChange}
/>

<label>Servings</label>
<Input
name="servings"
value={newFood.servings}
type="number"
onChange={handleInputChange}
/>

<Button type="primary" htmlType="submit">
Create
</Button>
</form>
);
}

export default AddFoodForm;
35 changes: 8 additions & 27 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
.App {
text-align: center;
margin: 10px;
}

.App-logo {
height: 40vmin;
pointer-events: none;
Button {
margin: 10px 0;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
.feedback {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
align-items: center;
padding: 50px;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
.feedback span {
font-size: 200px;
}
83 changes: 67 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,76 @@
import logo from './logo.svg';
// import logo from './logo.svg';
import { useState } from 'react';
import './App.css';
import { Row, Divider, Button} from 'antd';
import FoodBox from './FoodBox';
import AddFoodForm from './AddFoodForm';
import Search from './Search';
import foods from './foods.json';

function App() {
const [foodData, setFoodData] = useState(foods.map((food) => ({ ...food, id: food.name })));
const [filteredFoodData, setFilteredFoodData] = useState(foodData);
const [showAddForm, setShowAddForm] = useState(false);

const handleAddFood = (newFood) => {
// Use food name as id
const foodWithId = { ...newFood, id: newFood.name };

setFoodData([foodWithId, ...foodData]);
setFilteredFoodData([foodWithId, ...filteredFoodData]);
};

const handleDeleteFood = (id) => {
const updatedFoodData = foodData.filter((food) => food.id !== id);
setFoodData(updatedFoodData);
setFilteredFoodData(updatedFoodData);
};

const handleSearch = (query) => {
const filteredData = foodData.filter((food) =>
food.name.toLowerCase().includes(query.toLowerCase())
);
setFilteredFoodData(filteredData);
};

const toggleAddForm = () => {
setShowAddForm(!showAddForm);
};

return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>

<Divider>
<h2>Food List</h2>
</Divider>

{/* Display AddFoodForm if showAddForm is true */}
{showAddForm && <AddFoodForm onAddFood={handleAddFood} />}

{/* Toggle display of AddFoodForm */}
<Button type="default" onClick={toggleAddForm}>
{showAddForm ? 'Hide Form' : 'Add New Food'}
</Button>

{/* Display Search component */}
<Search onSearch={handleSearch} searchQuery={filteredFoodData} />

<Divider>Food List</Divider>

<Row style={{ width: '100%', justifyContent: 'center'}}>
{/* Render the list of Food Box components */}
{foodData.length === 0 ? (
<div className="feedback">
<h3>Oops! There is no more content to show.</h3>
<span>Ø</span>
</div>
) : (
foodData.map((food) => (
<FoodBox key={food.name} food={food} onDelete={handleDeleteFood} />
))
)}
</Row>
</div>
);
}

export default App;
29 changes: 29 additions & 0 deletions src/FoodBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Card, Col, Button } from 'antd';

function FoodBox({ food, onDelete }) {
const handleDelete = () => {
// Call the onDelete function with the food name when the delete button is clicked
onDelete(food.name);
};

return (
<Col>
<Card
title={food.name}
style={{ width: 250, height: 320, margin: 20 }}
>
<img src={food.image} height={60} alt="food" />
<p>Calories: {food.calories}</p>
<p>Servings: {food.servings}</p>
<p>
<b>Total Calories: {food.calories * food.servings} kcal</b>
</p>
<Button type="primary" onClick={handleDelete}>
Delete
</Button>
</Card>
</Col>
);
}

export default FoodBox;
18 changes: 18 additions & 0 deletions src/Search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Divider, Input } from 'antd';

function Search({ onSearch }) {
const handleSearch = (e) => {
onSearch(e.target.value);
};

return (
<>
<Divider>Search</Divider>

<label>Search</label>
<Input type="text" onChange={handleSearch} placeholder="Enter search query"/>
</>
);
}

export default Search;