diff --git a/src/main/resources/static/js/list/ShoppingCategory.js b/src/main/resources/static/js/list/ShoppingCategory.js index 3a2d076..7a4bcea 100644 --- a/src/main/resources/static/js/list/ShoppingCategory.js +++ b/src/main/resources/static/js/list/ShoppingCategory.js @@ -1,120 +1,35 @@ -import { addNewItem } from "./main.js"; - export class ShoppingCategory { - - constructor(categoryName, onDelete, onUpdate) { + constructor(categoryName) { this.categoryName = categoryName; - this.itemsInCategory = []; - this.remove = false; - this.isEditing = false; - this.onDelete = onDelete; - this.onUpdate = onUpdate; + this.categoryID = null; + } + + static fromJSON(categoryAsJSON) { + const category = new ShoppingCategory(categoryAsJSON.categoryName); + category.categoryID = categoryAsJSON.id; + return category; } render() { - let element; - const outerLi = document.createElement("li"); - //if (this.itemsInCategory.length == 0) return; + const li = document.createElement("li"); + const span = document.createElement("span"); + const deleteCategoryButton = document.createElement("button"); + const deleteAllItemsCheckbox = document.createElement("input"); + li.id = this.categoryID; + li.classList.add("category"); + span.textContent = this.categoryName; + span.classList.add("category__name--bold", "category__name--big"); - if (this.isEditing) { - element = this.#renderEditing(); - } else { - element = this.#renderNormal(); - } - outerLi.append(element); - return outerLi; - } + deleteCategoryButton.classList.add("category__deleteButton"); + deleteCategoryButton.textContent = "Kategorie löschen"; - #renderEditing() { - //const li = document.createElement("li"); - const editForm = document.createElement("form"); - const editCategoryNameField = document.createElement("input"); - const saveButton = document.createElement("button"); - const abortButton = document.createElement("button"); + deleteAllItemsCheckbox.setAttribute("type", "checkbox"); + li.appendChild(span); + li.appendChild(deleteCategoryButton); + li.appendChild(deleteAllItemsCheckbox); - editCategoryNameField.setAttribute("name", "newCategoryName"); - editCategoryNameField.setAttribute("value", this.categoryName); - - - abortButton.textContent = "Abbrechen"; - abortButton.addEventListener("click", (event) => { - event.preventDefault(); - this.isEditing = false; - this.onUpdate(); - }); - - saveButton.textContent = "Speichern"; - editForm.addEventListener("submit", (event) => { - event.preventDefault(); - - const editFormData = Object.fromEntries(new FormData(editForm)); - - if (!editFormData.newCategoryName) { - return; - } - - this.categoryName = editFormData.newCategoryName; - this.isEditing = false; - this.onUpdate(); - this.onDelete(); - }); - - editForm.appendChild(editCategoryNameField); - editForm.appendChild(saveButton); - editForm.appendChild(abortButton); - //li.appendChild(editForm); - - return editForm; - - } - - #renderNormal() { - - const spanCategoryName = document.createElement("span"); - const removeCategoryButton = document.createElement("button"); // Entfernen der Kategorie - const removeCategoryItemsCheckbox = document.createElement("input"); // alle Gegenstände drunter entfernen oder nicht - const removeCategoryItemsText = document.createElement("span"); - const ul = document.createElement("ul"); - const items = this.itemsInCategory.filter((item) => !item.remove); - - spanCategoryName.textContent = this.categoryName; - spanCategoryName.classList.add("category__name--big", "category__name--bold"); - spanCategoryName.addEventListener("click", () => { - this.isEditing = true; - this.onUpdate(); - }); - - removeCategoryItemsCheckbox.setAttribute("type", "checkbox"); - removeCategoryItemsText.textContent = "Alle Gegenstände in dieser Kategorie löschen? :"; - - - removeCategoryButton.textContent = "Löschen"; - removeCategoryButton.addEventListener("click", () => { - this.remove = true; - if (!removeCategoryItemsCheckbox.checked) { - this.itemsInCategory.forEach((element) => { - addNewItem(element.itemName, element.itemAmount, ""); - }); - } - this.onUpdate(); - this.onDelete(); - - }); - - ul.appendChild(spanCategoryName); - ul.appendChild(removeCategoryButton); - ul.appendChild(removeCategoryItemsText); - ul.appendChild(removeCategoryItemsCheckbox); - items.forEach((item, index) => { - ul.append(item.render()); - }); - - return ul; - } - - addItem(item) { - this.itemsInCategory.push(item); + return li; } } diff --git a/src/main/resources/static/js/list/list.js b/src/main/resources/static/js/list/list.js index 89b2131..6254fb5 100644 --- a/src/main/resources/static/js/list/list.js +++ b/src/main/resources/static/js/list/list.js @@ -12,10 +12,11 @@ function addItem(itemName, itemAmount, categoryID) { ui.addItem(); } -function addCategory(categoryName) { +async function addCategory(categoryName) { console.log(`adding category ${categoryName}`) - api.addCategory(currentList.id, categoryName); - ui.addCategory(categoryName); + const categoryAsJSON = await api.addCategory(currentList.id, categoryName); + console.log(categoryAsJSON); + ui.addCategory(categoryAsJSON); return 0; } diff --git a/src/main/resources/static/js/list/listAPI.js b/src/main/resources/static/js/list/listAPI.js index fc48881..f85cc2b 100644 --- a/src/main/resources/static/js/list/listAPI.js +++ b/src/main/resources/static/js/list/listAPI.js @@ -25,16 +25,16 @@ export function deleteItem(itemID) { } -export function addCategory(listID, categoryName) { - fetch(`/api/list/${listID}/category/`, +export async function addCategory(listID, categoryName) { + const newCategory = await fetch(`/api/list/${listID}/category/`, { method: "POST" , body: JSON.stringify({ "categoryName": categoryName }) , headers: { "Content-Type": "application/json" } - }).then(r => console.log("Add new category")); - listElements.push(new ShoppingCategory(categoryName, renderAddItemPrompt, renderItemList)); - renderItemList(); - return 0; + }); + //listElements.push(new ShoppingCategory(categoryName, renderAddItemPrompt, renderItemList)); + //renderItemList(); + return await newCategory.json(); } diff --git a/src/main/resources/static/js/list/listFrontend.js b/src/main/resources/static/js/list/listFrontend.js index dfa6751..f5f9d05 100644 --- a/src/main/resources/static/js/list/listFrontend.js +++ b/src/main/resources/static/js/list/listFrontend.js @@ -1,3 +1,4 @@ +import { ShoppingCategory } from "./ShoppingCategory.js"; export function addItem() { @@ -7,8 +8,11 @@ export function deleteItem(li) { li.remove(); } -export function addCategory() { +export function addCategory(categoryAsJSON) { + const list = document.querySelector(".displayedList"); + const category = ShoppingCategory.fromJSON(categoryAsJSON); + list.appendChild(category.render()); } export function deleteCategory() {