Skip to content

Commit

Permalink
user RenderHTML helper func in web package
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoolhether committed Mar 18, 2024
1 parent bc9ce41 commit 17e71bb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
27 changes: 14 additions & 13 deletions app/hypermedia/handlers/contactsgrp/contactsgrp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (h *Handlers) RootRedirect(ctx context.Context, w http.ResponseWriter, r *h

func (h *Handlers) CreateForm(ctx context.Context, w http.ResponseWriter, r *http.Request) error {

return fe.NewForm(fe.NewContact{}).Render(ctx, w)
return web.RenderHTML(ctx, w, fe.NewForm(fe.NewContact{}), http.StatusOK)
}

func (h *Handlers) Create(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
Expand All @@ -66,13 +66,14 @@ func (h *Handlers) Create(ctx context.Context, w http.ResponseWriter, r *http.Re
Email: fieldErrs.Fields()["email"],
}

return fe.NewForm(newContact).Render(ctx, w)
return web.RenderHTML(ctx, w, fe.NewForm(newContact), http.StatusOK)
}

err := h.core.Create(ctx, newContact.ToDB())
if err != nil {
newContact.InternalErrors = err.Error()
return fe.NewForm(newContact).Render(ctx, w)

return web.RenderHTML(ctx, w, fe.NewForm(newContact), http.StatusInternalServerError)
}

if err := h.sessions.AddFlash(w, r, "Created contact!"); err != nil {
Expand Down Expand Up @@ -109,11 +110,11 @@ func (h *Handlers) Query(ctx context.Context, w http.ResponseWriter, r *http.Req
// we know we only need to update a very specific
// part of the page, so we just return the rows.
if r.Header.Get("HX-Trigger") == "search" {
return fe.Rows(contactsToView(contacts)).Render(ctx, w)
return web.RenderHTML(ctx, w, fe.Rows(contactsToView(contacts)), http.StatusOK)
}

flashCtx := h.sessions.GetFlashCtx(w, r)
return fe.Index(query, page, contactsToView(contacts), h.core.ArchivePoll(ctx)).Render(flashCtx, w)
return web.RenderHTML(flashCtx, w, fe.Index(query, page, contactsToView(contacts), h.core.ArchivePoll(ctx)), http.StatusOK)
}

func (h *Handlers) QueryByID(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
Expand All @@ -129,7 +130,7 @@ func (h *Handlers) QueryByID(ctx context.Context, w http.ResponseWriter, r *http
return err
}

return fe.ShowByID(contact).Render(ctx, w)
return web.RenderHTML(ctx, w, fe.ShowByID(contact), http.StatusOK)
}

func (h *Handlers) ValidateEmail(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
Expand Down Expand Up @@ -176,7 +177,7 @@ func (h *Handlers) UpdateForm(ctx context.Context, w http.ResponseWriter, r *htt
//InternalErrors: "",
}

return fe.EditByID(c).Render(ctx, w)
return web.RenderHTML(ctx, w, fe.EditByID(c), http.StatusOK)
}

func (h *Handlers) Update(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
Expand Down Expand Up @@ -211,14 +212,14 @@ func (h *Handlers) Update(ctx context.Context, w http.ResponseWriter, r *http.Re
Email: fieldErrs.Fields()["email"],
}

return fe.EditByID(uc).Render(ctx, w)
return web.RenderHTML(ctx, w, fe.EditByID(uc), http.StatusBadRequest)
}

err = h.core.Update(ctx, uc.ToDB())
if err != nil {
// Or do failure flash here?
uc.InternalErrors = err.Error()
return fe.EditByID(uc).Render(ctx, w)
return web.RenderHTML(ctx, w, fe.EditByID(uc), http.StatusInternalServerError)
}

if err := h.sessions.AddFlash(w, r, "Updated contact!"); err != nil {
Expand Down Expand Up @@ -280,7 +281,7 @@ func (h *Handlers) DeleteBatch(ctx context.Context, w http.ResponseWriter, r *ht
}

flashCtx := h.sessions.GetFlashCtx(w, r)
return fe.Index("", 1, contactsToView(contacts), h.core.ArchivePoll(ctx)).Render(flashCtx, w)
return web.RenderHTML(flashCtx, w, fe.Index("", 1, contactsToView(contacts), h.core.ArchivePoll(ctx)), http.StatusOK)
}

func (h *Handlers) Count(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
Expand All @@ -300,18 +301,18 @@ func (h *Handlers) Archive(ctx context.Context, w http.ResponseWriter, r *http.R
return err
}

return fe.Archive(h.core.ArchivePoll(ctx)).Render(ctx, w)
return web.RenderHTML(ctx, w, fe.Archive(h.core.ArchivePoll(ctx)), http.StatusOK)
}

func (h *Handlers) ArchiveRm(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
h.core.ArchiveRm(ctx)

return fe.Archive(h.core.ArchivePoll(ctx)).Render(ctx, w)
return web.RenderHTML(ctx, w, fe.Archive(h.core.ArchivePoll(ctx)), http.StatusNoContent)
}

func (h *Handlers) ArchivePoll(ctx context.Context, w http.ResponseWriter, r *http.Request) error {

return fe.Archive(h.core.ArchivePoll(ctx)).Render(ctx, w)
return web.RenderHTML(ctx, w, fe.Archive(h.core.ArchivePoll(ctx)), http.StatusOK)
}

func (h *Handlers) ArchiveDL(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
Expand Down
Binary file added foundation/.DS_Store
Binary file not shown.
42 changes: 25 additions & 17 deletions foundation/web/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,54 @@ import (
"encoding/xml"
"net/http"

"github.com/a-h/templ"
"github.com/go-json-experiment/json"
)

// RespondJSON converts a Go value to JSON and sends it to the client.
func RespondJSON(ctx context.Context, w http.ResponseWriter, data any, statusCode int) error {
func RenderHTML(ctx context.Context, w http.ResponseWriter, component templ.Component, statusCode int) error {
setStatusCode(ctx, statusCode)
w.WriteHeader(statusCode)

if statusCode == http.StatusNoContent {
w.WriteHeader(statusCode)
return nil
}
return component.Render(ctx, w)
}

jsonData, err := json.Marshal(data)
func RenderXML(ctx context.Context, w http.ResponseWriter, data any, statusCode int) error {
setStatusCode(ctx, statusCode)

w.Header().Set("Content-Type", "application/vnd.hyperview+xml")

bytes, err := xml.MarshalIndent(data, "", " ")
//bytes, err := xml.Marshal(data)
if err != nil {
return err
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)

if _, err := w.Write(jsonData); err != nil {
_, err = w.Write(bytes)
if err != nil {
return err
}

return nil
}

func RenderXML(ctx context.Context, w http.ResponseWriter, data any, statusCode int) error {
// RespondJSON converts a Go value to JSON and sends it to the client.
func RespondJSON(ctx context.Context, w http.ResponseWriter, data any, statusCode int) error {
setStatusCode(ctx, statusCode)

w.Header().Set("Content-Type", "application/vnd.hyperview+xml")
if statusCode == http.StatusNoContent {
w.WriteHeader(statusCode)
return nil
}

bytes, err := xml.MarshalIndent(data, "", " ")
//bytes, err := xml.Marshal(data)
jsonData, err := json.Marshal(data)
if err != nil {
return err
}

_, err = w.Write(bytes)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)

if _, err := w.Write(jsonData); err != nil {
return err
}

Expand Down

0 comments on commit 17e71bb

Please sign in to comment.