proper-website-2/src/homepage.go

78 lines
1.6 KiB
Go
Raw Normal View History

2022-03-15 12:48:47 +01:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"dutchellie.nl/DutchEllie/proper-website-2/entity"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
2022-05-14 20:45:28 +02:00
var (
ApiURL string
)
2022-03-15 12:48:47 +01:00
type Homepage struct {
app.Compo
}
func NewHomepage() *Homepage {
return &Homepage{}
}
func (p *Homepage) Render() app.UI {
return newPage().
Title("Homepage").
LeftBar(
2022-03-15 13:22:59 +01:00
newHTMLBlock().
Class("left").
Class("leftbarblock").
Src("/web/blocks/snippets/bannerpanel.html"),
2022-03-15 12:48:47 +01:00
).
Main(
newHTMLBlock().
Class("right").
2022-03-15 13:22:59 +01:00
Class("contentblock").
2022-03-15 13:06:52 +01:00
Src("/web/blocks/pages/intro.html"),
2022-03-15 13:22:59 +01:00
newUIBlock().
Class("right").
Class("contentblock").
UI(
2022-03-15 16:42:26 +01:00
&guestbook{
2022-06-25 11:28:37 +02:00
OnSubmit: func(ctx app.Context, name, email, website, message, uuid string) {
2022-03-15 13:22:59 +01:00
var comment entity.Comment
comment.Name = name
2022-03-15 16:42:26 +01:00
comment.Email = email
comment.Website = website
2022-03-15 13:22:59 +01:00
comment.Message = message
2022-06-25 11:28:37 +02:00
comment.UUID = uuid
2022-03-15 13:22:59 +01:00
jsondata, err := json.Marshal(comment)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
2022-03-24 13:25:07 +01:00
url := ApiURL + "/comment"
2022-03-15 13:22:59 +01:00
2022-03-27 14:06:21 +02:00
// This is not Async'ed, because otherwise you run into a race
// condition where you reload the comments before the server had time
// to process the request!
{
2022-03-15 16:42:26 +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-27 14:06:21 +02:00
}
2022-03-15 13:22:59 +01:00
},
},
),
2022-03-15 12:48:47 +01:00
)
}