Skip to content
Open

done #26

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
433 changes: 0 additions & 433 deletions README.md

Large diffs are not rendered by default.

2,043 changes: 1,959 additions & 84 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{
"homepage": "http://helip0269.github.io/lab-react-opennutrition",
"name": "lab-react-root-nutrition",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"antd": "^5.12.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
Expand All @@ -34,5 +38,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"gh-pages": "^6.1.1"
}
}
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;
13 changes: 11 additions & 2 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
.App {
text-align: center;
margin: 10px;
}

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

@media (prefers-reduced-motion: no-preference) {
Expand All @@ -15,7 +18,8 @@

.App-header {
background-color: #282c34;
min-height: 100vh;
min-height: 100vh;}
.feedback {
display: flex;
flex-direction: column;
align-items: center;
Expand All @@ -26,6 +30,8 @@

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

@keyframes App-logo-spin {
Expand All @@ -36,3 +42,6 @@
transform: rotate(360deg);
}
}
.feedback span {
font-size: 200px;
}
85 changes: 68 additions & 17 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;
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;
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';

// src/index.js

// import 'antd/dist/antd.css';

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