proper-website-2/components/guestbookpanel.go

88 lines
1.8 KiB
Go
Raw Normal View History

2022-03-02 13:38:28 +01:00
package components
import (
"encoding/json"
"io"
"net/http"
2022-03-12 15:52:13 +01:00
"time"
2022-03-02 13:38:28 +01:00
2022-03-13 16:35:31 +01:00
"dutchellie.nl/DutchEllie/proper-website-2/entity"
2022-03-02 13:38:28 +01:00
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
/*
What is this supposed to do:
- It should call on the API to give it a certain number of comments, in the range x to y (this has to be implemented in the api)
- When it has called that, it should display those
- Dynamic links are there to navigate the user along the pages
- Comments are shown dynamically
- This panel can be shown or hidden (maybe)
AND VERY IMPORTANT!
- If a user submits a new comment, automatically put it on the page, no reloading
*/
type guestbookPanel struct {
app.Compo
2022-03-12 15:52:13 +01:00
comments []entity.Comment
2022-03-02 13:38:28 +01:00
}
func newGuestbookPanel() *guestbookPanel {
2022-03-12 15:52:13 +01:00
g := &guestbookPanel{}
g.LoadComments()
return g
2022-03-02 13:38:28 +01:00
}
func (g *guestbookPanel) Render() app.UI {
return app.Div().Body(
app.Range(g.comments).Slice(func(i int) app.UI {
return &guestbookComment{
Comment: g.comments[i],
}
}),
2022-03-12 15:52:13 +01:00
).Class("content gbp")
2022-03-02 13:38:28 +01:00
}
func (g *guestbookPanel) LoadComments() {
// TODO: maybe you can put this in a localbrowser storage?
2022-03-13 20:18:07 +01:00
url := ApiURL
2022-03-02 13:38:28 +01:00
res, err := http.Get(url)
if err != nil {
app.Log(err)
return
}
defer res.Body.Close()
jsondata, err := io.ReadAll(res.Body)
if err != nil {
app.Log(err)
return
}
err = json.Unmarshal(jsondata, &g.comments)
if err != nil {
app.Log(err)
return
}
}
type guestbookComment struct {
app.Compo
Comment entity.Comment
2022-03-12 15:52:13 +01:00
time string
2022-03-02 13:38:28 +01:00
}
func (c *guestbookComment) Render() app.UI {
2022-03-12 15:52:13 +01:00
c.time = c.Comment.PostDate.Format(time.RFC1123)
2022-03-02 13:38:28 +01:00
return app.Div().Body(
2022-03-12 15:52:13 +01:00
app.Div().Class().Body(
app.P().Text(c.Comment.Name).Class("name"),
app.P().Text(c.time).Class("date"),
).Class("comment-header"),
app.Div().Class("comment-message").Body(
app.P().Text(c.Comment.Message),
),
2022-03-02 13:38:28 +01:00
).Class("comment")
}