final version?
This commit is contained in:
@@ -1,20 +1,40 @@
|
||||
export class PresentsHandler {
|
||||
presents: {
|
||||
isBought: boolean,
|
||||
image: string,
|
||||
name: string,
|
||||
description: string
|
||||
link: string
|
||||
}[] = [];
|
||||
[id: number]: {
|
||||
id: number,
|
||||
version: number,
|
||||
isBought: boolean,
|
||||
image: string,
|
||||
name: string,
|
||||
description: string
|
||||
link: string
|
||||
}
|
||||
} = {};
|
||||
|
||||
// @ts-ignore
|
||||
private readonly basePath = __BASE_PATH__;
|
||||
|
||||
async loadPresents() {
|
||||
this.presents = require("./presents").default;
|
||||
const presents = require("./presents").default;
|
||||
presents.forEach(p => this.presents[p.id] = p);
|
||||
await this.updateStates();
|
||||
}
|
||||
|
||||
async updateStates() {
|
||||
//TODO vom server laden
|
||||
this.presents[3].isBought = true;
|
||||
const presentData: any[] = await fetch(this.basePath + "/presents", {
|
||||
"credentials": "same-origin",
|
||||
"method": "GET",
|
||||
}).then(function (res) {
|
||||
return res.json();
|
||||
});
|
||||
|
||||
presentData.forEach(data => {
|
||||
const present = this.presents[data.id];
|
||||
if (present){
|
||||
present.isBought = data.isBought === 1;
|
||||
present.version = data.version;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async showPresents() {
|
||||
@@ -26,8 +46,7 @@ export class PresentsHandler {
|
||||
await this.loadPresents();
|
||||
|
||||
presentContainer.innerText = "";
|
||||
console.log(this.presents);
|
||||
this.presents.forEach(present => {
|
||||
Object.values(this.presents).forEach(present => {
|
||||
const element = <HTMLLinkElement>presentTemplate.cloneNode(true);
|
||||
(<HTMLImageElement>element.querySelector(".present-image")).src = present.image;
|
||||
(<HTMLElement>element.querySelector(".present-name")).innerText = present.name;
|
||||
@@ -53,8 +72,56 @@ export class PresentsHandler {
|
||||
})
|
||||
}
|
||||
|
||||
private async setPresentIsBought(present: { isBought: boolean; image: string; name: string; description: string; link: string }, isBought: boolean) {
|
||||
//TODO auf Server updaten
|
||||
present.isBought = isBought;
|
||||
private async setPresentIsBought(present: { id: number, isBought: boolean; version: number, image: string; name: string; description: string; link: string }, isBought: boolean) {
|
||||
const url = this.basePath + "/presents"
|
||||
|
||||
const params = {
|
||||
id: present.id,
|
||||
version: present.version,
|
||||
isBought: isBought
|
||||
}
|
||||
|
||||
const res = await this.send(url, params);
|
||||
|
||||
console.log("result", res);
|
||||
|
||||
if (res.present && res.present.id === present.id) {
|
||||
present.version = res.present.version;
|
||||
present.isBought = res.present.isBought === 1
|
||||
}
|
||||
|
||||
if (res.success === false) {
|
||||
if (res.error === "wrong-version") {
|
||||
alert("Jemand hat schon vor dir das Geschenk bearbeitet. Bitte versuche es erneut.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async send(url, params) {
|
||||
let headers = {};
|
||||
if (!(params instanceof FormData) && typeof params === "object") {
|
||||
params = JSON.stringify(params);
|
||||
headers = {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
}
|
||||
|
||||
return fetch(url, {
|
||||
"credentials": "same-origin",
|
||||
"method": "POST",
|
||||
"headers": headers,
|
||||
"body": params,
|
||||
}).then(function (res) {
|
||||
return res.json();
|
||||
}).catch(function (e) {
|
||||
debugger;
|
||||
console.error("error", e);
|
||||
return {
|
||||
"success": false,
|
||||
"errors": [
|
||||
"not-online"
|
||||
]
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,6 @@ const images = {
|
||||
geschenke: require("../img/geschenke.jpg").default,
|
||||
}
|
||||
|
||||
|
||||
const callbacks = {
|
||||
home: () => {
|
||||
},
|
||||
@@ -54,6 +53,7 @@ const callbacks = {
|
||||
|
||||
const navBar = document.querySelector(".top-bar");
|
||||
const anchors = document.querySelectorAll(".anchor");
|
||||
|
||||
const margin = navBar.clientHeight + 50;
|
||||
|
||||
const observerDown = new IntersectionObserver(entries => {
|
||||
@@ -63,7 +63,6 @@ const callbacks = {
|
||||
}
|
||||
});
|
||||
}, {threshold: 1, rootMargin: "-" + margin + "px 0px 0px 0px"});
|
||||
// }, {threshold: 1, rootMargin: "-100px 0px 0px 0px"});
|
||||
const observerUp = new IntersectionObserver(entries => {
|
||||
entries.some(entry => {
|
||||
if (entry.isIntersecting && entry.intersectionRect.top === navBar.clientHeight && !scrollToView) {
|
||||
@@ -95,7 +94,6 @@ $(document).foundation();
|
||||
$(function () {
|
||||
const container = document.getElementById("main-content");
|
||||
const img = document.getElementById("home-img-container");
|
||||
const navbar = document.getElementById("main-menu");
|
||||
|
||||
let currentSite = "home";
|
||||
|
||||
@@ -130,4 +128,10 @@ $(function () {
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
document.querySelectorAll(".logo-img").forEach(img => {
|
||||
img.addEventListener("click", () => {
|
||||
document.querySelector("li[data-site='home']").dispatchEvent(new Event("click"));
|
||||
})
|
||||
})
|
||||
});
|
||||
@@ -1,49 +1,65 @@
|
||||
export default [{
|
||||
id: 1,
|
||||
isBought: false,
|
||||
image: require("../img/geschenke/test.jpg").default,
|
||||
name: "Akku-Bohrer",
|
||||
description: "Der von BOSCH, weil ich den schon immer mal haben wollte",
|
||||
link: "http://www.onlinewahn.de/ende.htm",
|
||||
version: 0,
|
||||
}, {
|
||||
isBought: true,
|
||||
id: 2,
|
||||
isBought: false,
|
||||
image: require("../img/geschenke/test.jpg").default,
|
||||
name: "Windows",
|
||||
description: "Bisher konnte ich es mir nicht leisten und musste immer auf eine Web-Version zurückgreifen",
|
||||
link: "https://www.windows93.net/",
|
||||
version: 0,
|
||||
},{
|
||||
id: 3,
|
||||
isBought: false,
|
||||
image: require("../img/geschenke/test.jpg").default,
|
||||
name: "Akku-Bohrer",
|
||||
description: "Der von BOSCH, weil ich den schon immer mal haben wollte",
|
||||
link: "http://www.onlinewahn.de/ende.htm",
|
||||
version: 0,
|
||||
}, {
|
||||
id: 4,
|
||||
isBought: false,
|
||||
image: require("../img/geschenke/test.jpg").default,
|
||||
name: "Windows",
|
||||
description: "Bisher konnte ich es mir nicht leisten und musste immer auf eine Web-Version zurückgreifen",
|
||||
link: "https://www.windows93.net/",
|
||||
version: 0,
|
||||
},{
|
||||
id: 5,
|
||||
isBought: false,
|
||||
image: require("../img/geschenke/test.jpg").default,
|
||||
name: "Akku-Bohrer",
|
||||
description: "Der von BOSCH, weil ich den schon immer mal haben wollte",
|
||||
link: "http://www.onlinewahn.de/ende.htm",
|
||||
version: 0,
|
||||
}, {
|
||||
id:6,
|
||||
isBought: false,
|
||||
image: require("../img/geschenke/test.jpg").default,
|
||||
name: "Windows",
|
||||
description: "Bisher konnte ich es mir nicht leisten und musste immer auf eine Web-Version zurückgreifen",
|
||||
link: "https://www.windows93.net/",
|
||||
version: 0,
|
||||
},{
|
||||
id:7,
|
||||
isBought: false,
|
||||
image: require("../img/geschenke/test.jpg").default,
|
||||
name: "Akku-Bohrer",
|
||||
description: "Der von BOSCH, weil ich den schon immer mal haben wollte",
|
||||
link: "http://www.onlinewahn.de/ende.htm",
|
||||
version: 0,
|
||||
}, {
|
||||
id: 8,
|
||||
isBought: false,
|
||||
image: require("../img/geschenke/test.jpg").default,
|
||||
name: "Windows",
|
||||
description: "Bisher konnte ich es mir nicht leisten und musste immer auf eine Web-Version zurückgreifen",
|
||||
link: "https://www.windows93.net/",
|
||||
version: 0,
|
||||
},]
|
||||
Reference in New Issue
Block a user