Better semantics, esp. for register page

This commit is contained in:
Teajey 2025-12-11 14:21:12 +09:00
parent d9dc6caea3
commit 8c4334e063
Signed by: Teajey
GPG Key ID: 970E790FE834A713
15 changed files with 87 additions and 65 deletions

View File

@ -1,6 +1,6 @@
{ {
"Method": "POST", "Method": "POST",
"Form": { "Elements": {
"Username": { "Username": {
"Label": "Username", "Label": "Username",
"Name": "username", "Name": "username",

View File

@ -4,12 +4,12 @@ import (
"encoding/xml" "encoding/xml"
) )
type Form struct { type Form[T any] struct {
Method string Method string
Content any `json:"Form"` Elements T
} }
func (i Form) MarshalXML(e *xml.Encoder, start xml.StartElement) error { func (i Form[T]) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{Local: "Form"} start.Name = xml.Name{Local: "Form"}
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Method"}, Value: i.Method}) start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Method"}, Value: i.Method})
@ -19,7 +19,7 @@ func (i Form) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return err return err
} }
err = e.Encode(i.Content) err = e.Encode(i.Elements)
if err != nil { if err != nil {
return err return err
} }

View File

@ -6,7 +6,6 @@ import (
) )
type Input struct { type Input struct {
// TODO: Perhaps inputs should be allowed to have children.
Label string Label string
Type string `json:",omitempty"` Type string `json:",omitempty"`
Name string Name string

View File

@ -15,9 +15,9 @@ type content struct {
} }
func TestSnapshotForm(t *testing.T) { func TestSnapshotForm(t *testing.T) {
form := htmlx.Form{ form := htmlx.Form[content]{
Method: "POST", Method: "POST",
Content: content{ Elements: content{
Username: htmlx.Input{ Username: htmlx.Input{
Label: "Username", Label: "Username",
Name: "username", Name: "username",

View File

@ -21,7 +21,7 @@ func (i Submit) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
} }
if err := e.EncodeToken(start); err != nil { if err := e.EncodeToken(start); err != nil {
return nil return err
} }
if err := e.EncodeToken(xml.CharData(i.Label)); err != nil { if err := e.EncodeToken(xml.CharData(i.Label)); err != nil {

View File

@ -1,19 +1,26 @@
package props package props
import ( import (
"html/template"
"lishwist/http/props/htmlx" "lishwist/http/props/htmlx"
"lishwist/http/templates" "lishwist/http/templates"
) )
type Register struct { type RegisterForm struct {
Navbar templates.NavCollapse
GeneralError string `json:",omitempty"`
Username htmlx.Input Username htmlx.Input
Password htmlx.Input Password htmlx.Input
ConfirmPassword htmlx.Input ConfirmPassword htmlx.Input
Submit htmlx.Submit
} }
func (p *Register) Validate() (valid bool) { type Register struct {
Nav templates.NavCollapse
Error string `json:",omitempty" xml:",omitempty"`
Warning template.HTML `json:",omitempty" xml:",omitempty"`
Form htmlx.Form[RegisterForm]
}
func (p *RegisterForm) Validate() (valid bool) {
valid = true valid = true
if p.Password.Value != p.ConfirmPassword.Value { if p.Password.Value != p.ConfirmPassword.Value {
@ -38,26 +45,38 @@ func (p *Register) Validate() (valid bool) {
func NewRegister(usernameVal, passwordVal, confirmPassVal string) *Register { func NewRegister(usernameVal, passwordVal, confirmPassVal string) *Register {
return &Register{ return &Register{
Navbar: templates.DefaultNavCollapse(), Nav: templates.DefaultNavCollapse(),
GeneralError: "", Error: "",
Username: htmlx.Input{ Warning: template.HTML(`<p>Your password will be stored in a safe, responsible manner; but don't trust my programming skills!</p><p class="mb-0">Maybe use a password here that you don't use for important things...</p>`),
Name: "username", Form: htmlx.Form[RegisterForm]{
Required: true, Method: "POST",
MinLength: 4, Elements: RegisterForm{
Value: usernameVal, Username: htmlx.Input{
}, Label: "Username",
Password: htmlx.Input{ Name: "username",
Type: "password", Required: true,
Name: "newPassword", MinLength: 4,
Required: true, Value: usernameVal,
MinLength: 5, },
Value: passwordVal, Password: htmlx.Input{
}, Label: "Password",
ConfirmPassword: htmlx.Input{ Type: "password",
Type: "password", Name: "newPassword",
Name: "confirmPassword", Required: true,
Required: true, MinLength: 5,
Value: confirmPassVal, Value: passwordVal,
},
ConfirmPassword: htmlx.Input{
Label: "Confirm password",
Type: "password",
Name: "confirmPassword",
Required: true,
Value: confirmPassVal,
},
Submit: htmlx.Submit{
Label: "Register",
},
},
}, },
} }
} }

View File

@ -30,7 +30,7 @@ func Login(s *response.Session, h http.Header, r *http.Request) rsvp.Response {
login.SuccessfulSetPassword = flashProps.SuccessfulSetPassword 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} 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 { if !valid {
session.FlashSet(&login) session.FlashSet(&login)
log.Printf("Invalid login submission: %#v\n", 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 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" 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) session.FlashSet(&login)
log.Printf("Invalid credentials: %s: %#v\n", err, 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 return resp
default: default:
login.GeneralError = "Something went wrong." login.GeneralError = "Something went wrong."
session.FlashSet(&login) session.FlashSet(&login)
log.Printf("Login error: %s\n", err) 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 return resp
} }
} }

View File

@ -18,11 +18,11 @@ func Register(session *response.Session, h http.Header, r *http.Request) rsvp.Re
flashProps, _ := flash.(*props.Register) flashProps, _ := flash.(*props.Register)
if flashProps != nil { if flashProps != nil {
register.Username.Value = flashProps.Username.Value register.Form.Elements.Username.Value = flashProps.Form.Elements.Username.Value
register.GeneralError = flashProps.GeneralError register.Error = flashProps.Error
register.Username.Error = flashProps.Username.Error register.Form.Elements.Username.Error = flashProps.Form.Elements.Username.Error
register.ConfirmPassword.Error = flashProps.ConfirmPassword.Error register.Form.Elements.ConfirmPassword.Error = flashProps.Form.Elements.ConfirmPassword.Error
} }
return response.Data("register.gotmpl", register) 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) register := props.NewRegister(username, newPassword, confirmPassword)
valid := register.Validate() valid := register.Form.Elements.Validate()
register.Password.Value = "" register.Form.Elements.Password.Value = ""
register.ConfirmPassword.Value = "" register.Form.Elements.ConfirmPassword.Value = ""
if !valid { if !valid {
s.FlashSet(&register) s.FlashSet(&register)
log.Printf("Invalid register submission: %#v\n", register) 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) _, err := lishwist.Register(username, newPassword)
if err != nil { if err != nil {
if errors.Is(err, lishwist.ErrorUsernameTaken) { if errors.Is(err, lishwist.ErrorUsernameTaken) {
register.Username.Error = "Username is taken" register.Form.Elements.Username.Error = "Username is taken"
} else { } else {
register.GeneralError = "Something went wrong." register.Error = "Something went wrong."
} }
s.FlashSet(&register) s.FlashSet(&register)
log.Printf("Registration failed: %s\n", err) log.Printf("Registration failed: %s\n", err)

View File

@ -25,7 +25,6 @@
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script> crossorigin="anonymous"></script>
<script> <script>
function getSubmissionNames(form) { function getSubmissionNames(form) {
return Array.from(new FormData(form).keys()); return Array.from(new FormData(form).keys());

View File

@ -4,7 +4,7 @@
{{template "head"}} {{template "head"}}
</head> </head>
{{with .Content}} {{with .Elements}}
<body> <body>
<div style="height: 100svh;" class="d-flex flex-column"> <div style="height: 100svh;" class="d-flex flex-column">
{{template "navbar"}} {{template "navbar"}}
@ -37,7 +37,7 @@
<div class="mb-3"> <div class="mb-3">
{{template "hyperlink" .RegisterLink}} {{template "hyperlink" .RegisterLink}}
</div> </div>
<input class="btn btn-primary" type="submit" value="{{.Submit.Value}}"> <input class="btn btn-primary" type="submit" value="{{.Submit.Label}}">
</div> </div>
</form> </form>
</div> </div>

View File

@ -6,34 +6,39 @@
<body> <body>
<div style="height: 100svh;" class="d-flex flex-column"> <div style="height: 100svh;" class="d-flex flex-column">
{{template "navbar" .Navbar}} {{template "navbar" .Nav}}
<div class="container d-flex flex-grow-1 justify-content-center align-items-center flex-column"> <div class="container d-flex flex-grow-1 justify-content-center align-items-center flex-column">
{{with .Warning}}
<div class="alert alert-warning" role="alert"> <div class="alert alert-warning" role="alert">
<p>Your password will be stored in a safe, responsible manner; but don't trust my programming skills!</p> {{.}}
<p class="mb-0">Maybe use a password here that you don't use for important things...</p>
</div> </div>
{{with .GeneralError}} {{end}}
{{with .Error}}
<div class="alert alert-danger" role="alert"> <div class="alert alert-danger" role="alert">
<p class="mb-0">{{.}}</p> <p class="mb-0">{{.}}</p>
</div> </div>
{{end}} {{end}}
<form method="post"> {{with .Form}}
<form method="{{.Method}}">
{{with .Elements}}
<div class="d-flex flex-column"> <div class="d-flex flex-column">
<label> <label>
Username {{.Username.Label}}
{{template "input" .Username}} {{template "input" .Username}}
</label> </label>
<label> <label>
Password {{.Password.Label}}
{{template "input" .Password}} {{template "input" .Password}}
</label> </label>
<label> <label>
Confirm password {{.ConfirmPassword.Label}}
{{template "input" .ConfirmPassword}} {{template "input" .ConfirmPassword}}
</label> </label>
<input class="btn btn-primary" type="submit" value="Register"> <input class="btn btn-primary" type="submit" value="{{.Submit.Label}}">
</div> </div>
{{end}}
</form> </form>
{{end}}
</div> </div>
</div> </div>
</body> </body>

View File

@ -20,8 +20,8 @@ type User struct {
} }
type NavCollapse struct { type NavCollapse struct {
User *User User *User `json:",omitempty"`
AccountLink *AccountLink AccountLink *AccountLink `json:",omitempty"`
Links []htmlx.Link Links []htmlx.Link
} }