kategorie hinzufügen aktualisert im frontend
addCategory ist async, damit wir das zurückgegebene JSON Objekt vom backend auch richtig entpacken können. Das JSON wandeln wir in eine ShoppingCategory um, was dann in einer eigenen Klasse die HTML Elemente in render() erstellt und es zurück gibt, was dann in ui.addCategory() an die vorhandene Liste dranhängen kann. Es wurden außerdem schonmal die Knöpfe hinzugefügt für das Löschen fixes #2
This commit is contained in:
1 parent
6cfe4d3510
commit
fd0a3959a5
4 files changed
+38
-118
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user