Support form state.
This commit is contained in:
parent
ce9b58bf4a
commit
85271607d0
2 changed files with 26 additions and 11 deletions
|
|
@ -104,7 +104,7 @@ func (h *Admin) EntityList(n *gen.Type) echo.HandlerFunc {
|
||||||
|
|
||||||
func (h *Admin) EntityAdd(n *gen.Type) echo.HandlerFunc {
|
func (h *Admin) EntityAdd(n *gen.Type) echo.HandlerFunc {
|
||||||
return func(ctx echo.Context) error {
|
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)
|
err := h.admin.Create(ctx, n.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg.Danger(ctx, err.Error())
|
msg.Danger(ctx, err.Error())
|
||||||
// TODO : hold state
|
|
||||||
return h.EntityAdd(n)(ctx)
|
return h.EntityAdd(n)(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package pages
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
@ -21,10 +22,6 @@ import (
|
||||||
. "maragu.dev/gomponents/html"
|
. "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 {
|
func AdminEntityDelete(ctx echo.Context, entityTypeName string) error {
|
||||||
r := ui.NewRequest(ctx)
|
r := ui.NewRequest(ctx)
|
||||||
form := Form(
|
form := Form(
|
||||||
|
|
@ -40,10 +37,9 @@ func AdminEntityDelete(ctx echo.Context, entityTypeName string) error {
|
||||||
return r.Render(layouts.Admin, form)
|
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 := ui.NewRequest(ctx)
|
||||||
r.Title = fmt.Sprintf("Add %s", schema.Name) // TODO
|
r.Title = fmt.Sprintf("Add %s", schema.Name) // TODO
|
||||||
|
|
||||||
nodes := make(Group, 0)
|
nodes := make(Group, 0)
|
||||||
|
|
||||||
label := func(name string) string {
|
label := func(name string) string {
|
||||||
|
|
@ -55,13 +51,27 @@ func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
|
||||||
return string(text)
|
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 {
|
for _, f := range schema.Fields {
|
||||||
|
// TODO cardinality?
|
||||||
switch f.Info.Type {
|
switch f.Info.Type {
|
||||||
case field.TypeString:
|
case field.TypeString:
|
||||||
nodes = append(nodes, InputField(InputFieldParams{
|
nodes = append(nodes, InputField(InputFieldParams{
|
||||||
Name: f.Name,
|
Name: f.Name,
|
||||||
InputType: "text",
|
InputType: "text",
|
||||||
Label: label(f.Name),
|
Label: label(f.Name),
|
||||||
|
Value: getValue(f.Name),
|
||||||
}))
|
}))
|
||||||
case field.TypeTime:
|
case field.TypeTime:
|
||||||
nodes = append(nodes, InputField(InputFieldParams{
|
nodes = append(nodes, InputField(InputFieldParams{
|
||||||
|
|
@ -69,10 +79,15 @@ func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
|
||||||
InputType: "text",
|
InputType: "text",
|
||||||
Label: label(f.Name),
|
Label: label(f.Name),
|
||||||
Help: fmt.Sprintf("Use the following format: %s", time.Now().Format(time.RFC3339)),
|
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:
|
case field.TypeBool:
|
||||||
|
// TODO
|
||||||
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
|
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:
|
default:
|
||||||
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
|
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,
|
Name: e.Name,
|
||||||
InputType: "number",
|
InputType: "number",
|
||||||
Label: label(e.Name),
|
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(Text(h)))
|
||||||
}
|
}
|
||||||
g = append(g,
|
g = append(g,
|
||||||
Td(A(Href(r.Path(routenames.AdminEntityEdit(params.EntityType.Name), row.ID)), Text("Edit"))),
|
Td(ButtonLink(r.Path(routenames.AdminEntityEdit(params.EntityType.Name), row.ID), "is-link", "Edit")),
|
||||||
Td(A(Href(r.Path(routenames.AdminEntityDelete(params.EntityType.Name), row.ID)), Text("Delete"))),
|
Td(ButtonLink(r.Path(routenames.AdminEntityDelete(params.EntityType.Name), row.ID), "is-danger", "Delete")),
|
||||||
)
|
)
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue