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
1,706 changes: 1,680 additions & 26 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"antd": "^5.22.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
Expand Down
38 changes: 0 additions & 38 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,38 +0,0 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

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

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

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
59 changes: 43 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
import logo from './logo.svg';
import './App.css';
import React, { useState } from 'react';
import foods from './foods.json';
import FoodBox from './components/FoodBox';
import AddFoodForm from './components/AddFoodForm';
import Search from './components/Search';

function App() {
const [foodList, setFoodList] = useState(foods); // Full list of foods
const [searchTerm, setSearchTerm] = useState(''); // Search query state

// Function to add new food
const handleAddFood = (newFood) => {
setFoodList([...foodList, newFood]);
};

// Function to update the search term
const handleSearch = (query) => {
setSearchTerm(query.toLowerCase());
};

// Function to delete a food item
const handleDelete = (foodName) => {
const updatedFoodList = foodList.filter((food) => food.name !== foodName);
setFoodList(updatedFoodList);
};

// Filter the food list based on the search term
const filteredFoodList = foodList.filter((food) =>
food.name.toLowerCase().includes(searchTerm)
);

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>
<h1>Food List</h1>

{/* Render Search Component */}
<Search handleSearch={handleSearch} />

{/* Render AddFoodForm */}
<AddFoodForm onAddFood={handleAddFood} />

{/* Render FoodBox Components */}
<div>
{filteredFoodList.map((food, index) => (
<FoodBox key={index} food={food} handleDelete={handleDelete} />
))}
</div>
</div>
);
}
Expand Down
93 changes: 93 additions & 0 deletions src/components/AddFoodForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React, { useState } from 'react';
import { Input, Button } from 'antd';

function AddFoodForm({ onAddFood }) {
// State to store form inputs
const [form, setForm] = useState({
name: '',
image: '',
calories: '',
servings: '',
});

// Handle input changes
const handleChange = (e) => {
const { name, value } = e.target;
setForm({ ...form, [name]: value });
};

// Handle form submission
const handleSubmit = (e) => {
e.preventDefault();

// Validate that all fields are filled
if (!form.name || !form.image || !form.calories || !form.servings) {
alert('Please fill all the fields');
return;
}

// Pass the new food object to the parent
onAddFood({
name: form.name,
image: form.image,
calories: Number(form.calories),
servings: Number(form.servings),
});

// Reset form fields
setForm({
name: '',
image: '',
calories: '',
servings: '',
});
};

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

<label>Name:</label>
<Input
name="name"
value={form.name}
type="text"
onChange={handleChange}
placeholder="Enter food name"
/>

<label>Image:</label>
<Input
name="image"
value={form.image}
type="text"
onChange={handleChange}
placeholder="Enter image URL"
/>

<label>Calories:</label>
<Input
name="calories"
value={form.calories}
type="number"
onChange={handleChange}
placeholder="Enter calories"
/>

<label>Servings:</label>
<Input
name="servings"
value={form.servings}
type="number"
onChange={handleChange}
placeholder="Enter servings"
/>

<Button type="primary" htmlType="submit" style={{ marginTop: '10px' }}>
Add Food
</Button>
</form>
);
}

export default AddFoodForm;
29 changes: 29 additions & 0 deletions src/components/FoodBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react'
import { Card, Col, Button } from 'antd';
const FoodBox = (props,handleDelete) => {
return (
<Col>
<Card
title={props.food.name}
style={{ width: 230, height: 300, margin: 10 }}
>
<img src={props.food.image} height={60} alt="props.food" />
<p>{props.food.calories}</p>
<p>{props.food.servings}</p>
<p>
<b>Total Calories: {props.food.calories*props.food.servings} * FOOD_SERVINGS </b> kcal
</p>
<Button
type="primary"
danger
onClick={() => handleDelete(props.food.name)}
style={{ marginTop: '10px' }}
>
Delete
</Button>
</Card>
</Col>
)
}

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

function Search({ handleSearch }) {
// Handle input change and pass the value to the parent
const handleInputChange = (e) => {
handleSearch(e.target.value);
};

return (
<div style={{ marginBottom: '20px' }}>
<h2>Search</h2>
<Input
type="text"
placeholder="Search food items"
onChange={handleInputChange}
/>
</div>
);
}

export default Search;
13 changes: 0 additions & 13 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';


import 'antd/dist/reset.css';


const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
Expand Down