Started on dynamic entity add form.

This commit is contained in:
mikestefanello 2025-03-31 19:11:08 -04:00
parent 33e98f9a9e
commit 9a92c4aad6
5 changed files with 81 additions and 23 deletions

View file

@ -10,6 +10,7 @@ import (
"entgo.io/ent/dialect/sql"
"entgo.io/ent/entc/gen"
"entgo.io/ent/entc/load"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/ent"
"github.com/mikestefanello/pagoda/ent/passwordtoken"
@ -45,7 +46,7 @@ func (h *Admin) Routes(g *echo.Group) {
entities := g.Group("/admin/content")
for _, p := range h.getEntityPlugins() {
pg := entities.Group(fmt.Sprintf("/%s", p.ID))
pg := entities.Group(fmt.Sprintf("/%s", strings.ToLower(p.ID)))
pg.GET("", h.EntityList(p)).Name = p.RouteNameList()
pg.POST("", h.EntityList(p)).Name = p.RouteNameListSubmit()
pg.GET("/add", h.EntityAdd(p)).Name = p.RouteNameAdd()
@ -103,13 +104,19 @@ func (h *Admin) EntityList(p AdminEntityPlugin) echo.HandlerFunc {
func (h *Admin) EntityAdd(p AdminEntityPlugin) echo.HandlerFunc {
return func(ctx echo.Context) error {
return nil
var schema *load.Schema
for _, s := range h.graph.Schemas {
if s.Name == p.ID {
schema = s
}
}
return pages.AdminEntityAdd(ctx, schema)
}
}
func (h *Admin) EntityAddSubmit(p AdminEntityPlugin) echo.HandlerFunc {
return func(ctx echo.Context) error {
return nil
return h.EntityAdd(p)(ctx)
}
}
@ -193,7 +200,7 @@ func (p *AdminEntityPlugin) RouteNameDeleteSubmit() string {
func (h *Admin) getEntityPlugins() []AdminEntityPlugin {
return []AdminEntityPlugin{
{
ID: "user",
ID: "User",
Label: "User",
LabelPlural: "Users",
Heading: []string{
@ -238,7 +245,7 @@ func (h *Admin) getEntityPlugins() []AdminEntityPlugin {
},
},
{
ID: "passwordtoken",
ID: "PasswordToken",
Label: "Password token",
LabelPlural: "Password tokens",
Heading: []string{

View file

@ -39,10 +39,10 @@ func (h *Auth) Init(c *services.Container) error {
}
func (h *Auth) Routes(g *echo.Group) {
g.GET("/logout", h.Logout, middleware.RequireAuthentication()).Name = routenames.Logout
g.GET("/logout", h.Logout, middleware.RequireAuthentication).Name = routenames.Logout
g.GET("/email/verify/:token", h.VerifyEmail).Name = routenames.VerifyEmail
noAuth := g.Group("/user", middleware.RequireNoAuthentication())
noAuth := g.Group("/user", middleware.RequireNoAuthentication)
noAuth.GET("/login", h.LoginPage).Name = routenames.Login
noAuth.POST("/login", h.LoginSubmit).Name = routenames.LoginSubmit
noAuth.GET("/register", h.RegisterPage).Name = routenames.Register

View file

@ -83,27 +83,23 @@ func LoadValidPasswordToken(authClient *services.AuthClient) echo.MiddlewareFunc
}
// RequireAuthentication requires that the user be authenticated in order to proceed
func RequireAuthentication() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if u := c.Get(context.AuthenticatedUserKey); u == nil {
return echo.NewHTTPError(http.StatusUnauthorized)
}
return next(c)
func RequireAuthentication(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if u := c.Get(context.AuthenticatedUserKey); u == nil {
return echo.NewHTTPError(http.StatusUnauthorized)
}
return next(c)
}
}
// RequireNoAuthentication requires that the user not be authenticated in order to proceed
func RequireNoAuthentication() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if u := c.Get(context.AuthenticatedUserKey); u != nil {
return echo.NewHTTPError(http.StatusForbidden)
}
return next(c)
func RequireNoAuthentication(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if u := c.Get(context.AuthenticatedUserKey); u != nil {
return echo.NewHTTPError(http.StatusForbidden)
}
return next(c)
}
}

View file

@ -153,6 +153,8 @@ func FileField(name, label string) Node {
func formFieldStatusClass(fm form.Form, formField string) string {
switch {
case fm == nil:
return ""
case !fm.IsSubmitted():
return ""
case fm.FieldHasErrors(formField):
@ -163,6 +165,10 @@ func formFieldStatusClass(fm form.Form, formField string) string {
}
func formFieldErrors(fm form.Form, field string) Node {
if fm == nil {
return nil
}
errs := fm.GetFieldErrors(field)
if len(errs) == 0 {
return nil

View file

@ -1,8 +1,11 @@
package pages
import (
"fmt"
"net/http"
"entgo.io/ent/entc/load"
"entgo.io/ent/schema/field"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/pager"
"github.com/mikestefanello/pagoda/pkg/ui"
@ -31,6 +34,52 @@ func AdminEntityDelete(ctx echo.Context) error {
return r.Render(layouts.Admin, form)
}
func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
r := ui.NewRequest(ctx)
r.Title = fmt.Sprintf("Add %s", "entity") // TODO
nodes := make(Group, 0)
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,
}))
case field.TypeTime:
nodes = append(nodes, InputField(InputFieldParams{
Name: f.Name,
InputType: "datetime",
Label: f.Name,
}))
case field.TypeBool:
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
default:
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
}
}
for _, e := range schema.Edges {
if e.Inverse {
continue
}
nodes = append(nodes, InputField(InputFieldParams{
Name: e.Name,
InputType: "number",
Label: e.Name,
}))
}
nodes = append(nodes, ControlGroup(
FormButton("is-primary", "Submit"),
ButtonLink("/", "is-secondary", "Cancel"),
), CSRF(r))
return r.Render(layouts.Admin, Form(Method(http.MethodPost), nodes))
}
type AdminEntityListParams struct {
Title string
Headers []string