summaryrefslogtreecommitdiff
path: root/client/src/adminArticle.js
blob: bc50915475691f710f9049d6f9c5371debaa60b1 (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
import {
    AdminAttraction
} from "./templateImplementations.js"

import {
    kill,
    findParent
} from "./utils.js"

export function addAttractionInDatabase(newattraction) {

    return async function addAttraction(event) {
        const name = newattraction.querySelector("#newattractionname").value;
        const description = newattraction.querySelector("#newattractiondescription").value;
        const lat = Number.parseFloat(newattraction.querySelector("#newlat").value);
        const lon = Number.parseFloat(newattraction.querySelector("#newlong").value);
        if (!name || !description) return;
        if (Number.isNaN(lat)) return;
        if (Number.isNaN(lon)) return;

        console.log("adding " + name + " at lat: " + lat + " and long: " + lon);
        try {
            const response = await fetch("api/admin/add",
                {
                    method: 'POST', // *GET, POST, PUT, DELETE, etc.
                    mode: 'cors', // no-cors, *cors, same-origin
                    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
                    credentials: 'same-origin', // include, *same-origin, omit
                    headers: {
                      'Content-Type': 'application/json'
                      // 'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    redirect: 'follow', // manual, *follow, error
                    referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
                    body: JSON.stringify({name: name, description: description, lat: lat, lon: lon}) // body data type must match "Content-Type" header
              });

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

            const main = findParent(parent => {return parent.tagName === "MAIN"})(event.target)
            const template = main.parentNode.querySelector("#adminattraction");
            const attractionJSON = await response.json();
            console.log(attractionJSON)
            const newAttraction = new AdminAttraction(attractionJSON, template);
            newAttraction.addToNode(main);

        } catch(error) {
            console.log("Something went wrong when deleting an attraction: ", error);
        }

    }
}

export function deleteAttractionInDatabase(name) {
    return async function deleteAttraction(event) {
        console.log("deleting " + name);
        try {
            const response = await fetch("api/admin/delete",
                {
                    method: 'POST', // *GET, POST, PUT, DELETE, etc.
                    mode: 'cors', // no-cors, *cors, same-origin
                    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
                    credentials: 'same-origin', // include, *same-origin, omit
                    headers: {
                      'Content-Type': 'application/json'
                      // 'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    redirect: 'follow', // manual, *follow, error
                    referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
                    body: JSON.stringify({name: name}) // body data type must match "Content-Type" header
              });

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

        } catch(error) {
            console.log("Something went wrong when deleting an attraction: ", error);
        }
        kill(findParent(parent => {return parent.tagName === "ARTICLE"})(event.target));
    }
}


export function updateAttractionInDatabase(name, field) {
    return async function update(event) {
        console.log("updating " + field + " field of " + name);
        try {
            const response = await fetch("api/admin/edit",
                {
                    method: 'POST', // *GET, POST, PUT, DELETE, etc.
                    mode: 'cors', // no-cors, *cors, same-origin
                    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
                    credentials: 'same-origin', // include, *same-origin, omit
                    headers: {
                      'Content-Type': 'application/json'
                      // 'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    redirect: 'follow', // manual, *follow, error
                    referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
                    body: JSON.stringify({name: name, field: field, value: event.target.value}) // body data type must match "Content-Type" header
              });

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

        } catch(error) {
            console.log("Something went wrong when editing an attraction: ", error);
        }
    }
}