proper-website-2/src/guestbookpanel.go

101 lines
2.1 KiB
Go
Raw Normal View History

2022-03-15 12:48:47 +01:00
package main
2022-03-02 13:38:28 +01:00
import (
"encoding/json"
"io"
"net/http"
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
}
2022-03-15 16:42:26 +01:00
func (g *guestbookPanel) OnMount(ctx app.Context) {
g.LoadComments(ctx)
}
func (g *guestbookPanel) OnNav(ctx app.Context) {
g.LoadComments(ctx)
}
func (g *guestbookPanel) OnUpdate(ctx app.Context) {
g.LoadComments(ctx)
2022-03-02 13:38:28 +01:00
}
func (g *guestbookPanel) Render() app.UI {
2022-03-15 16:42:26 +01:00
return app.Div().Body(
app.Range(g.comments).Slice(func(i int) app.UI {
return &guestbookComment{
Comment: g.comments[i],
}
},
),
).OnSubmit(func(ctx app.Context, e app.Event) {
g.LoadComments(ctx)
})
2022-03-02 13:38:28 +01:00
}
2022-03-15 16:42:26 +01:00
func (g *guestbookPanel) LoadComments(ctx app.Context) {
2022-03-02 13:38:28 +01:00
// TODO: maybe you can put this in a localbrowser storage?
2022-03-13 20:18:07 +01:00
url := ApiURL
2022-03-15 16:42:26 +01:00
ctx.Async(func() {
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
}
2022-03-02 13:38:28 +01:00
2022-03-15 16:42:26 +01:00
ctx.Dispatch(func(ctx app.Context) {
err = json.Unmarshal(jsondata, &g.comments)
if err != nil {
app.Log(err)
return
}
})
})
2022-03-02 13:38:28 +01:00
}
2022-03-15 16:42:26 +01:00
/*type guestbookComment struct {
2022-03-02 13:38:28 +01:00
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")
}
2022-03-15 16:42:26 +01:00
*/