Support form state.

This commit is contained in:
mikestefanello 2025-04-08 14:22:44 -04:00
parent ce9b58bf4a
commit 85271607d0
2 changed files with 26 additions and 11 deletions

View file

@ -104,7 +104,7 @@ func (h *Admin) EntityList(n *gen.Type) echo.HandlerFunc {
func (h *Admin) EntityAdd(n *gen.Type) echo.HandlerFunc {
return func(ctx echo.Context) error {
return pages.AdminEntityAdd(ctx, h.getEntitySchema(n))
return pages.AdminEntityForm(ctx, h.getEntitySchema(n), nil)
}
}
@ -113,7 +113,6 @@ func (h *Admin) EntityAddSubmit(n *gen.Type) echo.HandlerFunc {
err := h.admin.Create(ctx, n.Name)
if err != nil {
msg.Danger(ctx, err.Error())
// TODO : hold state
return h.EntityAdd(n)(ctx)
}

View file

@ -3,6 +3,7 @@ package pages
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
"unicode"
@ -21,10 +22,6 @@ import (
. "maragu.dev/gomponents/html"
)
func Entity(ctx echo.Context) error {
return ui.NewRequest(ctx).Render(layouts.Admin, Div(Text("abc")))
}
func AdminEntityDelete(ctx echo.Context, entityTypeName string) error {
r := ui.NewRequest(ctx)
form := Form(
@ -40,10 +37,9 @@ func AdminEntityDelete(ctx echo.Context, entityTypeName string) error {
return r.Render(layouts.Admin, form)
}
func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
func AdminEntityForm(ctx echo.Context, schema *load.Schema, values url.Values) error {
r := ui.NewRequest(ctx)
r.Title = fmt.Sprintf("Add %s", schema.Name) // TODO
nodes := make(Group, 0)
label := func(name string) string {
@ -55,13 +51,27 @@ func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
return string(text)
}
getValue := func(name string) string {
if value := ctx.FormValue(name); value != "" {
return value
}
if values != nil && len(values[name]) > 0 {
return values[name][0] // TODO cardinality
}
return ""
}
for _, f := range schema.Fields {
// TODO cardinality?
switch f.Info.Type {
case field.TypeString:
nodes = append(nodes, InputField(InputFieldParams{
Name: f.Name,
InputType: "text",
Label: label(f.Name),
Value: getValue(f.Name),
}))
case field.TypeTime:
nodes = append(nodes, InputField(InputFieldParams{
@ -69,10 +79,15 @@ func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
InputType: "text",
Label: label(f.Name),
Help: fmt.Sprintf("Use the following format: %s", time.Now().Format(time.RFC3339)),
Value: time.Now().Format(time.RFC3339),
Value: getValue(f.Name),
}))
case field.TypeBool:
// TODO
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
case field.TypeEnum:
// TODO
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
// case numeric TODO
default:
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
}
@ -86,6 +101,7 @@ func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
Name: e.Name,
InputType: "number",
Label: label(e.Name),
Value: getValue(e.Name), // TODO load does not load this
}))
}
@ -132,8 +148,8 @@ func AdminEntityList(ctx echo.Context, params AdminEntityListParams) error {
g = append(g, Td(Text(h)))
}
g = append(g,
Td(A(Href(r.Path(routenames.AdminEntityEdit(params.EntityType.Name), row.ID)), Text("Edit"))),
Td(A(Href(r.Path(routenames.AdminEntityDelete(params.EntityType.Name), row.ID)), Text("Delete"))),
Td(ButtonLink(r.Path(routenames.AdminEntityEdit(params.EntityType.Name), row.ID), "is-link", "Edit")),
Td(ButtonLink(r.Path(routenames.AdminEntityDelete(params.EntityType.Name), row.ID), "is-danger", "Delete")),
)
return g
}