diff options
| author | Mike Vink <mike1994vink@gmail.com> | 2021-06-10 18:41:21 +0200 |
|---|---|---|
| committer | Mike Vink <mike1994vink@gmail.com> | 2021-06-10 18:41:21 +0200 |
| commit | 22ee6924ca6a59ecf4ca0030daab302f697b3445 (patch) | |
| tree | 7990b4cf02754ca42e27585d28dd67fddaaf0bf3 /client/src/adminArticle.js | |
| parent | fd46c28539cf7c130dfbb0458f7b9057ad4f1e46 (diff) | |
big progress
Diffstat (limited to 'client/src/adminArticle.js')
| -rw-r--r-- | client/src/adminArticle.js | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/client/src/adminArticle.js b/client/src/adminArticle.js new file mode 100644 index 0000000..bc50915 --- /dev/null +++ b/client/src/adminArticle.js @@ -0,0 +1,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); + } + } +} |
