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 {
|
func (h *Admin) EntityAdd(n *gen.Type) echo.HandlerFunc {
|
||||||
return func(ctx echo.Context) error {
|
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 {
|
func (h *Admin) EntityEdit(n *gen.Type) echo.HandlerFunc {
|
||||||
return func(ctx echo.Context) error {
|
return func(ctx echo.Context) error {
|
||||||
v := ctx.Get(context.AdminEntityKey).(map[string][]string)
|
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"
|
. "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?
|
// TODO inline validation?
|
||||||
|
isNew := values == nil
|
||||||
nodes := make(Group, 0)
|
nodes := make(Group, 0)
|
||||||
|
|
||||||
getValue := func(name string) string {
|
getValue := func(name string) string {
|
||||||
|
// Values in the submitted form take precedence.
|
||||||
if value := r.Context.FormValue(name); value != "" {
|
if value := r.Context.FormValue(name); value != "" {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fallback to the entity's values, if being edited.
|
||||||
if values != nil && len(values[name]) > 0 {
|
if values != nil && len(values[name]) > 0 {
|
||||||
return values[name][0] // TODO cardinality
|
return values[name][0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attempt to add form elements for all editable entity fields.
|
||||||
for _, f := range schema.Fields {
|
for _, f := range schema.Fields {
|
||||||
// TODO cardinality?
|
// TODO cardinality?
|
||||||
if !isNew && f.Immutable {
|
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))
|
nodes = append(nodes, InputField(p))
|
||||||
|
|
||||||
case field.TypeTime:
|
case field.TypeTime:
|
||||||
nodes = append(nodes, InputField(InputFieldParams{
|
nodes = append(nodes, InputField(InputFieldParams{
|
||||||
Name: f.Name,
|
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),
|
Label: admin.FieldLabel(f.Name),
|
||||||
Value: getValue(f.Name),
|
Value: getValue(f.Name),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
case field.TypeInt, field.TypeInt8, field.TypeInt16, field.TypeInt32, field.TypeInt64,
|
case field.TypeInt, field.TypeInt8, field.TypeInt16, field.TypeInt32, field.TypeInt64,
|
||||||
field.TypeUint, field.TypeUint8, field.TypeUint16, field.TypeUint32, field.TypeUint64,
|
field.TypeUint, field.TypeUint8, field.TypeUint16, field.TypeUint32, field.TypeUint64,
|
||||||
field.TypeFloat32, field.TypeFloat64:
|
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),
|
Label: admin.FieldLabel(f.Name),
|
||||||
Value: getValue(f.Name),
|
Value: getValue(f.Name),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
case field.TypeBool:
|
case field.TypeBool:
|
||||||
nodes = append(nodes, Checkbox(CheckboxParams{
|
nodes = append(nodes, Checkbox(CheckboxParams{
|
||||||
Name: f.Name,
|
Name: f.Name,
|
||||||
Label: admin.FieldLabel(f.Name),
|
Label: admin.FieldLabel(f.Name),
|
||||||
Checked: getValue(f.Name) == "true",
|
Checked: getValue(f.Name) == "true",
|
||||||
}))
|
}))
|
||||||
|
|
||||||
case field.TypeEnum:
|
case field.TypeEnum:
|
||||||
options := make([]Choice, 0, len(f.Enums)+1)
|
options := make([]Choice, 0, len(f.Enums)+1)
|
||||||
if f.Optional {
|
if f.Optional {
|
||||||
|
|
@ -95,18 +103,23 @@ func AdminEntity(r *ui.Request, isNew bool, schema *load.Schema, values url.Valu
|
||||||
Value: getValue(f.Name),
|
Value: getValue(f.Name),
|
||||||
Options: options,
|
Options: options,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
default:
|
default:
|
||||||
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
|
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(
|
return Form(
|
||||||
Method(http.MethodPost),
|
Method(http.MethodPost),
|
||||||
nodes,
|
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 {
|
func AdminEntityDelete(r *ui.Request, entityTypeName string) Node {
|
||||||
return Form(
|
return Form(
|
||||||
Method(http.MethodPost),
|
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(
|
ControlGroup(
|
||||||
FormButton("is-link", "Delete"),
|
FormButton("is-link", "Delete"),
|
||||||
ButtonLink(
|
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)
|
r := ui.NewRequest(ctx)
|
||||||
if isNew {
|
if values == nil {
|
||||||
r.Title = fmt.Sprintf("Add %s", schema.Name)
|
r.Title = fmt.Sprintf("Add %s", schema.Name)
|
||||||
} else {
|
} else {
|
||||||
r.Title = fmt.Sprintf("Edit %s", schema.Name)
|
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(
|
return r.Render(
|
||||||
layouts.Primary,
|
layouts.Primary,
|
||||||
forms.AdminEntity(r, isNew, schema, values),
|
forms.AdminEntity(r, schema, values),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue