diff --git a/src/main.js b/src/main.js index bdfb41f..f8d0ee0 100644 --- a/src/main.js +++ b/src/main.js @@ -1,4 +1,5 @@ import { ShoppingListItem } from "./ShoppingListItem"; +import { ShoppingCategory } from "./ShoppingCategory"; const addItemForm = document.querySelector(".list__form__addItem"); const addCategoryForm = document.querySelector(".list__form__addCategory") @@ -12,8 +13,13 @@ function addCategory() { } function addNewItem(itemName, itemAmount, category) { - listElements.push(new ShoppingListItem(itemName, itemAmount, category, renderList)); - renderList(); + if (category) { + category = listElements.find((element) => element instanceof ShoppingCategory && element.categoryName == category) + category.addItem(new ShoppingListItem(itemName, itemAmount, renderItemList)); + } else { + listElements.push(new ShoppingListItem(itemName, itemAmount, renderItemList)); + } + renderItemList(); return 0; }