From c76da12b1af36e0450ce8c5b2d1eb629104bd5c3 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Fri, 7 Apr 2023 13:33:09 -0600 Subject: [PATCH] gui done enough --- http.go | 18 +++++++++++------- public/root.html | 26 +++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/http.go b/http.go index 7f458e5..845d850 100644 --- a/http.go +++ b/http.go @@ -18,10 +18,11 @@ type Context struct { func HTTP(port int, db DB) error { foo := func(w http.ResponseWriter, r *http.Request) { - switch r.URL.Path { - case "/": - httpRoot(w, r) - default: + if r.URL.Path == "/" { + httpGUI(w, r) + } else if strings.HasPrefix(r.URL.Path, "/api/questions") && strings.HasSuffix(r.URL.Path, "/answers") && r.Method == http.MethodPost { + httpPostQuestionAnswers(w, r) + } else { http.NotFound(w, r) } } @@ -65,10 +66,10 @@ func withAuth(foo http.HandlerFunc) http.HandlerFunc { } //go:embed public/root.html -var httpRootHTML string +var httpGUIHTML string -func httpRoot(w http.ResponseWriter, r *http.Request) { - body := httpRootHTML +func httpGUI(w http.ResponseWriter, r *http.Request) { + body := httpGUIHTML if os.Getenv("DEBUG") != "" { b, _ := os.ReadFile("public/root.html") body = string(b) @@ -97,3 +98,6 @@ func httpAssignments(ctx context.Context) (interface{}, error) { } return todo, nil } + +func httpPostQuestionAnswers(w http.ResponseWriter, r *http.Request) { +} diff --git a/public/root.html b/public/root.html index 9bfb10f..38479f6 100644 --- a/public/root.html +++ b/public/root.html @@ -71,7 +71,14 @@ } function pushAnswer(idq, answer, passed) { - console.log(idq, answer, passed) + http("post", `/api/questions/${idq}/answers`, noopcallback, + JSON.stringify( + { + "answer": answer, + "passed": new Boolean(passed), + } + ), + ) } function nextQuestion(form) { @@ -110,6 +117,23 @@ form.children.answer.retake = todo[1].Retake } + function noopcallback(responseBody, responseStatus) { + console.log(responseStatus, responseBody) + } + function http(method, remote, callback, body) { + var xmlhttp = new XMLHttpRequest(); + xmlhttp.onreadystatechange = function() { + if (xmlhttp.readyState == XMLHttpRequest.DONE) { + callback(xmlhttp.responseText, xmlhttp.status) + } + }; + xmlhttp.open(method, remote, true); + if (typeof body == "undefined") { + body = null + } + xmlhttp.send(body); + } + nextQuestion(document.getElementById("flash"))