Make admin menu links dynamic.
This commit is contained in:
parent
8219b96df7
commit
36bc20b90f
6 changed files with 42 additions and 15 deletions
|
|
@ -132,10 +132,11 @@ func (h *Handler) PasswordTokenDelete(ctx echo.Context, id int) error {
|
|||
}
|
||||
|
||||
func (h *Handler) PasswordTokenList(ctx echo.Context) (*EntityList, error) {
|
||||
page, offset := h.getPageAndOffset(ctx)
|
||||
res, err := h.client.PasswordToken.
|
||||
Query().
|
||||
Limit(h.Config.ItemsPerPage + 1).
|
||||
Offset(h.getOffset(ctx)).
|
||||
Offset(offset).
|
||||
Order(passwordtoken.ByID(sql.OrderDesc())).
|
||||
All(ctx.Request().Context())
|
||||
|
||||
|
|
@ -149,6 +150,7 @@ func (h *Handler) PasswordTokenList(ctx echo.Context) (*EntityList, error) {
|
|||
"Created at",
|
||||
},
|
||||
Entities: make([]EntityValues, 0, len(res)),
|
||||
Page: page,
|
||||
HasNextPage: len(res) > h.Config.ItemsPerPage,
|
||||
}
|
||||
|
||||
|
|
@ -225,10 +227,11 @@ func (h *Handler) UserDelete(ctx echo.Context, id int) error {
|
|||
}
|
||||
|
||||
func (h *Handler) UserList(ctx echo.Context) (*EntityList, error) {
|
||||
page, offset := h.getPageAndOffset(ctx)
|
||||
res, err := h.client.User.
|
||||
Query().
|
||||
Limit(h.Config.ItemsPerPage + 1).
|
||||
Offset(h.getOffset(ctx)).
|
||||
Offset(offset).
|
||||
Order(user.ByID(sql.OrderDesc())).
|
||||
All(ctx.Request().Context())
|
||||
|
||||
|
|
@ -244,6 +247,7 @@ func (h *Handler) UserList(ctx echo.Context) (*EntityList, error) {
|
|||
"Created at",
|
||||
},
|
||||
Entities: make([]EntityValues, 0, len(res)),
|
||||
Page: page,
|
||||
HasNextPage: len(res) > h.Config.ItemsPerPage,
|
||||
}
|
||||
|
||||
|
|
@ -275,13 +279,13 @@ func (h *Handler) UserGet(ctx echo.Context, id int) (url.Values, error) {
|
|||
return v, err
|
||||
}
|
||||
|
||||
func (h *Handler) getOffset(ctx echo.Context) int {
|
||||
func (h *Handler) getPageAndOffset(ctx echo.Context) (int, int) {
|
||||
if page, err := strconv.Atoi(ctx.QueryParam(h.Config.PageQueryKey)); err == nil {
|
||||
if page > 1 {
|
||||
return (page - 1) * h.Config.ItemsPerPage
|
||||
return page, (page - 1) * h.Config.ItemsPerPage
|
||||
}
|
||||
}
|
||||
return 0
|
||||
return 1, 0
|
||||
}
|
||||
|
||||
func (h *Handler) bind(ctx echo.Context, entity any) error {
|
||||
|
|
|
|||
|
|
@ -156,10 +156,11 @@
|
|||
}
|
||||
|
||||
func (h *Handler) {{ $n.Name }}List(ctx echo.Context) (*EntityList, error) {
|
||||
page, offset := h.getPageAndOffset(ctx)
|
||||
res, err := h.client.{{ $n.Name }}.
|
||||
Query().
|
||||
Limit(h.Config.ItemsPerPage+1).
|
||||
Offset(h.getOffset(ctx)).
|
||||
Offset(offset).
|
||||
Order({{ $n.Package }}.ByID(sql.OrderDesc())).
|
||||
All(ctx.Request().Context())
|
||||
|
||||
|
|
@ -176,6 +177,7 @@
|
|||
{{- end }}
|
||||
},
|
||||
Entities: make([]EntityValues, 0, len(res)),
|
||||
Page: page,
|
||||
HasNextPage: len(res) > h.Config.ItemsPerPage,
|
||||
}
|
||||
|
||||
|
|
@ -223,13 +225,13 @@
|
|||
}
|
||||
{{ end }}
|
||||
|
||||
func (h *Handler) getOffset(ctx echo.Context) int {
|
||||
func (h *Handler) getPageAndOffset(ctx echo.Context) (int, int) {
|
||||
if page, err := strconv.Atoi(ctx.QueryParam(h.Config.PageQueryKey)); err == nil {
|
||||
if page > 1 {
|
||||
return (page-1) * h.Config.ItemsPerPage
|
||||
return page, (page-1) * h.Config.ItemsPerPage
|
||||
}
|
||||
}
|
||||
return 0
|
||||
return 1, 0
|
||||
}
|
||||
|
||||
func (h *Handler) bind(ctx echo.Context, entity any) error {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
// Code generated by ent, DO NOT EDIT.
|
||||
package admin
|
||||
|
||||
{{ range $n := $.Nodes }}
|
||||
{{- range $n := $.Nodes }}
|
||||
type {{ $n.Name }} struct {
|
||||
{{- range $f := $n.Fields }}
|
||||
{{ fieldName $f.Name }} {{ if (fieldIsPointer $f) }}*{{ end }}{{ $f.Type }} `form:"{{ $f.Name }}"`
|
||||
|
|
@ -16,6 +16,7 @@
|
|||
type EntityList struct {
|
||||
Columns []string
|
||||
Entities []EntityValues
|
||||
Page int
|
||||
HasNextPage bool
|
||||
}
|
||||
|
||||
|
|
@ -30,4 +31,12 @@
|
|||
TimeFormat string
|
||||
}
|
||||
|
||||
func GetEntityTypeNames() []string {
|
||||
return []string{
|
||||
{{- range $n := $.Nodes }}
|
||||
"{{ $n.Name }}",
|
||||
{{- end }}
|
||||
}
|
||||
}
|
||||
|
||||
{{ end }}
|
||||
|
|
@ -20,6 +20,7 @@ type User struct {
|
|||
type EntityList struct {
|
||||
Columns []string
|
||||
Entities []EntityValues
|
||||
Page int
|
||||
HasNextPage bool
|
||||
}
|
||||
|
||||
|
|
@ -33,3 +34,10 @@ type HandlerConfig struct {
|
|||
PageQueryKey string
|
||||
TimeFormat string
|
||||
}
|
||||
|
||||
func GetEntityTypeNames() []string {
|
||||
return []string{
|
||||
"PasswordToken",
|
||||
"User",
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,6 @@ func (h *Admin) Routes(g *echo.Group) {
|
|||
// TODO admin user status middleware
|
||||
entities := g.Group("/admin/content")
|
||||
|
||||
// TODO: can we generate something we can loop instead?
|
||||
for _, n := range h.graph.Nodes {
|
||||
ng := entities.Group(fmt.Sprintf("/%s", strings.ToLower(n.Name)))
|
||||
ng.GET("", h.EntityList(n)).
|
||||
|
|
@ -71,7 +70,6 @@ func (h *Admin) Routes(g *echo.Group) {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO, maybe this can be used outside of admin stuff as well?
|
||||
func (h *Admin) middlewareEntityLoad(n *gen.Type) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(ctx echo.Context) error {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package layouts
|
||||
|
||||
import (
|
||||
"github.com/mikestefanello/pagoda/ent/admin"
|
||||
"github.com/mikestefanello/pagoda/pkg/routenames"
|
||||
"github.com/mikestefanello/pagoda/pkg/ui"
|
||||
. "github.com/mikestefanello/pagoda/pkg/ui/components"
|
||||
|
|
@ -42,17 +43,22 @@ func Admin(r *ui.Request, content Node) Node {
|
|||
}
|
||||
|
||||
func adminMenu(r *ui.Request) Node {
|
||||
entityTypeNames := admin.GetEntityTypeNames()
|
||||
entityTypeLinks := make(Group, len(entityTypeNames))
|
||||
for _, n := range entityTypeNames {
|
||||
entityTypeLinks = append(entityTypeLinks, MenuLink(r, n, routenames.AdminEntityList(n)))
|
||||
}
|
||||
|
||||
return Aside(
|
||||
Class("menu"),
|
||||
HxBoost(),
|
||||
P(
|
||||
Class("menu-label"),
|
||||
Text("Content"),
|
||||
Text("Entities"),
|
||||
),
|
||||
Ul(
|
||||
Class("menu-list"),
|
||||
MenuLink(r, "User", routenames.AdminEntityList("User")),
|
||||
MenuLink(r, "PasswordToken", routenames.AdminEntityList("PasswordToken")),
|
||||
entityTypeLinks,
|
||||
),
|
||||
P(
|
||||
Class("menu-label"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue