diff --git a/src/main/resources/static/js/pages/index/index.js b/src/main/resources/static/js/pages/index/index.js index 56fbad1..068c768 100644 --- a/src/main/resources/static/js/pages/index/index.js +++ b/src/main/resources/static/js/pages/index/index.js @@ -1,6 +1,6 @@ import { ShoppingList } from "../../models/ShoppingList.js"; import { editElement } from "../../shared/editing.js"; -import { initErrors } from "../../shared/error.js"; +import { showError } from "../../shared/error.js"; import { handleResponse } from "../../shared/helper.js"; import { setElementAttributes } from "../../shared/sharedUI.js"; import * as api from "./api/indexAPI.js"; @@ -9,18 +9,23 @@ import * as ui from "./frontend/indexUI.js" const addListForm = document.querySelector(".addListForm"); async function addList(listName) { - const json = await handleResponse(api.addList(listName), `error creating list`); - if (json) ui.addList(json); + const response = await handleResponse(api.addList(listName), `error creating list`); + if (response.ok) { + ui.addList(response.json); + } else { + const name = document.querySelector(".addListForm__inputName"); + showError({ highlightFields: [name], elementForMessage: addListForm, message: response.msg }); + } } + async function deleteList(listElement) { - const json = await handleResponse(api.deleteList(listElement.dataset.id), `error deleting list`); - if (json) ui.deleteList(listElement); + const response = await handleResponse(api.deleteList(listElement.dataset.id), `error deleting list`); + if (response.ok) ui.deleteList(listElement); } function init() { - initErrors(document.querySelector(".nav")); customElements.define("shopping-list", ShoppingList); addListForm.addEventListener("submit", (e) => { @@ -44,6 +49,14 @@ function init() { setElementAttributes(shoppingListElement, { "list-name": f.listName }); //nav wird hier ^ aktualisiert } + , onFailure: (response) => { + + const editForm = document.querySelector(".editForm"); + const name = document.querySelector("input.editForm__name"); + + showError({ highlightFields: [name], elementForMessage: editForm, message: response.json.msg }); + } + , errmsg: `liste konnte nicht aktualsiiert werden` })