From 8c3f04e859aeaf266fbad8afa1cb66d58965852b Mon Sep 17 00:00:00 2001 From: mikestefanello <552328+mikestefanello@users.noreply.github.com> Date: Sun, 13 Apr 2025 09:32:43 -0400 Subject: [PATCH] Clean up. --- ent/admin/extension.go | 9 +++++---- ent/admin/handler.go | 2 +- ent/schema/passwordtoken.go | 1 + pkg/handlers/admin.go | 3 +++ pkg/ui/pages/entity.go | 38 ++++++++++++++++--------------------- 5 files changed, 26 insertions(+), 27 deletions(-) diff --git a/ent/admin/extension.go b/ent/admin/extension.go index 4632b60..3b16260 100644 --- a/ent/admin/extension.go +++ b/ent/admin/extension.go @@ -26,7 +26,7 @@ func (*Extension) Templates() []*gen.Template { gen.NewTemplate("admin"). Funcs(template.FuncMap{ "fieldName": fieldName, - "fieldLabel": fieldLabel, + "fieldLabel": FieldLabel, "fieldIsPointer": fieldIsPointer, }). ParseFS(templateDir, "templates/*tmpl"), @@ -51,13 +51,14 @@ func fieldName(name string) string { return strings.Join(parts, "") } -func fieldLabel(name string) string { +func FieldLabel(name string) string { if len(name) == 0 { return name } - out := strings.ReplaceAll(name, "_", " ") - return upperFirst(out) + name = strings.ReplaceAll(name, "_id", "_ID") + name = strings.ReplaceAll(name, "_", " ") + return upperFirst(name) } func fieldIsPointer(f *gen.Field) bool { diff --git a/ent/admin/handler.go b/ent/admin/handler.go index 6c09326..75f1e9e 100644 --- a/ent/admin/handler.go +++ b/ent/admin/handler.go @@ -138,7 +138,7 @@ func (h *Handler) PasswordTokenList(ctx echo.Context) (*EntityList, error) { list := &EntityList{ Columns: []string{ - "User id", + "User ID", "Created at", }, Entities: make([]EntityValues, 0, len(res)), diff --git a/ent/schema/passwordtoken.go b/ent/schema/passwordtoken.go index 81195af..e3b3e04 100644 --- a/ent/schema/passwordtoken.go +++ b/ent/schema/passwordtoken.go @@ -20,6 +20,7 @@ type PasswordToken struct { // Fields of the PasswordToken. func (PasswordToken) Fields() []ent.Field { return []ent.Field{ + // TODO rename to Token field.String("hash"). Sensitive(). NotEmpty(), diff --git a/pkg/handlers/admin.go b/pkg/handlers/admin.go index aa0b670..029f1ed 100644 --- a/pkg/handlers/admin.go +++ b/pkg/handlers/admin.go @@ -129,6 +129,7 @@ func (h *Admin) EntityAddSubmit(n *gen.Type) echo.HandlerFunc { return redirect. New(ctx). Route(routenames.AdminEntityList(n.Name)). + StatusCode(http.StatusFound). Go() } } @@ -154,6 +155,7 @@ func (h *Admin) EntityEditSubmit(n *gen.Type) echo.HandlerFunc { return redirect. New(ctx). Route(routenames.AdminEntityList(n.Name)). + StatusCode(http.StatusFound). Go() } } @@ -177,6 +179,7 @@ func (h *Admin) EntityDeleteSubmit(n *gen.Type) echo.HandlerFunc { return redirect. New(ctx). Route(routenames.AdminEntityList(n.Name)). + StatusCode(http.StatusFound). Go() } } diff --git a/pkg/ui/pages/entity.go b/pkg/ui/pages/entity.go index ab44ac4..79238aa 100644 --- a/pkg/ui/pages/entity.go +++ b/pkg/ui/pages/entity.go @@ -4,9 +4,7 @@ import ( "fmt" "net/http" "net/url" - "strings" "time" - "unicode" "entgo.io/ent/entc/gen" "entgo.io/ent/entc/load" @@ -49,15 +47,6 @@ func AdminEntityForm(ctx echo.Context, isNew bool, schema *load.Schema, values u nodes := make(Group, 0) - label := func(name string) string { - if len(name) == 0 { - return name - } - text := []rune(strings.ReplaceAll(name, "_", " ")) - text[0] = unicode.ToUpper(text[0]) - return string(text) - } - getValue := func(name string) string { if value := ctx.FormValue(name); value != "" { return value @@ -78,22 +67,27 @@ func AdminEntityForm(ctx echo.Context, isNew bool, schema *load.Schema, values u // TODO sensitive edits switch f.Info.Type { case field.TypeString: - inputType := "text" - if f.Sensitive { - inputType = "password" - } - nodes = append(nodes, InputField(InputFieldParams{ + p := InputFieldParams{ Name: f.Name, - InputType: inputType, - Label: label(f.Name), + InputType: "text", + Label: admin.FieldLabel(f.Name), Value: getValue(f.Name), - })) + } + + if f.Sensitive { + p.InputType = "password" + if !isNew { + p.Placeholder = "*****" + p.Help = "SENSITIVE: This field will only be updated if a value is provided." + } + } + nodes = append(nodes, InputField(p)) case field.TypeTime: // todo make this easier nodes = append(nodes, InputField(InputFieldParams{ Name: f.Name, InputType: "text", - Label: label(f.Name), + Label: admin.FieldLabel(f.Name), Help: fmt.Sprintf("Use the following format: %s", time.Now().Format(time.RFC3339)), Value: getValue(f.Name), })) @@ -102,13 +96,13 @@ func AdminEntityForm(ctx echo.Context, isNew bool, schema *load.Schema, values u nodes = append(nodes, InputField(InputFieldParams{ Name: f.Name, InputType: "number", - Label: label(f.Name), + Label: admin.FieldLabel(f.Name), Value: getValue(f.Name), })) case field.TypeBool: nodes = append(nodes, Checkbox(CheckboxParams{ Name: f.Name, - Label: label(f.Name), + Label: admin.FieldLabel(f.Name), Checked: getValue(f.Name) == "true", })) case field.TypeEnum: