summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--components/index.html2
-rw-r--r--internal/handlers/main.go17
-rw-r--r--internal/server/router.go1
3 files changed, 19 insertions, 1 deletions
diff --git a/components/index.html b/components/index.html
index 5626dbd..0c51047 100644
--- a/components/index.html
+++ b/components/index.html
@@ -17,7 +17,7 @@
{{range $index, $option := .Options}}
<button
hx-post="/answer"
- hx-vals='{"selected": "{{$index}}"}'
+ hx-vals='{"selected": "{{$index}}", "question_id": "{{.ID}}"}'
hx-target="#ancestor"
hx-swap="outerHTML"
class="option-button"
diff --git a/internal/handlers/main.go b/internal/handlers/main.go
index 3f2e1b2..1f3f740 100644
--- a/internal/handlers/main.go
+++ b/internal/handlers/main.go
@@ -37,6 +37,23 @@ func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
}
+func (h *Handlers) HandleAnswer(w http.ResponseWriter, r *http.Request) {
+ if err := r.ParseForm(); err != nil {
+ abortWithError(w, "Invalid form data")
+ return
+ }
+
+ selectedIndex := r.FormValue("selected")
+ questionID := r.FormValue("question_id")
+ h.log.Info("answer received",
+ "selected", selectedIndex,
+ "question_id", questionID)
+
+ // TODO: Add actual answer validation logic here
+ // For now just return the same question
+ h.MainPage(w, r)
+}
+
func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseGlob("components/*.html")
if err != nil {
diff --git a/internal/server/router.go b/internal/server/router.go
index 7034103..43a29b9 100644
--- a/internal/server/router.go
+++ b/internal/server/router.go
@@ -22,6 +22,7 @@ func (srv *server) ListenToRequests() {
mux.HandleFunc("GET /ping", h.Ping)
mux.HandleFunc("GET /", h.MainPage)
+ mux.HandleFunc("POST /answer", h.HandleAnswer)
// mux.HandleFunc("POST /login", h.HandleLogin)
// mux.HandleFunc("POST /signup", h.HandleSignup)