diff --git a/src/main/resources/static/js/models/ShoppingCategory.js b/src/main/resources/static/js/models/ShoppingCategory.js index a8545a2..dae39d5 100644 --- a/src/main/resources/static/js/models/ShoppingCategory.js +++ b/src/main/resources/static/js/models/ShoppingCategory.js @@ -1,4 +1,3 @@ - export class ShoppingCategory extends HTMLElement { static get observedAttributes() { return ["category-name"] } @@ -44,7 +43,7 @@ export class ShoppingCategory extends HTMLElement { } - attributeChangeCallback(name, oldVal, newVal) { + attributeChangedCallback(name, oldVal, newVal) { if (!this._rendered) return; if (newVal == oldVal) return; switch (name) { diff --git a/src/main/resources/static/js/pages/index/api/indexAPI.js b/src/main/resources/static/js/pages/index/api/indexAPI.js index f9397d9..77676e0 100644 --- a/src/main/resources/static/js/pages/index/api/indexAPI.js +++ b/src/main/resources/static/js/pages/index/api/indexAPI.js @@ -1,8 +1,8 @@ -export async function addList(listName) { - return await fetch(`/api/lists`, +export function addList(listName) { + return fetch(`/api/lists`, { method: "POST", body: JSON.stringify({ listName: listName }), @@ -12,14 +12,14 @@ export async function addList(listName) { } -export async function deleteList(id) { - return await fetch(`/api/lists/${id}`, { method: "DELETE" }); +export function deleteList(id) { + return fetch(`/api/lists/${id}`, { method: "DELETE" }); } -export async function editList(listID, toChange) { - return await fetch(`/api/lists/${listID}`, +export function editList(listID, toChange) { + return fetch(`/api/lists/${listID}`, { method: "PATCH", body: JSON.stringify(toChange), diff --git a/src/main/resources/static/js/pages/index/frontend/indexUI.js b/src/main/resources/static/js/pages/index/frontend/indexUI.js index 678996c..6514097 100644 --- a/src/main/resources/static/js/pages/index/frontend/indexUI.js +++ b/src/main/resources/static/js/pages/index/frontend/indexUI.js @@ -47,5 +47,6 @@ export function editList(id, formElement, formData, listLi) { listLi.querySelector(".list__name").textContent = newName; listLi.setAttribute("href", `/list/${id}`); formElement.replaceWith(listLi); - } + + diff --git a/src/main/resources/static/js/pages/index/index.js b/src/main/resources/static/js/pages/index/index.js index a3e0dd9..efb6ae3 100644 --- a/src/main/resources/static/js/pages/index/index.js +++ b/src/main/resources/static/js/pages/index/index.js @@ -1,20 +1,14 @@ -import { showError, initErrors } from "../../shared/error.js"; +import { initErrors } from "../../shared/error.js"; +import { handleResponse } from "../../shared/helper.js"; +import { abortEditing } from "../../shared/sharedUI.js"; import * as api from "./api/indexAPI.js"; import * as ui from "./frontend/indexUI.js" const addListForm = document.querySelector(".addListForm"); async function addList(listName) { - const response = await api.addList(listName); - const json = await response.json(); - - if (!response.ok) { - console.error(`error creating list ${json.msg}`); - showError(json.msg); - return; - } - - ui.addList(json); + const json = await handleResponse(api.addList(listName), `error creating list`); + if (json) ui.addList(json); } function editList(listLi) { @@ -26,51 +20,27 @@ function editList(listLi) { formElement.querySelector(".editForm__name").focus(); - async function save() { - let formData = Object.fromEntries(new FormData(formElement)); - - console.log(`editing catogory ${id} with ${formData}`) - const response = await api.editList(id, { newListName: formData.newListName }); - - if (!response.ok) { - const json = await response.json(); - console.error(`error editing list ${json}`); - showError(json.msg); - - return; - } - - ui.editList(id, formElement, formData, listLi); - } - - formElement.addEventListener("submit", (event) => { + formElement.addEventListener("submit", async (event) => { event.preventDefault(); - save(); + + let formData = Object.fromEntries(new FormData(formElement)); + const result = await handleResponse(api.editList(id, { newListName: formData.newListName }), `error editing list`); + if (result) ui.editList(id, formElement, formData, listLi); }); formElement.querySelector(".editForm__abortButton").addEventListener("click", () => { - formElement.replaceWith(listLi); + abortEditing(formElement, listLi); }) formElement.addEventListener("keydown", (e) => { - if (e.key == "Escape") formElement.replaceWith(listLi); + if (e.key == "Escape") abortEditing(formElement, listLi); }) } async function deleteList(listElement) { - - const response = await api.deleteList(listElement.dataset.id); - - if (!response.ok) { - const json = await response.json(); - console.error(`error deleting list ${json}`); - showError(json.msg); - - return; - } - - ui.deleteList(listElement); + const json = await handleResponse(api.deleteList(listElement.dataset.id), `error deleting list`); + if (json) ui.deleteList(listElement); } diff --git a/src/main/resources/static/js/pages/list/api/categoryAPI.js b/src/main/resources/static/js/pages/list/api/categoryAPI.js index a5d7350..c5b32dd 100644 --- a/src/main/resources/static/js/pages/list/api/categoryAPI.js +++ b/src/main/resources/static/js/pages/list/api/categoryAPI.js @@ -1,5 +1,5 @@ -export async function addCategory(listID, categoryName) { - return await fetch(`/api/categories`, +export function addCategory(listID, categoryName) { + return fetch(`/api/categories`, { method: "POST" , body: JSON.stringify({ "categoryName": categoryName, "listID": listID }) @@ -7,9 +7,8 @@ export async function addCategory(listID, categoryName) { }); } - -export async function editCategory(categoryID, toChange) { - return await fetch(`/api/categories/${categoryID}`, +export function editCategory(categoryID, toChange) { + return fetch(`/api/categories/${categoryID}`, { method: "PATCH", body: JSON.stringify(toChange), @@ -17,6 +16,6 @@ export async function editCategory(categoryID, toChange) { }); } -export async function deleteCategory(deleteAllItems, categoryID) { - return await fetch(`/api/categories/${categoryID}?deleteAllItems=${deleteAllItems}`, { method: "DELETE" }); +export function deleteCategory(deleteAllItems, categoryID) { + return fetch(`/api/categories/${categoryID}?deleteAllItems=${deleteAllItems}`, { method: "DELETE" }); } diff --git a/src/main/resources/static/js/pages/list/api/itemAPI.js b/src/main/resources/static/js/pages/list/api/itemAPI.js index 63bcf8e..379acaa 100644 --- a/src/main/resources/static/js/pages/list/api/itemAPI.js +++ b/src/main/resources/static/js/pages/list/api/itemAPI.js @@ -1,7 +1,5 @@ - - -export async function addItem(listID, itemName, itemAmount, categoryID) { - return await fetch(`/api/items`, +export function addItem(listID, itemName, itemAmount, categoryID) { + return fetch(`/api/items`, { method: "POST", body: JSON.stringify({ @@ -14,15 +12,14 @@ export async function addItem(listID, itemName, itemAmount, categoryID) { }); } +export function deleteItem(itemID) { -export async function deleteItem(itemID) { - - return await fetch(`/api/items/${itemID}`, { method: "DELETE" }); + return fetch(`/api/items/${itemID}`, { method: "DELETE" }); } -export async function editItem(itemID, toChange) { - return await fetch(`/api/items/${itemID}`, +export function editItem(itemID, toChange) { + return fetch(`/api/items/${itemID}`, { method: "PATCH", body: JSON.stringify(toChange), @@ -31,10 +28,6 @@ export async function editItem(itemID, toChange) { } -export async function deleteAllDoneItems(listID) { - - return await fetch(`/api/lists/${listID}/items?done=true`, { method: "DELETE" }); - - +export function deleteAllDoneItems(listID) { + return fetch(`/api/lists/${listID}/items?done=true`, { method: "DELETE" }); } - diff --git a/src/main/resources/static/js/pages/list/frontend/categoryUI.js b/src/main/resources/static/js/pages/list/frontend/categoryUI.js index 46dd162..b016f74 100644 --- a/src/main/resources/static/js/pages/list/frontend/categoryUI.js +++ b/src/main/resources/static/js/pages/list/frontend/categoryUI.js @@ -1,5 +1,3 @@ - - export function addCategory(categoryAsJSON) { const list = document.querySelector(".displayedList"); const category = document.createElement("shopping-category"); @@ -29,36 +27,4 @@ export function deleteCategory(deleteAllItems, categoryID) { } category.remove() document.querySelector(`option[value='${categoryID}']`).remove(); - -} - -export function createEditCategoryForm(categoryName) { - const editForm = document.createElement("form"); - const inputName = document.createElement("input"); - const abortButton = document.createElement("button"); - const saveButton = document.createElement("button"); - - inputName.setAttribute("name", "newCategoryName"); - inputName.setAttribute("value", categoryName); - inputName.classList.add("editForm__name"); - - abortButton.setAttribute("type", "button"); - abortButton.classList.add("editForm__buttonAbort"); - abortButton.textContent = "Abbrechen"; - saveButton.textContent = "Speichern"; - - editForm.append(inputName, saveButton, abortButton); - - return editForm; -} - -export function editCategory(id, formElement, formData, categoryDiv) { - - let newName = formData.newCategoryName; - - categoryDiv.querySelector(".category__name").textContent = newName; - formElement.replaceWith(categoryDiv); - - document.querySelector(`option[value='${id}']`).textContent = newName; - } diff --git a/src/main/resources/static/js/pages/list/frontend/itemUI.js b/src/main/resources/static/js/pages/list/frontend/itemUI.js index b7f2993..36c2d98 100644 --- a/src/main/resources/static/js/pages/list/frontend/itemUI.js +++ b/src/main/resources/static/js/pages/list/frontend/itemUI.js @@ -1,5 +1,3 @@ -import { ShoppingItem } from "../../../models/ShoppingItem.js"; - export function addItem(itemAsJSON) { let list; if (!itemAsJSON.categoryID) { @@ -20,47 +18,6 @@ export function addItem(itemAsJSON) { list.append(li); } -export function createEditItemForm(currentName, currentAmount) { - const editForm = document.createElement("form"); - const inputName = document.createElement("input"); - const inputAmount = document.createElement("input"); - const abortButton = document.createElement("button"); - const saveButton = document.createElement("button"); - - inputName.setAttribute("name", "newItemName"); - inputName.setAttribute("value", currentName); - inputName.classList.add("editForm__name"); - - inputAmount.setAttribute("name", "newItemAmount"); - inputAmount.setAttribute("value", currentAmount); - inputAmount.classList.add("editForm__amount"); - - - abortButton.setAttribute("type", "button"); - abortButton.classList.add("editForm__buttonAbort"); - abortButton.textContent = "Abbrechen"; - - saveButton.textContent = "Speichern"; - saveButton.setAttribute("type", "submit"); - - - editForm.append(inputName, inputAmount, saveButton, abortButton); - - return editForm; -} - -export function editItem(shoppingItemElement, formElement, formData) { - - let newName = formData.newItemName; - let newAmount = formData.newItemAmount; - shoppingItemElement.setAttribute("item-name", newName); - shoppingItemElement.setAttribute("item-amount", newAmount); - - - formElement.replaceWith(shoppingItemElement); - -} - export function deleteItem(li) { li.remove(); } diff --git a/src/main/resources/static/js/pages/list/frontend/listUI.js b/src/main/resources/static/js/pages/list/frontend/listUI.js index e6adab1..14a8a56 100644 --- a/src/main/resources/static/js/pages/list/frontend/listUI.js +++ b/src/main/resources/static/js/pages/list/frontend/listUI.js @@ -1,3 +1,4 @@ + export * from "./itemUI.js"; export * from "./categoryUI.js"; @@ -7,3 +8,4 @@ export function deleteAllDoneItems() { li.remove(); }) } + diff --git a/src/main/resources/static/js/pages/list/list.js b/src/main/resources/static/js/pages/list/list.js index 2a9ef86..cca0bf6 100644 --- a/src/main/resources/static/js/pages/list/list.js +++ b/src/main/resources/static/js/pages/list/list.js @@ -1,9 +1,11 @@ import { ShoppingCategory } from "../../models/ShoppingCategory.js"; import { ShoppingItem } from "../../models/ShoppingItem.js"; -import { showError, initErrors } from "../../shared/error.js"; +import { initErrors } from "../../shared/error.js"; import * as api from "./api/listAPI.js" import * as ui from "./frontend/listUI.js" - +import { handleResponse } from "../../shared/helper.js"; +import { editElement } from "../../shared/editing.js"; +import * as sharedUI from "../../shared/sharedUI.js"; const addCategoryForm = document.querySelector(".addCategoryForm"); const addItemForm = document.querySelector(".addItemForm"); @@ -13,170 +15,36 @@ const deleteAllDoneItemsButton = document.querySelector(".deleteAllDoneItemsButt async function addItem(itemName, itemAmount, categoryID) { console.log(` adding item with id:${currentList.dataset.id}, name: ${itemName}, amount: ${itemAmount}, category: ${categoryID}`) - const response = await api.addItem(currentList.dataset.id, itemName, itemAmount, categoryID); - const json = await response.json(); - - if (!response.ok) { - console.error(`Error, item couldnt be created ${json}`); - showError(json.msg); - return; - } - - console.log(`rendering frontend item: ${json}`); - ui.addItem(json); + const json = await handleResponse(api.addItem(currentList.dataset.id, itemName, itemAmount, categoryID), `item couldnt be created!`); + if (json) ui.addItem(json); } async function addCategory(categoryName) { console.log(`adding category ${categoryName}`) - const response = await api.addCategory(currentList.dataset.id, categoryName); - const json = await response.json(); + const json = await handleResponse(api.addCategory(currentList.dataset.id, categoryName), `category couldnt be added!`); + if (json) ui.addCategory(json); - if (!response.ok) { - console.error(`Error, category couldnt be created ${json}`); - showError(json.msg); - - return; - } - - ui.addCategory(json); - return 0; -} - -//shoppingItem ist immer das li mit der id -async function editItem(shoppingItem, id) { - - - //erstellen das Forms - const formElement = ui.createEditItemForm(shoppingItem.querySelector(".item__itemName").textContent, shoppingItem.querySelector(".item__itemAmount").textContent); - - shoppingItem.replaceWith(formElement); - - formElement.querySelector(".editForm__name").focus(); - - async function save() { - - let formData = Object.fromEntries(new FormData(formElement)); - console.log(`editing item ${id} with ${formData}`) - - const response = await api.editItem(id, { itemName: formData.newItemName, itemAmount: formData.newItemAmount }); - - if (!response.ok) { - const json = await response.json(); - console.error(`Error editingItem ${json}`); - showError(json.msg) - return; - } - - ui.editItem(shoppingItem, formElement, formData); - } - - function abort() { - formElement.replaceWith(shoppingItem); - } - - formElement.addEventListener("submit", (event) => { - event.preventDefault(); - save(); - }); - - formElement.addEventListener("click", (e) => { - if (e.target.matches(".editForm__buttonAbort")) abort(); - }) - - formElement.addEventListener("keydown", (e) => { - if (e.key == "Escape") abort(); - }); - - -} - -function editCategory(categoryElement, id) { - //erstellen das Forms - //wir nutzen für das Element nur das div, weil sonst alle items mitgelöscht werden würden - const categoryDiv = categoryElement.querySelector(".category__header"); - const formElement = ui.createEditCategoryForm(categoryDiv.querySelector(".category__name").textContent); - - categoryDiv.replaceWith(formElement); - - formElement.querySelector(".editForm__name").focus(); - - async function save() { - - let formData = Object.fromEntries(new FormData(formElement)); - - console.log(`editing catogory ${id} with ${formData}`) - - const response = await api.editCategory(id, { categoryName: formData.newCategoryName }); - - if (!response.ok) { - const json = await response.json(); - console.error(`Error editing category ${json}`); - showError(json.msg); - return; - } - - ui.editCategory(id, formElement, formData, categoryDiv); - } - - function abort() { - formElement.replaceWith(categoryDiv); - } - - - formElement.addEventListener("submit", (event) => { - event.preventDefault(); - save(); - }); - - formElement.addEventListener("click", (e) => { - if (e.target.matches(".editForm__buttonAbort")) abort(); - }) - - - formElement.addEventListener("keydown", (e) => { - if (e.key == "Escape") abort(); - }) } async function deleteItem(li) { - const response = await api.deleteItem(li.dataset.id); + console.log(`deleting item ${li.dataset.id}`) - if (!response.ok) { - const json = await response.json(); - console.error(`Error deleting item ${json}`); - showError(json.msg); - return; - } - - ui.deleteItem(li); + const json = await handleResponse(api.deleteItem(li.dataset.id), `error deleting item`); + if (json) ui.deleteItem(li); } async function deleteCategory(deleteAllItems, categoryID) { console.log(`deleting category with allItems: ${deleteAllItems} id: ${categoryID}`); - const response = await api.deleteCategory(deleteAllItems, categoryID); + const json = await handleResponse(api.deleteCategory(deleteAllItems, categoryID), `error deleting category`); - if (!response.ok) { - const json = await response.json(); - console.error(`Error deleting category ${json}`); - showError(json.msg); - return; - } - ui.deleteCategory(deleteAllItems, categoryID); + if (json) ui.deleteCategory(deleteAllItems, categoryID); } async function deleteAllDoneItems() { console.log(`deleting all done items...`) - const response = await api.deleteAllDoneItems(currentList.dataset.id); - - if (!response.ok) { - const json = await response.json(); - console.error(`Error deleting all done items ${json}`); - showError(json.msg); - - return; - } - ui.deleteAllDoneItems(); + const json = await handleResponse(api.deleteAllDoneItems(currentList.dataset.id), `error deleting all done items`); + if (json) ui.deleteAllDoneItems(); } function init() { @@ -216,12 +84,43 @@ function init() { } else if (event.target.closest(".item__text")) { const shoppingItemElement = event.target.closest("shopping-item"); - editItem(shoppingItemElement, shoppingItemElement.closest("li").dataset.id); + const id = event.target.closest("li").dataset.id; + + let itemName = shoppingItemElement.querySelector(".item__itemName").textContent; + let itemAmount = shoppingItemElement.querySelector(".item__itemAmount").textContent; + /* + * { name: "", value: "", class: "", attr: ""} + */ + + editElement({ + element: shoppingItemElement, apiCall: api.editItem, id: id, + fields: [ + { name: "itemName", value: itemName, class: "editForm__name", attr: "item-name" }, + { name: "itemAmount", value: itemAmount, class: "editForm__amount", attr: "item-amount" } + ] + , onSuccess: (f) => { sharedUI.setElementAttributes(shoppingItemElement, { "item-name": f.itemName, "item-amount": f.itemAmount }) } + , errmsg: `konnte Item nicht bearbeiten` + }); } else if (event.target.closest(".category__name")) { - const li = event.target.closest("li"); - editCategory(event.target.closest("shopping-category"), li.dataset.id); + const categoryElement = event.target.closest("shopping-category"); + const categoryHeader = event.target.closest(".category__header"); + const id = event.target.closest("li").dataset.id; + + let categoryName = categoryElement.querySelector(".category__name").textContent; + + editElement({ + element: categoryHeader, apiCall: api.editCategory, id: id, + fields: [ + { name: "categoryName", value: categoryName, class: "editForm__name", attr: "category-name" }, + ] + , onSuccess: (f) => { + sharedUI.setElementAttributes(categoryElement, { "category-name": f.categoryName }) + document.querySelector(`option[value='${id}']`).textContent = f.categoryName; + } + , errmsg: `konnte Kategorie nicht bearbeiten` + }); } }); @@ -235,8 +134,8 @@ function init() { }); console.log("javascript initialized!"); - return 0; } + document.addEventListener('DOMContentLoaded', init); diff --git a/src/main/resources/static/js/shared/editing.js b/src/main/resources/static/js/shared/editing.js new file mode 100644 index 0000000..f3762a6 --- /dev/null +++ b/src/main/resources/static/js/shared/editing.js @@ -0,0 +1,29 @@ +import { abortEditing, createEditForm } from "./sharedUI.js"; +import { handleResponse } from "./helper.js"; + +export async function editElement({ element, fields, apiCall, id, onSuccess, errmsg }) { + const formElement = createEditForm(fields); + + element.replaceWith(formElement); + + formElement.querySelector("input").focus(); + + formElement.addEventListener("submit", async (event) => { + event.preventDefault(); + + let formData = Object.fromEntries(new FormData(formElement)); + let result = await handleResponse(apiCall(id, formData), errmsg); + + abortEditing(formElement, element); + + if (result) onSuccess(formData); + }); + + formElement.addEventListener("click", (e) => { + if (e.target.matches(".editForm__buttonAbort")) abortEditing(formElement, element); + }) + + formElement.addEventListener("keydown", (e) => { + if (e.key == "Escape") abortEditing(formElement, element); + }); +} diff --git a/src/main/resources/static/js/shared/helper.js b/src/main/resources/static/js/shared/helper.js new file mode 100644 index 0000000..26f4b29 --- /dev/null +++ b/src/main/resources/static/js/shared/helper.js @@ -0,0 +1,21 @@ +import { showError } from "./error.js"; + +export async function handleResponse(promise, errorMsg) { + const response = await promise; + + //weil 204 kein body ist un damit unparsbar, geben wir vorab true zurück + if (response.status == 204) { + return true; + } + + const json = await response.json(); + + if (!response.ok) { + showError(json.msg); + console.error(errorMsg); + return false; + } + + return json; + +} diff --git a/src/main/resources/static/js/shared/sharedUI.js b/src/main/resources/static/js/shared/sharedUI.js new file mode 100644 index 0000000..71e5437 --- /dev/null +++ b/src/main/resources/static/js/shared/sharedUI.js @@ -0,0 +1,36 @@ +export function abortEditing(formElement, old) { + formElement.replaceWith(old); +} + +export function createEditForm(fields) { + const editForm = document.createElement("form"); + const abortButton = document.createElement("button"); + const saveButton = document.createElement("button"); + + const inputs = fields.map(f => { + const input = document.createElement("input"); + input.setAttribute("name", f.name); + input.setAttribute("value", f.value); + input.classList.add(f.class); + return input; + }); + + abortButton.setAttribute("type", "button"); + abortButton.classList.add("editForm__buttonAbort"); + abortButton.textContent = "Abbrechen"; + + saveButton.textContent = "Speichern"; + saveButton.classList.add("editForm__buttonSave"); + saveButton.setAttribute("type", "submit"); + + + editForm.append(...inputs, saveButton, abortButton); + + return editForm; +} + +export function setElementAttributes(element, attributes) { + + for (const [attr, val] of Object.entries(attributes)) element.setAttribute(attr, val); + +}