regester form

main
DustyP 9 months ago
parent bbec7ec509
commit 7834ceb883

@ -117,6 +117,7 @@ func (s *Server) routes() {
s.router.Get("/admin", s.handleAdmin()) s.router.Get("/admin", s.handleAdmin())
s.router.Get("/register", s.handleRegister()) s.router.Get("/register", s.handleRegister())
s.router.Get("/register/form", s.handleRegisterForm())
s.router.Route("/api/groups", func(r chi.Router) { s.router.Route("/api/groups", func(r chi.Router) {
r.Post("/", s.handleCreateGroup()) r.Post("/", s.handleCreateGroup())
@ -433,6 +434,27 @@ func (s *Server) handleRegister() http.HandlerFunc {
} }
} }
// handleRegisterForm returns just the registration form component
func (s *Server) handleRegisterForm() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Get groups for the form
groups, err := s.db.GetGroups()
if err != nil {
s.logger.Error("Failed to get groups", "error", err)
http.Error(w, "Failed to get groups", http.StatusInternalServerError)
return
}
// Render just the registration form component
component := templates.RegisterForm(groups)
if err := component.Render(r.Context(), w); err != nil {
s.logger.Error("Failed to render registration form", "error", err)
http.Error(w, "Failed to render registration form", http.StatusInternalServerError)
return
}
}
}
// API handlers for groups // API handlers for groups
// handleCreateGroup creates a new group // handleCreateGroup creates a new group

