From c2d82a17832590d86c7c243671b64df95913350e Mon Sep 17 00:00:00 2001
From: "Grail Finder (aider)" <wohilas@gmail.com>
Date: Sat, 5 Apr 2025 17:36:57 +0300
Subject: refactor: track question status using Status field instead of
 showNext flag

---
 components/index.html     | 2 +-
 internal/handlers/main.go | 8 ++------
 2 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/components/index.html b/components/index.html
index 50839f4..3efca24 100644
--- a/components/index.html
+++ b/components/index.html
@@ -15,7 +15,7 @@
             </div>
             <div id="feedback" data-testid="feedback">
               {{if .Correct}}{{template "feedback" .}}{{else if .Requirement}}{{template "feedback" .}}{{end}}
-              {{if .ShowNext}}
+              {{if ne .Status 0}}
               <button 
                   hx-get="/next-question?current_id={{.ID}}" 
                   hx-target="#ancestor"
diff --git a/internal/handlers/main.go b/internal/handlers/main.go
index 4a64380..97a54cd 100644
--- a/internal/handlers/main.go
+++ b/internal/handlers/main.go
@@ -16,7 +16,6 @@ type Handlers struct {
 	cfg      config.Config
 	log      *slog.Logger
 	repo     repos.FullRepo
-	showNext bool // Tracks when to show next button
 }
 
 // NewHandlers constructor
@@ -80,11 +79,12 @@ func (h *Handlers) HandleAnswer(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 	selectedIdx++ // in db index starts from 1
-	h.showNext = true
 	feedback := ""
 	if selectedIdx == int(question.CorrectIndex) {
+		question.Status = 1
 		feedback = `<div data-testid="feedback" class="feedback">Correct! 🎉</div>`
 	} else {
+		question.Status = 2
 		feedback = `<div data-testid="feedback" class="feedback">Wrong answer! The correct answer was: ` + getCorrectOption(question) + `</div>`
 	}
 
@@ -119,7 +119,6 @@ func (h *Handlers) HandleNextQuestion(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	h.showNext = false // Reset flag for new question
 	h.renderQuestion(w, question)
 }
 
@@ -133,8 +132,6 @@ func (h *Handlers) renderQuestion(w http.ResponseWriter, question *models.Questi
 	// Add ShowNext flag to template data
 	type TemplateData struct {
 		*models.Question
-		ShowNext bool
-		Correct  bool
 	}
 
 	err = tmpl.ExecuteTemplate(w, "main", &TemplateData{
@@ -161,6 +158,5 @@ func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	h.showNext = false
 	h.renderQuestion(w, question)
 }
-- 
cgit v1.2.3