diff --git a/WebServer/src/js/Dashboard.kt b/WebServer/src/js/Dashboard.kt new file mode 100644 index 0000000..311161c --- /dev/null +++ b/WebServer/src/js/Dashboard.kt @@ -0,0 +1,19 @@ +import org.w3c.fetch.RequestInit +import kotlin.browser.document +import kotlin.browser.window +import kotlin.js.Json + +class Dashboard { + init { + window.fetch("/senddata/wastedata", RequestInit(method = "POST", body = "action=getStartHeaderData")).then { it -> it.text().then { + println("response text is: "+it) + val json = JSON.parse(it) + document.getElementById("total-connection-labels")?.innerHTML = json["collectionnumber"] as String + document.getElementById("planed-collection-label")?.innerHTML = json["futurecollections"] as String + + document.getElementById("finished-collection-label")?.innerHTML = json["finshedcollections"] as String + + document.getElementById("total-city-number-label")?.innerHTML = json["citynumber"] as String + } } + } +} \ No newline at end of file diff --git a/WebServer/src/js/Index.kt b/WebServer/src/js/Index.kt new file mode 100644 index 0000000..fca4f39 --- /dev/null +++ b/WebServer/src/js/Index.kt @@ -0,0 +1,39 @@ +import org.w3c.dom.HTMLInputElement +import org.w3c.fetch.RequestInit +import kotlin.browser.document +import kotlin.browser.window +import kotlin.js.Json + +class Index { + init { + document.getElementById("loginbtn")?.addEventListener("click", { + it.preventDefault() + println("clicked!!!") + + val username = document.getElementById("userfield") as HTMLInputElement + val passfield = document.getElementById("passfield") as HTMLInputElement + println(username.value) + + console.log("fetch 'data.json' with 'post'") + window.fetch("/senddata/loginget", RequestInit(method = "POST", body = "username=${username.value}&password=${passfield.value}")).then { response -> + response.text().then { text -> + val json = JSON.parse(text) + if (json["status"] == "nodbconn") { + js("""Swal.fire({ + type: "error", + title: 'No connection to Database', + html: 'Setup DB here --> click.' + })""") + } + + if (json["accept"] == true) { + println("successfully logged in!") + document.cookie = "username=$username" + window.location.replace("dashboard.html") + } + } + } + }) + } + // todo register pwa correctly +} \ No newline at end of file diff --git a/WebServer/src/js/Main.kt b/WebServer/src/js/Main.kt index a43d3e8..ff95fe0 100644 --- a/WebServer/src/js/Main.kt +++ b/WebServer/src/js/Main.kt @@ -1,33 +1,18 @@ -import org.w3c.dom.HTMLInputElement -import org.w3c.fetch.RequestInit -import org.w3c.xhr.XMLHttpRequest -import kotlin.browser.document import kotlin.browser.window fun main() { val callurl = window.document.URL - if(callurl.contains("sec.html")){ - document.write("Hello, in document 2!") - }else { - window.onload = { + window.onload = { + if (callurl.endsWith("/") || callurl.contains("index.html")) { println("loaded sucessfully") - document.getElementById("loginbtn")?.addEventListener("click",{ - it.preventDefault() - println("clicked!!!") - - val username = document.getElementById("userfield") as HTMLInputElement - val passfield = document.getElementById("passfield") as HTMLInputElement - println(username.value) - - console.log("fetch 'data.json' with 'post'") - window.fetch("/senddata/loginget", RequestInit(method = "POST", body = "username=${username.value}&password=${passfield.value}")).then { - it.text().then { println(it) } - } - }) + Index() + } + else if (callurl.contains("dashboard.html")) { + Dashboard() + } + else { + println("js called from undefined html file") } - document.write("Hello, world!") - - val req = Requester() - req.request() } + } \ No newline at end of file diff --git a/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java b/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java index b061403..00bf86a 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java +++ b/src/java/com/wasteinformationserver/website/datarequests/login/LoginState.java @@ -17,7 +17,7 @@ public class LoginState { private String email; private int permission; - boolean loggedin = false; + boolean loggedin = true; public void logIn() { loggedin = true; diff --git a/src/resources/wwwroot/dashboard.html b/src/resources/wwwroot/dashboard.html index d2d8e1d..ff57123 100644 --- a/src/resources/wwwroot/dashboard.html +++ b/src/resources/wwwroot/dashboard.html @@ -448,8 +448,8 @@ - - + +