@ -33,7 +33,7 @@ templ Admin(groups []models.Group, racers []models.Racer) {
<form <form
hx-post="/api/groups" hx-post="/api/groups"
hx-target="#groups-list" hx-target="#groups-list"
hx-swap="outerHTML" hx-swap="innerHTML"
hx-on::after-request="this.reset(); document.getElementById('group-form-container').style.display = 'none';" hx-on::after-request="this.reset(); document.getElementById('group-form-container').style.display = 'none';"
> >
<div class="mb-3"> <div class="mb-3">
@ -99,7 +99,7 @@ templ Admin(groups []models.Group, racers []models.Racer) {
<h2 class="h5 mb-0">Racers</h2> <h2 class="h5 mb-0">Racers</h2>
<button <button
class="btn btn-sm btn-primary" class="btn btn-sm btn-primary"
hx-get="/register" hx-get="/register/form"
hx-target="#racer-form-container" hx-target="#racer-form-container"
> >
Add Racer Add Racer

@ -46,7 +46,7 @@ func Admin(groups []models.Group, racers []models.Racer) templ.Component {
}() }()
} }
ctx = templ.InitializeContext(ctx) ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div class=\"container py-4\"><h1 class=\"mb-4\">Admin Dashboard</h1><div class=\"row\"><div class=\"col-md-6 mb-4\"><div class=\"card\"><div class=\"card-header d-flex justify-content-between align-items-center\"><h2 class=\"h5 mb-0\">Groups</h2><button class=\"btn btn-sm btn-primary\" id=\"show-group-form-btn\" onclick=\"toggleGroupForm()\">Add Group</button></div><div class=\"card-body\"><div id=\"group-form-container\" class=\"mb-4\" style=\"display: none;\"><div class=\"card\"><div class=\"card-header\"><h3 class=\"h6 mb-0\">Add New Group</h3></div><div class=\"card-body\"><form hx-post=\"/api/groups\" hx-target=\"#groups-list\" hx-swap=\"outerHTML\" hx-on::after-request=\"this.reset(); document.getElementById(&#39;group-form-container&#39;).style.display = &#39;none&#39;;\"><div class=\"mb-3\"><label for=\"group-name\" class=\"form-label\">Name</label> <input type=\"text\" class=\"form-control\" id=\"group-name\" name=\"name\" required></div><div class=\"mb-3\"><label for=\"group-description\" class=\"form-label\">Description</label> <textarea class=\"form-control\" id=\"group-description\" name=\"description\" rows=\"2\"></textarea></div><div class=\"d-flex justify-content-end\"><button type=\"button\" class=\"btn btn-secondary me-2\" onclick=\"toggleGroupForm()\">Cancel</button> <button type=\"submit\" class=\"btn btn-primary\">Save Group</button></div></form></div></div></div><div id=\"groups-list\">") templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div class=\"container py-4\"><h1 class=\"mb-4\">Admin Dashboard</h1><div class=\"row\"><div class=\"col-md-6 mb-4\"><div class=\"card\"><div class=\"card-header d-flex justify-content-between align-items-center\"><h2 class=\"h5 mb-0\">Groups</h2><button class=\"btn btn-sm btn-primary\" id=\"show-group-form-btn\" onclick=\"toggleGroupForm()\">Add Group</button></div><div class=\"card-body\"><div id=\"group-form-container\" class=\"mb-4\" style=\"display: none;\"><div class=\"card\"><div class=\"card-header\"><h3 class=\"h6 mb-0\">Add New Group</h3></div><div class=\"card-body\"><form hx-post=\"/api/groups\" hx-target=\"#groups-list\" hx-swap=\"innerHTML\" hx-on::after-request=\"this.reset(); document.getElementById(&#39;group-form-container&#39;).style.display = &#39;none&#39;;\"><div class=\"mb-3\"><label for=\"group-name\" class=\"form-label\">Name</label> <input type=\"text\" class=\"form-control\" id=\"group-name\" name=\"name\" required></div><div class=\"mb-3\"><label for=\"group-description\" class=\"form-label\">Description</label> <textarea class=\"form-control\" id=\"group-description\" name=\"description\" rows=\"2\"></textarea></div><div class=\"d-flex justify-content-end\"><button type=\"button\" class=\"btn btn-secondary me-2\" onclick=\"toggleGroupForm()\">Cancel</button> <button type=\"submit\" class=\"btn btn-primary\">Save Group</button></div></form></div></div></div><div id=\"groups-list\">")
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
@ -133,7 +133,7 @@ func Admin(groups []models.Group, racers []models.Racer) templ.Component {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
} }
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "</div></div></div></div><div class=\"col-md-6 mb-4\"><div class=\"card\"><div class=\"card-header d-flex justify-content-between align-items-center\"><h2 class=\"h5 mb-0\">Racers</h2><button class=\"btn btn-sm btn-primary\" hx-get=\"/register\" hx-target=\"#racer-form-container\">Add Racer</button></div><div class=\"card-body\"><div id=\"racer-form-container\" class=\"mb-4\"></div><div id=\"racers-list\">") templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "</div></div></div></div><div class=\"col-md-6 mb-4\"><div class=\"card\"><div class=\"card-header d-flex justify-content-between align-items-center\"><h2 class=\"h5 mb-0\">Racers</h2><button class=\"btn btn-sm btn-primary\" hx-get=\"/register/form\" hx-target=\"#racer-form-container\">Add Racer</button></div><div class=\"card-body\"><div id=\"racer-form-container\" class=\"mb-4\"></div><div id=\"racers-list\">")
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }

@ -2,6 +2,7 @@ package templates
import ( import (
"fmt" "fmt"
"strconv"
"track-gopher/models" "track-gopher/models"
) )
@ -125,4 +126,51 @@ templ Register(groups []models.Group) {
</script> </script>
</body> </body>
</html> </html>
}
templ RegisterForm(groups []models.Group) {
<div class="card">
<div class="card-header">
<h3 class="h6 mb-0">Add New Racer</h3>
</div>
<div class="card-body">
<form
hx-post="/api/racers"
hx-target="#racers-list"
hx-swap="innerHTML"
hx-on::after-request="this.reset(); document.getElementById('racer-form-container').style.display = 'none';"
>
<!-- Form fields for racer registration -->
<div class="mb-3">
<label for="first-name" class="form-label">First Name</label>
<input type="text" class="form-control" id="first-name" name="first_name" required/>
</div>
<div class="mb-3">
<label for="last-name" class="form-label">Last Name</label>
<input type="text" class="form-control" id="last-name" name="last_name" required/>
</div>
<div class="mb-3">
<label for="car-number" class="form-label">Car Number</label>
<input type="text" class="form-control" id="car-number" name="car_number" required/>
</div>
<div class="mb-3">
<label for="car-weight" class="form-label">Car Weight (oz)</label>
<input type="number" step="0.01" class="form-control" id="car-weight" name="car_weight" required/>
</div>
<div class="mb-3">
<label for="group-id" class="form-label">Group</label>
<select class="form-select" id="group-id" name="group_id" required>
<option value="">Select a group</option>
for _, group := range groups {
<option value={ strconv.FormatInt(group.ID, 10) }>{ group.Name }</option>
}
</select>
</div>
<div class="d-flex justify-content-end">
<button type="button" class="btn btn-secondary me-2" onclick="document.getElementById('racer-form-container').style.display = 'none';">Cancel</button>
<button type="submit" class="btn btn-primary">Save Racer</button>
</div>
</form>
</div>
</div>
} }

