Code cleanup.
This commit is contained in:
parent
38b65878f8
commit
d672e8cb60
4 changed files with 29 additions and 13 deletions
|
|
@ -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.AdminEntityInput(ctx, true, h.getEntitySchema(n), nil)
|
||||
return pages.AdminEntityInput(ctx, h.getEntitySchema(n), nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -129,7 +129,7 @@ func (h *Admin) EntityAddSubmit(n *gen.Type) echo.HandlerFunc {
|
|||
func (h *Admin) EntityEdit(n *gen.Type) echo.HandlerFunc {
|
||||
return func(ctx echo.Context) error {
|
||||
v := ctx.Get(context.AdminEntityKey).(map[string][]string)
|
||||
return pages.AdminEntityInput(ctx, false, h.getEntitySchema(n), v)
|
||||
return pages.AdminEntityInput(ctx, h.getEntitySchema(n), v)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,22 +14,26 @@ import (
|
|||
. "maragu.dev/gomponents/html"
|
||||
)
|
||||
|
||||
func AdminEntity(r *ui.Request, isNew bool, schema *load.Schema, values url.Values) Node {
|
||||
func AdminEntity(r *ui.Request, schema *load.Schema, values url.Values) Node {
|
||||
// TODO inline validation?
|
||||
isNew := values == nil
|
||||
nodes := make(Group, 0)
|
||||
|
||||
getValue := func(name string) string {
|
||||
// Values in the submitted form take precedence.
|
||||
if value := r.Context.FormValue(name); value != "" {
|
||||
return value
|
||||
}
|
||||
|
||||
// Fallback to the entity's values, if being edited.
|
||||
if values != nil && len(values[name]) > 0 {
|
||||
return values[name][0] // TODO cardinality
|
||||
return values[name][0]
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// Attempt to add form elements for all editable entity fields.
|
||||
for _, f := range schema.Fields {
|
||||
// TODO cardinality?
|
||||
if !isNew && f.Immutable {
|
||||
|
|
@ -53,6 +57,7 @@ func AdminEntity(r *ui.Request, isNew bool, schema *load.Schema, values url.Valu
|
|||
}
|
||||
}
|
||||
nodes = append(nodes, InputField(p))
|
||||
|
||||
case field.TypeTime:
|
||||
nodes = append(nodes, InputField(InputFieldParams{
|
||||
Name: f.Name,
|
||||
|
|
@ -60,6 +65,7 @@ func AdminEntity(r *ui.Request, isNew bool, schema *load.Schema, values url.Valu
|
|||
Label: admin.FieldLabel(f.Name),
|
||||
Value: getValue(f.Name),
|
||||
}))
|
||||
|
||||
case field.TypeInt, field.TypeInt8, field.TypeInt16, field.TypeInt32, field.TypeInt64,
|
||||
field.TypeUint, field.TypeUint8, field.TypeUint16, field.TypeUint32, field.TypeUint64,
|
||||
field.TypeFloat32, field.TypeFloat64:
|
||||
|
|
@ -69,12 +75,14 @@ func AdminEntity(r *ui.Request, isNew bool, schema *load.Schema, values url.Valu
|
|||
Label: admin.FieldLabel(f.Name),
|
||||
Value: getValue(f.Name),
|
||||
}))
|
||||
|
||||
case field.TypeBool:
|
||||
nodes = append(nodes, Checkbox(CheckboxParams{
|
||||
Name: f.Name,
|
||||
Label: admin.FieldLabel(f.Name),
|
||||
Checked: getValue(f.Name) == "true",
|
||||
}))
|
||||
|
||||
case field.TypeEnum:
|
||||
options := make([]Choice, 0, len(f.Enums)+1)
|
||||
if f.Optional {
|
||||
|
|
@ -95,18 +103,23 @@ func AdminEntity(r *ui.Request, isNew bool, schema *load.Schema, values url.Valu
|
|||
Value: getValue(f.Name),
|
||||
Options: options,
|
||||
}))
|
||||
|
||||
default:
|
||||
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
|
||||
}
|
||||
}
|
||||
|
||||
nodes = append(nodes, ControlGroup(
|
||||
FormButton("is-primary", "Submit"),
|
||||
ButtonLink(r.Path(routenames.AdminEntityList(schema.Name)), "is-secondary", "Cancel"),
|
||||
), CSRF(r))
|
||||
|
||||
return Form(
|
||||
Method(http.MethodPost),
|
||||
nodes,
|
||||
ControlGroup(
|
||||
FormButton("is-primary", "Submit"),
|
||||
ButtonLink(
|
||||
r.Path(routenames.AdminEntityList(schema.Name)),
|
||||
"is-secondary",
|
||||
"Cancel",
|
||||
),
|
||||
),
|
||||
CSRF(r),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,10 @@ import (
|
|||
func AdminEntityDelete(r *ui.Request, entityTypeName string) Node {
|
||||
return Form(
|
||||
Method(http.MethodPost),
|
||||
P(Class("subtitle"), Textf("Are you sure you want to delete this %s?", entityTypeName)),
|
||||
P(
|
||||
Class("subtitle"),
|
||||
Textf("Are you sure you want to delete this %s?", entityTypeName),
|
||||
),
|
||||
ControlGroup(
|
||||
FormButton("is-link", "Delete"),
|
||||
ButtonLink(
|
||||
|
|
|
|||
|
|
@ -28,9 +28,9 @@ func AdminEntityDelete(ctx echo.Context, entityTypeName string) error {
|
|||
)
|
||||
}
|
||||
|
||||
func AdminEntityInput(ctx echo.Context, isNew bool, schema *load.Schema, values url.Values) error {
|
||||
func AdminEntityInput(ctx echo.Context, schema *load.Schema, values url.Values) error {
|
||||
r := ui.NewRequest(ctx)
|
||||
if isNew {
|
||||
if values == nil {
|
||||
r.Title = fmt.Sprintf("Add %s", schema.Name)
|
||||
} else {
|
||||
r.Title = fmt.Sprintf("Edit %s", schema.Name)
|
||||
|
|
@ -38,7 +38,7 @@ func AdminEntityInput(ctx echo.Context, isNew bool, schema *load.Schema, values
|
|||
|
||||
return r.Render(
|
||||
layouts.Primary,
|
||||
forms.AdminEntity(r, isNew, schema, values),
|
||||
forms.AdminEntity(r, schema, values),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue