-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from IvanChiosa/delete-meal
Delete meal
- Loading branch information
Showing
7 changed files
with
275 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,60 @@ | ||
import {Meal} from "../Meal.ts"; | ||
import {useNavigate} from "react-router-dom"; | ||
import SearchBar from '../components/SearchBar'; | ||
import { useState } from 'react'; | ||
import {useState} from 'react'; | ||
import axios from "axios"; | ||
|
||
|
||
type MealPageProps = { | ||
meals: Meal[], | ||
getMeals: ()=>void | ||
} | ||
export default function AllMealPage(props: MealPageProps) { | ||
const navigate = useNavigate() | ||
const [meals, setMeals] = useState(props.meals); | ||
const [manage, setManage] = useState<boolean>(true) | ||
|
||
function changeManageState() { | ||
setManage(!manage) | ||
} | ||
function deleteThisItem(id:string){ | ||
// Optimistic UI update: Remove the meal from the local state | ||
setMeals(props.meals.filter(meal=>meal._id!=id)) | ||
axios.delete("api/meals/delete/"+id) | ||
.then(() => { | ||
// Refresh the data by fetching the updated data | ||
props.getMeals() | ||
}) | ||
} | ||
|
||
|
||
return ( | ||
<div><SearchBar setMeals={setMeals} /> | ||
<div className={"meal-container"}> | ||
<div> | ||
<div className={"management"}> | ||
<button onClick={changeManageState}><span>Manage</span></button> | ||
<SearchBar setMeals={setMeals} /> | ||
</div> | ||
|
||
{meals.map((meal: Meal) => ( | ||
<div className={"meal-card"} key={meal.idMeal} onClick={()=>navigate("/recipe/"+meal._id)}> | ||
{meal.strMealThumb && ( | ||
<img className={"meal-picture"} | ||
src={meal.strMealThumb} | ||
alt={meal.strMeal} | ||
/> | ||
)} | ||
<p className={"meal-introduction"}>{meal.strMeal}</p> | ||
</div> | ||
) | ||
)} | ||
</div> | ||
<div className={"meal-container"}> | ||
{meals.map((meal: Meal) => ( | ||
<div className={"meal-card"} key={meal.idMeal}> | ||
{meal.strMealThumb && ( | ||
<img className={"meal-picture"} | ||
onClick={()=>navigate("/recipe/"+meal._id)} | ||
src={meal.strMealThumb} | ||
alt={meal.strMeal} | ||
/> | ||
)} | ||
{manage && | ||
<div className={"two-buttons"}> | ||
<button onClick={()=>navigate("/recipe/edit/"+meal._id)}><span>Edit</span></button> | ||
<button onClick={()=>deleteThisItem(meal._id)}><span>Delete</span></button> | ||
</div>} | ||
<p className={"meal-introduction"}>{meal.strMeal}</p> | ||
</div> | ||
) | ||
)} | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.