@ -10,6 +10,7 @@ import templruntime "github.com/a-h/templ/runtime"
import ( import (
"fmt" "fmt"
"strconv"
"track-gopher/models" "track-gopher/models"
) )
@ -46,7 +47,7 @@ func Register(groups []models.Group) templ.Component {
var templ_7745c5c3_Var2 string var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprint(group.ID)) templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprint(group.ID))
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates/register.templ`, Line: 64, Col: 47} return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates/register.templ`, Line: 65, Col: 47}
} }
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
@ -59,7 +60,7 @@ func Register(groups []models.Group) templ.Component {
var templ_7745c5c3_Var3 string var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(group.Name) templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(group.Name)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates/register.templ`, Line: 64, Col: 62} return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates/register.templ`, Line: 65, Col: 62}
} }
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
@ -78,4 +79,69 @@ func Register(groups []models.Group) templ.Component {
}) })
} }
func RegisterForm(groups []models.Group) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var4 := templ.GetChildren(ctx)
if templ_7745c5c3_Var4 == nil {
templ_7745c5c3_Var4 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<div class=\"card\"><div class=\"card-header\"><h3 class=\"h6 mb-0\">Add New Racer</h3></div><div class=\"card-body\"><form hx-post=\"/api/racers\" hx-target=\"#racers-list\" hx-swap=\"innerHTML\" hx-on::after-request=\"this.reset(); document.getElementById(&#39;racer-form-container&#39;).style.display = &#39;none&#39;;\"><!-- Form fields for racer registration --><div class=\"mb-3\"><label for=\"first-name\" class=\"form-label\">First Name</label> <input type=\"text\" class=\"form-control\" id=\"first-name\" name=\"first_name\" required></div><div class=\"mb-3\"><label for=\"last-name\" class=\"form-label\">Last Name</label> <input type=\"text\" class=\"form-control\" id=\"last-name\" name=\"last_name\" required></div><div class=\"mb-3\"><label for=\"car-number\" class=\"form-label\">Car Number</label> <input type=\"text\" class=\"form-control\" id=\"car-number\" name=\"car_number\" required></div><div class=\"mb-3\"><label for=\"car-weight\" class=\"form-label\">Car Weight (oz)</label> <input type=\"number\" step=\"0.01\" class=\"form-control\" id=\"car-weight\" name=\"car_weight\" required></div><div class=\"mb-3\"><label for=\"group-id\" class=\"form-label\">Group</label> <select class=\"form-select\" id=\"group-id\" name=\"group_id\" required><option value=\"\">Select a group</option> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, group := range groups {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "<option value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.FormatInt(group.ID, 10))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates/register.templ`, Line: 165, Col: 54}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 string
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(group.Name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates/register.templ`, Line: 165, Col: 69}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "</option>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "</select></div><div class=\"d-flex justify-content-end\"><button type=\"button\" class=\"btn btn-secondary me-2\" onclick=\"document.getElementById(&#39;racer-form-container&#39;).style.display = &#39;none&#39;;\">Cancel</button> <button type=\"submit\" class=\"btn btn-primary\">Save Racer</button></div></form></div></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate var _ = templruntime.GeneratedTemplate

Loading…
Cancel
Save