proper-website-2/components/homepage.go

61 lines
1.2 KiB
Go
Raw Normal View History

2022-03-01 15:50:53 +01:00
package components
import (
2022-03-02 13:38:28 +01:00
"bytes"
"encoding/json"
"fmt"
"net/http"
"git.home.dutchellie.nl/DutchEllie/proper-website-2/entity"
2022-03-01 15:50:53 +01:00
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
type Homepage struct {
app.Compo
2022-03-01 17:07:33 +01:00
2022-03-02 13:38:28 +01:00
showGuestbook bool
guestbookUpdated bool
2022-03-01 15:50:53 +01:00
}
func NewHomepage() *Homepage {
2022-03-02 13:38:28 +01:00
return &Homepage{showGuestbook: true, guestbookUpdated: false}
2022-03-01 15:50:53 +01:00
}
func (p *Homepage) Render() app.UI {
2022-03-02 13:38:28 +01:00
gbp := newGuestbookPanel()
2022-03-01 15:50:53 +01:00
return app.Div().Body(
&header{},
app.Div().Body(
2022-03-01 17:07:33 +01:00
newNavbar(),
2022-03-02 13:38:28 +01:00
&homePanel{
onShowClick: func() {
p.showGuestbook = !p.showGuestbook
},
},
&guestbookForm{
OnSubmit: func(name, message string) {
var comment entity.Comment
comment.Name = name
comment.Message = message
jsondata, err := json.Marshal(comment)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
url := "/api/comment"
req, err := http.Post(url, "application/json", bytes.NewBuffer(jsondata))
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
if req.StatusCode == 200 {
}
defer req.Body.Close()
},
},
app.If(p.showGuestbook, gbp),
2022-03-01 15:50:53 +01:00
).Class("main"),
)
}