summaryrefslogtreecommitdiff
path: root/client/src/utils.js
blob: b088f730a661b0f17b89c161717c2aa510b721f4 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// client/src/function.js
export async function fetchOrders() {

    try {

        const response = await fetch("api/myorders");

        if (!response.ok) {
            const message = `An error has occured: ${response.status}`;
            throw new Error(message);
        }

        const orders = await response.json();
        return orders;


    } catch(error) {
        console.log("something went wrong when fetching orders: ", error);
    }

}

export async function fetchAttractions() {

    try {

        const response = await fetch("api/attractions");

        if (!response.ok) {
            const message = `An error has occured: ${response.status}`;
            throw new Error(message);
        }

        const attractions = await response.json();
        return attractions;


    } catch(error) {
        console.log("something went wrong when fetching attractions: ", error);
    }

}

export function readOrderArrayFromLocalStorage() {
    var orders = JSON.parse(localStorage.getItem("shoppingBasketArray"));
    return orders;
}


export function displayNumberOfItemsInShoppingBasketWithBadge() {
    var shoppingBasketArray = JSON.parse(localStorage.getItem("shoppingBasketArray"));
    if (shoppingBasketArray === null) {
        document.querySelector(".badge").innerText = 0;
    } else {
        document.querySelector(".badge").innerText = shoppingBasketArray.length;
    }
}

export function dutchCurrencyFormat(number) {
    var decimal = (number * 10) % 10
    if (decimal === 0) return number + ",-";
    else {
        return (number * 10 - decimal) / 10 + "," + decimal;
    }
}

export function dutchCurrencyFormatWithSign(number) {
    return "\u20AC" + dutchCurrencyFormat(number);
}


export function findParent(func) {
    return function startingFromThisNode(node) {
        if (func(node)) {
            return node;
        } else {
            return startingFromThisNode(node.parentNode);
        }
    }
}

export function kill(node) {
    node.parentNode.removeChild(node);
}

export function killChildren(func) {
    return function givenTheParent(parent) {
        console.log("removing children of ")
        console.log(parent)
        function removeParentsChildren(child) {
            if (child === null) return;
            else if (func(child)) {
                const next = child.nextSibling;
                parent.removeChild(child);
                return removeParentsChildren(next);
            } else {
                return removeParentsChildren(child.nextSibling);
            }
        }
        removeParentsChildren(parent.firstChild)
    }
}

export function getUserGeoLocation() {
    if (navigator.geolocation) console.log("browser supports geolocation")

    const getCoords = async () => {
        const pos = await new Promise((resolve, reject) => {
          navigator.geolocation.getCurrentPosition(resolve, reject);
        });

        return {
          long: pos.coords.longitude,
          lat: pos.coords.latitude,
        };
    };

    return getCoords();
}

export function distanceToUser(userLocation) {
    return function calculateDistanceBasedOnLatLon(lat, lon) {
        const R = 6371e3; // metres
        const phi1 = userLocation.lat * Math.PI/180; // φ, λ in radians
        const phi2 = lat * Math.PI/180;
        const diffPhi = (lat-userLocation.lat) * Math.PI/180;
        const diffLambda = (lon-userLocation.long) * Math.PI/180;

        const a = Math.sin(diffPhi/2) * Math.sin(diffPhi/2) +
                  Math.cos(phi1) * Math.cos(phi2) *
                  Math.sin(diffLambda/2) * Math.sin(diffLambda/2);
        const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

        const d = R * c; // in metres
        return d;
    }
}

export function bubbleSort(array, key, compare) {
    const sorted = [...array];
    console.log("original: " + array);
    console.log("copy: " + sorted);

    var tmp;
    for (let i = 0; i < array.length; i++) {
        for (let j = 1; j < array.length - i; j++) {

            if (compare(key(sorted[j - 1]), key(sorted[j]))) {
                tmp = sorted[j];
                sorted[j] = sorted[j-1];
                sorted[j - 1] = tmp;
            }


        }
    }
    return sorted;
}