class Post { #data; #HTMLElement = document.createElement("post-anzeigen"); #HTMLElementToEdit = document.createElement("post-bearbeiten"); get node() { return this.#HTMLElement; } get nodeToEdit() { return this.#HTMLElementToEdit; } get data() { return this.#data; } set data(data) { this.#data = data || {}; this.node.data = this.#data; this.nodeToEdit.data = this.#data; } constructor({ token, id, data } = {}) { this.#HTMLElement.object = this; this.#HTMLElementToEdit.object = this; if (token || id) { const query = token ? `token=${encodeURIComponent(token)}` : id ? `id=${encodeURIComponent(id)}` : ""; fetch(`/api/post/?${query}`) .then((response) => response.json().then((json) => ({ response, json })).catch(() => ({ response, json: null }))) .then(({ response, json }) => { if (response.ok) { this.data = json.data || {}; } else { const errorMessage = json?.error || response.statusText; console.error("Fehler beim Laden des Beitrags:", errorMessage); } }) .catch((error) => { console.error("Fehler beim Laden des Beitrags:", error); }); } else { this.data = data || {}; } } save() { return fetch("/api/post/save/", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(this.#data), }) .then((response) => response.json().then((result) => ({ response, result }))) .then(({ response, result }) => { if (response.ok) { return result; } else { const errorMessage = result.error || response.statusText; console.error("Fehler beim Speichern des Posts:", errorMessage); return Promise.reject(new Error(errorMessage)); } }) .catch((error) => { console.error("Netzwerkfehler beim Speichern des Posts:", error); return Promise.reject(error); }); } } { const template = `