Added ogent for entity code gen.

This commit is contained in:
mikestefanello 2025-04-01 10:20:14 -04:00
parent 9a92c4aad6
commit 196d34cc1f
29 changed files with 13445 additions and 12 deletions

View file

@ -10,6 +10,13 @@ import (
)
func JS(r *ui.Request) Node {
return Group{
Script(Src("https://unpkg.com/htmx.org@2.0.0/dist/htmx.min.js")),
Script(Src("https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"), Defer()),
}
}
func HtmxListeners(r *ui.Request) Node {
const htmxErr = `
document.body.addEventListener('htmx:beforeSwap', function(evt) {
if (evt.detail.xhr.status >= 400){
@ -28,8 +35,6 @@ func JS(r *ui.Request) Node {
`
return Group{
Script(Src("https://unpkg.com/htmx.org@2.0.0/dist/htmx.min.js")),
Script(Src("https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"), Defer()),
Script(Raw(htmxErr)),
Iff(len(r.CSRF) > 0, func() Node {
return Script(Raw(fmt.Sprintf(htmxCSRF, r.CSRF)))

View file

@ -36,6 +36,7 @@ func Admin(r *ui.Request, content Node) Node {
),
),
),
HtmxListeners(r),
),
)
}

View file

@ -3,6 +3,9 @@ package pages
import (
"fmt"
"net/http"
"strings"
"time"
"unicode"
"entgo.io/ent/entc/load"
"entgo.io/ent/schema/field"
@ -40,19 +43,29 @@ func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
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)
}
for _, f := range schema.Fields {
switch f.Info.Type {
case field.TypeString:
nodes = append(nodes, InputField(InputFieldParams{
Name: f.Name,
InputType: "text",
Label: f.Name,
Label: label(f.Name),
}))
case field.TypeTime:
nodes = append(nodes, InputField(InputFieldParams{
Name: f.Name,
InputType: "datetime",
Label: f.Name,
InputType: "text",
Label: label(f.Name),
Help: fmt.Sprintf("Use the following format: %s", time.Now().Format(time.RFC3339)),
}))
case field.TypeBool:
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
@ -68,16 +81,19 @@ func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
nodes = append(nodes, InputField(InputFieldParams{
Name: e.Name,
InputType: "number",
Label: e.Name,
Label: label(e.Name),
}))
}
nodes = append(nodes, ControlGroup(
FormButton("is-primary", "Submit"),
ButtonLink("/", "is-secondary", "Cancel"),
ButtonLink("/", "is-secondary", "Cancel"), // todo
), CSRF(r))
return r.Render(layouts.Admin, Form(Method(http.MethodPost), nodes))
return r.Render(layouts.Admin, Form(
Method(http.MethodPost),
nodes,
))
}
type AdminEntityListParams struct {