diff options
author | Grail Finder (aider) <wohilas@gmail.com> | 2025-04-05 16:52:02 +0300 |
---|---|---|
committer | Grail Finder (aider) <wohilas@gmail.com> | 2025-04-05 16:52:02 +0300 |
commit | b5f3dd055ecc25120df65f6655808d8a8897f1a3 (patch) | |
tree | 797efcf2bd68d93272f287ee77ad04bba11b1a64 | |
parent | 3374cb0fb2ce024eb5da78dc63c7313db8ec4c95 (diff) |
feat: extract feedback into template partial and add Correct field
-rw-r--r-- | components/feedback.html | 33 | ||||
-rw-r--r-- | components/index.html | 10 | ||||
-rw-r--r-- | internal/handlers/main.go | 1 |
3 files changed, 37 insertions, 7 deletions
diff --git a/components/feedback.html b/components/feedback.html new file mode 100644 index 0000000..0eda969 --- /dev/null +++ b/components/feedback.html @@ -0,0 +1,33 @@ +{{define "feedback"}} +{{if .Correct}} +<div class="correct-feedback"> + <div class="correct-answer">Correct!</div> + <div class="explanation">{{.Explanation}}</div> + {{if .ShowNext}} + <button + hx-get="/next-question?current_id={{.ID}}" + hx-target="#ancestor" + hx-swap="outerHTML" + class="next-button" + data-testid="next-button"> + Next Question → + </button> + {{end}} +</div> +{{else}} +<div class="error-feedback"> + <div class="wrong-answer">Try Again</div> + <div class="explanation">{{.Explanation}}</div> + {{if .ShowNext}} + <button + hx-get="/next-question?current_id={{.ID}}" + hx-target="#ancestor" + hx-swap="outerHTML" + class="next-button" + data-testid="next-button"> + Next Question → + </button> + {{end}} +</div> +{{end}} +{{end}} diff --git a/components/index.html b/components/index.html index f0f18f0..9c43a50 100644 --- a/components/index.html +++ b/components/index.html @@ -13,13 +13,8 @@ <div class="question-text" data-testid="question"> {{.Text}} </div> - <div id="feedback" data-testid="feedback" class="{{if .Correct}}correct-feedback{{else}}error-feedback{{end}}"> - {{if .Correct}} - <div class="correct-answer">Correct!</div> - {{else}} - <div class="wrong-answer">Try Again</div> - {{end}} - <div class="explanation">{{.Explanation}}</div> + <div id="feedback" data-testid="feedback"> + {{template "feedback" .}} {{if .ShowNext}} <button hx-get="/next-question?current_id={{.ID}}" @@ -31,6 +26,7 @@ </button> {{end}} </div> + {{template "feedback" .}} <div id="options"> <button data-testid="option1" diff --git a/internal/handlers/main.go b/internal/handlers/main.go index abd2226..4a64380 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -134,6 +134,7 @@ func (h *Handlers) renderQuestion(w http.ResponseWriter, question *models.Questi type TemplateData struct { *models.Question ShowNext bool + Correct bool } err = tmpl.ExecuteTemplate(w, "main", &TemplateData{ |