From 8c4334e0631695a9f536f87c390948a84086245f Mon Sep 17 00:00:00 2001
From: Teajey <21069848+Teajey@users.noreply.github.com>
Date: Thu, 11 Dec 2025 14:21:12 +0900
Subject: [PATCH] Better semantics, esp. for register page
---
http/props/htmlx/TestSnapshotForm.snap.json | 4 +-
http/props/htmlx/TestSnapshotForm.snap.xml | 2 +-
http/props/htmlx/TestSnapshotLink.snap.json | 2 +-
http/props/htmlx/TestSnapshotLink.snap.xml | 2 +-
http/props/htmlx/form.go | 10 +--
http/props/htmlx/input.go | 1 -
http/props/htmlx/snapshot_test.go | 4 +-
http/props/htmlx/submit.go | 2 +-
http/props/register.go | 67 +++++++++++++--------
http/routing/login.go | 8 +--
http/routing/register.go | 18 +++---
http/templates/base.gotmpl | 1 -
http/templates/login.gotmpl | 4 +-
http/templates/register.gotmpl | 23 ++++---
http/templates/templates.go | 4 +-
15 files changed, 87 insertions(+), 65 deletions(-)
diff --git a/http/props/htmlx/TestSnapshotForm.snap.json b/http/props/htmlx/TestSnapshotForm.snap.json
index f0d0e69..cf3431d 100644
--- a/http/props/htmlx/TestSnapshotForm.snap.json
+++ b/http/props/htmlx/TestSnapshotForm.snap.json
@@ -1,6 +1,6 @@
{
"Method": "POST",
- "Form": {
+ "Elements": {
"Username": {
"Label": "Username",
"Name": "username",
@@ -22,4 +22,4 @@
"Label": "Login"
}
}
-}
\ No newline at end of file
+}
diff --git a/http/props/htmlx/TestSnapshotForm.snap.xml b/http/props/htmlx/TestSnapshotForm.snap.xml
index dce450e..fbe94cf 100644
--- a/http/props/htmlx/TestSnapshotForm.snap.xml
+++ b/http/props/htmlx/TestSnapshotForm.snap.xml
@@ -5,4 +5,4 @@
Register
Your password will be stored in a safe, responsible manner; but don't trust my programming skills!
Maybe use a password here that you don't use for important things...
`), + Form: htmlx.Form[RegisterForm]{ + Method: "POST", + Elements: RegisterForm{ + Username: htmlx.Input{ + Label: "Username", + Name: "username", + Required: true, + MinLength: 4, + Value: usernameVal, + }, + Password: htmlx.Input{ + Label: "Password", + Type: "password", + Name: "newPassword", + Required: true, + MinLength: 5, + Value: passwordVal, + }, + ConfirmPassword: htmlx.Input{ + Label: "Confirm password", + Type: "password", + Name: "confirmPassword", + Required: true, + Value: confirmPassVal, + }, + Submit: htmlx.Submit{ + Label: "Register", + }, + }, }, } } diff --git a/http/routing/login.go b/http/routing/login.go index b938608..9c3f761 100644 --- a/http/routing/login.go +++ b/http/routing/login.go @@ -30,7 +30,7 @@ func Login(s *response.Session, h http.Header, r *http.Request) rsvp.Response { login.SuccessfulSetPassword = flashProps.SuccessfulSetPassword } - body := htmlx.Form{Method: "POST", Content: login} + body := htmlx.Form[*props.Login]{Method: "POST", Elements: login} return rsvp.Response{TemplateName: "login.gotmpl", Body: body} } @@ -55,7 +55,7 @@ func LoginPost(session *response.Session, h http.Header, r *http.Request) rsvp.R if !valid { session.FlashSet(&login) log.Printf("Invalid login submission: %#v\n", login) - resp.Body = htmlx.Form{Method: "POST", Content: login} + resp.Body = htmlx.Form[*props.Login]{Method: "POST", Elements: login} return resp } @@ -67,13 +67,13 @@ func LoginPost(session *response.Session, h http.Header, r *http.Request) rsvp.R login.GeneralError = "Username or password invalid. If you're having trouble accessing your account, you may want to consider asking the System Admin (Thomas) to reset your password" session.FlashSet(&login) log.Printf("Invalid credentials: %s: %#v\n", err, login) - resp.Body = htmlx.Form{Method: "POST", Content: login} + resp.Body = htmlx.Form[*props.Login]{Method: "POST", Elements: login} return resp default: login.GeneralError = "Something went wrong." session.FlashSet(&login) log.Printf("Login error: %s\n", err) - resp.Body = htmlx.Form{Method: "POST", Content: login} + resp.Body = htmlx.Form[*props.Login]{Method: "POST", Elements: login} return resp } } diff --git a/http/routing/register.go b/http/routing/register.go index 4b85f49..2fef3be 100644 --- a/http/routing/register.go +++ b/http/routing/register.go @@ -18,11 +18,11 @@ func Register(session *response.Session, h http.Header, r *http.Request) rsvp.Re flashProps, _ := flash.(*props.Register) if flashProps != nil { - register.Username.Value = flashProps.Username.Value + register.Form.Elements.Username.Value = flashProps.Form.Elements.Username.Value - register.GeneralError = flashProps.GeneralError - register.Username.Error = flashProps.Username.Error - register.ConfirmPassword.Error = flashProps.ConfirmPassword.Error + register.Error = flashProps.Error + register.Form.Elements.Username.Error = flashProps.Form.Elements.Username.Error + register.Form.Elements.ConfirmPassword.Error = flashProps.Form.Elements.ConfirmPassword.Error } return response.Data("register.gotmpl", register) @@ -46,9 +46,9 @@ func RegisterPost(s *response.Session, h http.Header, r *http.Request) rsvp.Resp register := props.NewRegister(username, newPassword, confirmPassword) - valid := register.Validate() - register.Password.Value = "" - register.ConfirmPassword.Value = "" + valid := register.Form.Elements.Validate() + register.Form.Elements.Password.Value = "" + register.Form.Elements.ConfirmPassword.Value = "" if !valid { s.FlashSet(®ister) log.Printf("Invalid register submission: %#v\n", register) @@ -58,9 +58,9 @@ func RegisterPost(s *response.Session, h http.Header, r *http.Request) rsvp.Resp _, err := lishwist.Register(username, newPassword) if err != nil { if errors.Is(err, lishwist.ErrorUsernameTaken) { - register.Username.Error = "Username is taken" + register.Form.Elements.Username.Error = "Username is taken" } else { - register.GeneralError = "Something went wrong." + register.Error = "Something went wrong." } s.FlashSet(®ister) log.Printf("Registration failed: %s\n", err) diff --git a/http/templates/base.gotmpl b/http/templates/base.gotmpl index 65f074e..0a21e28 100644 --- a/http/templates/base.gotmpl +++ b/http/templates/base.gotmpl @@ -25,7 +25,6 @@ -