summaryrefslogtreecommitdiff
path: root/client/src/shoppingbasket.js
blob: c23c02cef814518fa16f61d2e1d8a5cbb0950afb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
document.querySelector(".badge").innerText = localStorage.length;

function getOrderArray() {
    var orders = new Array;
    for (let i = 0; i < localStorage.length; i++) {
        var order = localStorage.getItem(i+1);
        order = JSON.parse(order);
        orders.push(order);
    }
    // console.log(orders);
    return orders;
}

class Order {
    constructor(orderJSON) {
        for (const [key, value] of Object.entries(orderJSON)) {
            this[key] = value;
        }

    }

    addToMain() {
        // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template
        var main = document.querySelector("main");
        var template = document.querySelector("#ticket");

        var clone = template.content.cloneNode(true);
        // console.log(clone);
        var lines = clone.querySelectorAll("div");

        for (var i = 0; i < lines.length; i++) {
            var text = lines[i].textContent;

            if (text === "Parkname") {
                console.log(this.name);
                lines[i].textContent = this.name;
            }

            if (text.toLowerCase().includes("adults")) {
                lines[i].textContent = text + " " + this.adults;
            }

            if (text.toLowerCase().includes("kids")) {
                lines[i].textContent = text + " " + this.children;
            }
        }

        main.appendChild(clone);
    }
}


function displayOrders() {
    var orders = getOrderArray();
    for (let i = 0; i < orders.length; i++) {
        orderObj = new Order(orders[i]);
        orderObj.addToMain();
    }

}

displayOrders();

function finalizePayment(event) {
    console.log("finalizing payments");
    localStorage.clear();
    window.location.replace("orderplaced.html");
}

document.querySelector("#finalizepaymentbutton").addEventListener("click", finalizePayment);