Clean up.

This commit is contained in:
mikestefanello 2025-04-13 09:32:43 -04:00
parent 60afbb18c0
commit 8c3f04e859
5 changed files with 26 additions and 27 deletions

View file

@ -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 {

View file

@ -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)),

View file

@ -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(),

View file

@ -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()
}
}

View file

@ -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: