diff --git a/Francesco.Recipes.World/wwwroot/js/site.js b/Francesco.Recipes.World/wwwroot/js/site.js index 12bdab8..20666ef 100644 --- a/Francesco.Recipes.World/wwwroot/js/site.js +++ b/Francesco.Recipes.World/wwwroot/js/site.js @@ -189,3 +189,25 @@ async function addIngredient() { } } + + async function addSelectedIngredientsToShoppingList() { + var form = document.getElementById('ingredient-form'); + var formData = new FormData(form); + var selectedIngredientIds = formData.getAll('ingredientIds'); + + var response = await fetch('/ShoppingList/CreateOrAddIngredients', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ recipeId: '@Model.Id', ingredientIds: selectedIngredientIds }) + }); + + if (response.ok) { + var result = await response.json(); + localStorage.setItem('shoppingListId', result.shoppingListId); + alert('Shopping list updated.'); + } else { + alert('Failed to update shopping list.'); + } +} \ No newline at end of file