Compare commits
5
Commits
7af099f022
...
0b69384fb7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b69384fb7 | ||
|
|
2d19d9a06f | ||
|
|
70338266f2 | ||
|
|
00ac28aebf | ||
|
|
c9c38f07ad |
No files matched your search
@@ -1,4 +1,3 @@
|
||||
|
||||
export class ShoppingCategory extends HTMLElement {
|
||||
|
||||
static get observedAttributes() { return ["category-name"] }
|
||||
|
||||
@@ -7,7 +7,6 @@ export async function addCategory(listID, categoryName) {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export async function editCategory(categoryID, toChange) {
|
||||
return await fetch(`/api/categories/${categoryID}`,
|
||||
{
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
export async function addItem(listID, itemName, itemAmount, categoryID) {
|
||||
return await fetch(`/api/items`,
|
||||
{
|
||||
@@ -14,7 +12,6 @@ export async function addItem(listID, itemName, itemAmount, categoryID) {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export async function deleteItem(itemID) {
|
||||
|
||||
return await fetch(`/api/items/${itemID}`, { method: "DELETE" });
|
||||
@@ -32,9 +29,5 @@ export async function editItem(itemID, toChange) {
|
||||
}
|
||||
|
||||
export async function deleteAllDoneItems(listID) {
|
||||
|
||||
return await fetch(`/api/lists/${listID}/items?done=true`, { method: "DELETE" });
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -18,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.itemName;
|
||||
let newAmount = formData.itemAmount;
|
||||
shoppingItemElement.setAttribute("item-name", newName);
|
||||
shoppingItemElement.setAttribute("item-amount", newAmount);
|
||||
|
||||
|
||||
formElement.replaceWith(shoppingItemElement);
|
||||
|
||||
}
|
||||
|
||||
export function deleteItem(li) {
|
||||
li.remove();
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ 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";
|
||||
import * as sharedAPI from "../../shared/sharedAPI.js";
|
||||
|
||||
const addCategoryForm = document.querySelector(".addCategoryForm");
|
||||
const addItemForm = document.querySelector(".addItemForm");
|
||||
@@ -27,34 +27,6 @@ async function addCategory(categoryName) {
|
||||
|
||||
}
|
||||
|
||||
async function editElement({ element, fields, apiCall, id, onSuccess, errmsg }) {
|
||||
const formElement = sharedUI.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);
|
||||
|
||||
sharedUI.abortEditing(formElement, element);
|
||||
|
||||
if (result) onSuccess(formData);
|
||||
});
|
||||
|
||||
formElement.addEventListener("click", (e) => {
|
||||
if (e.target.matches(".editForm__buttonAbort")) sharedUI.abortEditing(formElement, element);
|
||||
})
|
||||
|
||||
formElement.addEventListener("keydown", (e) => {
|
||||
if (e.key == "Escape") sharedUI.abortEditing(formElement, element);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
async function deleteItem(li) {
|
||||
const json = await handleResponse(api.deleteItem(li.dataset.id), `error deleting item`);
|
||||
|
||||
@@ -128,7 +100,6 @@ function init() {
|
||||
, onSuccess: (f) => { sharedUI.setElementAttributes(shoppingItemElement, { "item-name": f.itemName, "item-amount": f.itemAmount }) }
|
||||
, errmsg: `konnte Item nicht bearbeiten`
|
||||
});
|
||||
//editItem(shoppingItemElement, shoppingItemElement.closest("li").dataset.id);
|
||||
|
||||
} else if (event.target.closest(".category__name")) {
|
||||
|
||||
@@ -146,7 +117,6 @@ function init() {
|
||||
, onSuccess: (f) => {
|
||||
sharedUI.setElementAttributes(categoryElement, { "category-name": f.categoryName })
|
||||
document.querySelector(`option[value='${id}']`).textContent = f.categoryName;
|
||||
//option name ändern
|
||||
}
|
||||
, errmsg: `konnte Kategorie nicht bearbeiten`
|
||||
});
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
export async function editElement({ element, fields, apiCall, id, onSuccess, errmsg }) {
|
||||
const formElement = sharedUI.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);
|
||||
|
||||
sharedUI.abortEditing(formElement, element);
|
||||
|
||||
if (result) onSuccess(formData);
|
||||
});
|
||||
|
||||
formElement.addEventListener("click", (e) => {
|
||||
if (e.target.matches(".editForm__buttonAbort")) sharedUI.abortEditing(formElement, element);
|
||||
})
|
||||
|
||||
formElement.addEventListener("keydown", (e) => {
|
||||
if (e.key == "Escape") sharedUI.abortEditing(formElement, element);
|
||||
});
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import { showError } from "./error.js";
|
||||
|
||||
|
||||
export async function handleResponse(promise, errorMsg) {
|
||||
const response = await promise;
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
export async function editElement(apiPath, toChange) {
|
||||
return await fetch(apiPath,
|
||||
{
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(toChange),
|
||||
headers: { "Content-Type": "application/json" }
|
||||
});
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
|
||||
export function abortEditing(formElement, old) {
|
||||
formElement.replaceWith(old);
|
||||
}
|
||||
|
||||
|
||||
export function createEditForm(fields) {
|
||||
const editForm = document.createElement("form");
|
||||
const abortButton = document.createElement("button");
|
||||
@@ -31,10 +29,8 @@ export function createEditForm(fields) {
|
||||
return editForm;
|
||||
}
|
||||
|
||||
|
||||
export function setElementAttributes(element, attributes) {
|
||||
|
||||
for (const [attr, val] of Object.entries(attributes)) element.setAttribute(attr, val);
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user