proper-website-2/components/homepage.go

66 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"
)
2022-03-12 15:52:13 +01:00
const (
2022-03-12 16:22:41 +01:00
apiurl = "https://api.nicecock.eu/"
2022-03-12 15:52:13 +01:00
)
2022-03-01 15:50:53 +01:00
type Homepage struct {
app.Compo
2022-03-01 17:07:33 +01:00
2022-03-12 15:52:13 +01:00
showGuestbook bool
2022-03-07 13:05:33 +01:00
page string
2022-03-01 15:50:53 +01:00
}
func NewHomepage() *Homepage {
2022-03-12 15:52:13 +01:00
return &Homepage{showGuestbook: true, page: "home"}
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{},
2022-03-07 13:05:33 +01:00
&navbar{},
&homePanel{
onShowClick: func() {
p.showGuestbook = !p.showGuestbook
2022-03-02 13:38:28 +01:00
},
2022-03-07 13:05:33 +01:00
},
&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
}
2022-03-12 15:52:13 +01:00
url := apiurl + "api/comment"
2022-03-07 13:05:33 +01:00
req, err := http.Post(url, "application/json", bytes.NewBuffer(jsondata))
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
if req.StatusCode == 200 {
p.Update()
}
defer req.Body.Close()
2022-03-02 13:38:28 +01:00
},
2022-03-07 13:05:33 +01:00
},
//app.If(p.showGuestbook, gbp),
gbp.Render(),
).Class("main")
2022-03-01 15:50:53 +01:00
}