diff --git a/public/index.html b/public/index.html index 95e8d88..e35ab48 100644 --- a/public/index.html +++ b/public/index.html @@ -15,48 +15,115 @@ -
+
+ +
-

Units (Räume/Objekte)

-
- - - +

+ Units (Räume/Objekte) + +

+ + +

Neue Unit anlegen

+ +
+ + +
+ +

Eigenschaften (Traits)

+
+ + + + +
+ +
-
    -
  • -
    - - + +
      +
    • + +
      +
      + +
      + ↳ Befindet sich in: +
      +
      +
      - - + +
      + + +
      + +
      + +
      + + +
      +
      + + +
      +
      + 🛏 + +
      +
      + +
    +
    -

    Events (Buchungen/Aufgaben)

    -
    - +
    - - + +
    -
      -
    • + +
        +
      • - - Unit: + +
        + Verknüpft mit: +
        - +
    +
@@ -68,35 +135,66 @@ data() { return { units: [], events: [], - newUnit: { name: "", traits: "{}" }, + newUnit: { name: "", parentId: "", traitsObj: { is_rentable: true, base_price: null, area_sqm: null, sleep_capacity: null } }, newEvent: { unitId: "", type: "booking" }, errorMsg: null } }, methods: { async fetchUnits() { - const res = await fetch("/api/units"); - const data = await res.json(); - this.units = data.map(u => ({ ...u, traitsRaw: JSON.stringify(u.traits || {}, null, 2) })); + try { + const res = await fetch("/api/units"); + const data = await res.json(); + this.units = data.map(u => ({ + ...u, + traitsObj: { + is_rentable: u.traits?.is_rentable || false, + base_price: u.traits?.base_price || null, + area_sqm: u.traits?.area_sqm || null, + sleep_capacity: u.traits?.sleep_capacity || null, + ...(u.traits || {}) + } + })); + } catch (e) { this.errorMsg = "Fehler beim Laden der Units: " + e.message; } }, async fetchEvents() { - const res = await fetch("/api/events"); - this.events = await res.json(); + try { + const res = await fetch("/api/events"); + this.events = await res.json(); + } catch (e) { this.errorMsg = "Fehler beim Laden der Events: " + e.message; } }, getUnitName(id) { const u = this.units.find(u => u.id === id); return u ? u.name : id; }, async createUnit() { - const body = { name: this.newUnit.name, traits: JSON.parse(this.newUnit.traits || "{}") }; - await fetch("/api/units", { method: "POST", headers: {"Content-Type":"application/json"}, body: JSON.stringify(body) }); - this.newUnit.name = ""; - this.fetchUnits(); + try { + const traits = { ...this.newUnit.traitsObj }; + Object.keys(traits).forEach(k => traits[k] === null && delete traits[k]); + + const body = { + name: this.newUnit.name, + parentId: this.newUnit.parentId || null, + traits + }; + await fetch("/api/units", { method: "POST", headers: {"Content-Type":"application/json"}, body: JSON.stringify(body) }); + this.newUnit = { name: "", parentId: "", traitsObj: { is_rentable: true, base_price: null, area_sqm: null, sleep_capacity: null } }; + this.fetchUnits(); + } catch (e) { alert(e.message); } }, async updateUnit(unit) { - const body = { name: unit.name, traits: JSON.parse(unit.traitsRaw) }; - await fetch(`/api/units/${unit.id}`, { method: "PUT", headers: {"Content-Type":"application/json"}, body: JSON.stringify(body) }); - alert("Gespeichert!"); + try { + const traits = { ...unit.traitsObj }; + Object.keys(traits).forEach(k => traits[k] === null && delete traits[k]); + + const body = { + name: unit.name, + parentId: unit.parentId || null, + traits + }; + await fetch(`/api/units/${unit.id}`, { method: "PUT", headers: {"Content-Type":"application/json"}, body: JSON.stringify(body) }); + this.fetchUnits(); + } catch (e) { alert(e.message); } }, async deleteUnit(id) { await fetch(`/api/units/${id}`, { method: "DELETE" }); @@ -105,6 +203,7 @@ async createEvent() { const body = { unitId: this.newEvent.unitId, type: this.newEvent.type, traits: {} }; await fetch("/api/events", { method: "POST", headers: {"Content-Type":"application/json"}, body: JSON.stringify(body) }); + this.newEvent.type = "booking"; this.fetchEvents(); }, async deleteEvent(id) { @@ -116,9 +215,9 @@ this.fetchUnits(); this.fetchEvents(); } - }).mount("#app") + }).mount("#app"); } catch (err) { - document.body.innerHTML += "
Error: " + err.message + "
"; + document.body.innerHTML += "
Error: " + err.message + "
"; } });