proper-website-2/src/guestbookform.go

106 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 (
"fmt"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
type guestbookForm struct {
app.Compo
name string
message string
2022-03-13 16:17:27 +01:00
gbModalOpen bool
OnSubmit func(
2022-03-15 16:42:26 +01:00
ctx app.Context,
2022-03-02 13:38:28 +01:00
name string,
message string,
) // Handler to implement which calls the api
}
func (g *guestbookForm) Render() app.UI {
return app.Div().Body(
app.Form().Body(
app.Input().
Type("text").
Name("name").
Placeholder("Name").
Required(true).
2022-03-12 15:52:13 +01:00
OnChange(g.ValueTo(&g.name)).
2022-03-02 13:38:28 +01:00
Value(g.name),
app.Input().
Type("text").
Name("message").
Placeholder("Message").
Required(true).
2022-03-12 15:52:13 +01:00
OnChange(g.ValueTo(&g.message)).
2022-03-02 13:38:28 +01:00
Value(g.message),
app.Input().
Type("submit").
Name("submit"),
).ID("form").
OnSubmit(func(ctx app.Context, e app.Event) {
// This was to prevent the page from reloading
e.PreventDefault()
if g.name == "" || g.message == "" {
fmt.Printf("Error: one or more field(s) are empty. For now unhandled\n")
2022-03-13 18:22:47 +01:00
return
2022-03-02 13:38:28 +01:00
}
2022-03-13 16:17:27 +01:00
if len(g.name) > 40 || len(g.message) > 360 {
fmt.Printf("Error: Your message is too long fucker\n")
g.gbModalOpen = true
return
}
2022-03-15 16:42:26 +01:00
g.OnSubmit(ctx, g.name, g.message)
2022-03-13 18:22:47 +01:00
g.clear()
2022-03-02 13:38:28 +01:00
}),
2022-03-13 16:17:27 +01:00
app.If(
g.gbModalOpen,
&guestbookAlertModal{
OnClose: func() {
g.gbModalOpen = false
g.Update()
},
},
),
2022-03-15 13:22:59 +01:00
)
2022-03-02 13:38:28 +01:00
}
func (g *guestbookForm) clear() {
g.name = ""
g.message = ""
}
2022-03-13 16:17:27 +01:00
2022-03-15 16:42:26 +01:00
/*
2022-03-13 16:17:27 +01:00
type guestbookAlertModal struct {
app.Compo
PreviousAttempts int
OnClose func() // For when we close the modal
}
func (g *guestbookAlertModal) Render() app.UI {
return app.Div().
Class("gb-modal").
ID("gbModal").
OnClick(func(ctx app.Context, e app.Event) {
g.OnClose()
}).
Body(
app.Div().
Class("gb-modal-content").
Body(
app.Span().Class("close").Text("X").
OnClick(func(ctx app.Context, e app.Event) {
//modal := app.Window().GetElementByID("gbModal")
//modal.Set("style", "none")
g.OnClose()
}),
app.P().Text("Your name must be <= 40 and your message must be <= 360 characters"),
),
)
}
2022-03-15 16:42:26 +01:00
*/