use beads service
use pointes for comments use htmx routing
This commit is contained in:
@@ -13,7 +13,7 @@ type CommentForm struct {
|
||||
}
|
||||
|
||||
func ListComments(w http.ResponseWriter, r *http.Request) {
|
||||
comments := r.Context().Value(commentsKey).([]models.Comment)
|
||||
comments := r.Context().Value(commentsKey).([]*models.Comment)
|
||||
hx := HTMX(r)
|
||||
|
||||
if hx.IsHxRequest() {
|
||||
@@ -40,14 +40,14 @@ func CreateComment(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if err := ValidateForm(form); err != nil {
|
||||
if hx.IsHxRequest() {
|
||||
hx.WriteString("<div class=\"alert alert-error\">Please fix the form errors</div>")
|
||||
hx.WriteString(`<div class="alert alert-error">Please fix the form errors</div>`)
|
||||
} else {
|
||||
http.Error(w, "Validation error: "+err.Error(), http.StatusUnprocessableEntity)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
comment, err := svc.Beads.AddComment(r.Context(), issue.ID, form.Author, form.Text)
|
||||
comment, err := svc.Beads.AddIssueComment(r.Context(), issue.ID, form.Author, form.Text)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to create comment: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -55,7 +55,7 @@ func CreateComment(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if hx.IsHxRequest() {
|
||||
components.CommentItem(components.CommentItemProps{
|
||||
Comment: *comment,
|
||||
Comment: comment,
|
||||
}).Render(r.Context(), w)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
svc := Services(r)
|
||||
hx := HTMX(r)
|
||||
|
||||
issues, err := svc.Beads.AllIssues(r.Context())
|
||||
if err != nil {
|
||||
@@ -22,5 +23,10 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
},
|
||||
}
|
||||
|
||||
if hx.IsHxRequest() {
|
||||
routes.IndexContent(props).Render(r.Context(), w)
|
||||
return
|
||||
}
|
||||
|
||||
routes.Index(props).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ func IssueCtx(next http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
comments, err := svc.Beads.GetComments(r.Context(), issue.ID)
|
||||
comments, err := svc.Beads.GetIssueComments(r.Context(), issue.ID)
|
||||
if err != nil {
|
||||
http.Error(w, "Error getting comments: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -107,13 +107,10 @@ func IssueCtx(next http.Handler) http.Handler {
|
||||
|
||||
func GetIssue(w http.ResponseWriter, r *http.Request) {
|
||||
issue := r.Context().Value(issueKey).(*models.Issue)
|
||||
comments := r.Context().Value(commentsKey).([]models.Comment)
|
||||
comments := r.Context().Value(commentsKey).([]*models.Comment)
|
||||
hx := HTMX(r)
|
||||
|
||||
acceptHeader := r.Header.Get("Accept")
|
||||
wantsHTML := acceptHeader == "" || strings.Contains(acceptHeader, "text/html")
|
||||
|
||||
if wantsHTML && !hx.IsHxRequest() {
|
||||
if !hx.IsHxRequest() && strings.Contains(r.Header.Get("Accept"), "text/html") {
|
||||
routes.IssueDetail(routes.IssueDetailProps{
|
||||
Issue: *issue,
|
||||
Comments: comments,
|
||||
@@ -121,6 +118,14 @@ func GetIssue(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if hx.IsHxRequest() {
|
||||
routes.IssueDetailContent(routes.IssueDetailProps{
|
||||
Issue: *issue,
|
||||
Comments: comments,
|
||||
}).Render(r.Context(), w)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
hx.WriteJSON(issue)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user