Skip to content

Commit

Permalink
feat: update templates
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Sep 11, 2024
1 parent ae3ad25 commit 8436f6d
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 53 deletions.
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
templates.Get("/new", handlers.NewTemplate())
templates.Get("/:id", handlers.ShowTemplate())
templates.Delete("/:id", handlers.DeleteTemplate())
templates.Get("/:id/edit/body", handlers.EditTemplateBody())
templates.Put("/:id/edit/body", handlers.EditTemplateBody())
templates.Get("/:id/edit/title", handlers.EditTemplateTitle())
templates.Put("/:id/edit/title", handlers.EditTemplateTitle())
templates.Post("/new", handlers.CreateTemplate())

// Me ...
Expand Down
28 changes: 28 additions & 0 deletions internal/adapters/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,31 @@ func (a *handlers) DeleteTemplate() fiber.Handler {
return templates.NewDeleteTemplateController(a.store)
})
}

// EditTemplateBody ...
func (a *handlers) EditTemplateBody() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return templates.NewEditBodyController(a.store)
})
}

// UpdateTemplateBody ...
func (a *handlers) UpdateTemplateBody() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return templates.NewEditBodyController(a.store)
})
}

// EditTemplateTitle ...
func (a *handlers) EditTemplateTitle() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return templates.NewEditTitleController(a.store)
})
}

// UpdateTemplateTitle ...
func (a *handlers) UpdateTemplateTitle() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return templates.NewEditTitleController(a.store)
})
}
56 changes: 45 additions & 11 deletions internal/components/templates/templates-body-card.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
package templates

import (
"fmt"

"github.com/zeiss/service-lens/internal/builders"
"github.com/zeiss/service-lens/internal/models"
"github.com/zeiss/service-lens/internal/utils"

"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/util"
"github.com/zeiss/fiber-goth/adapters"
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/cards"
"github.com/zeiss/service-lens/internal/models"
"github.com/zeiss/fiber-htmx/components/tailwind"
"github.com/zeiss/pkg/conv"

"go.abhg.dev/goldmark/mermaid"
)

// TemplateBodyCardProps ...
type TemplateBodyCardProps struct {
// ClassNames ...
ClassNames htmx.ClassNames
Template models.Template
Markdown string
// Template ...
Template models.Template
// User ...
User adapters.GothUser
}

// TemplateBodyCard ...
Expand All @@ -20,24 +39,39 @@ func TemplateBodyCard(props TemplateBodyCardProps) htmx.Node {
cards.CardProps{
ClassNames: htmx.Merge(
htmx.ClassNames{
"my-2": true,
"mx-2": true,
tailwind.M2: true,
},
),
},
htmx.HxTarget("this"),
htmx.HxSwap("outerHTML"),
htmx.ID("body"),
cards.Body(
cards.BodyProps{},
htmx.Div(
htmx.Raw(props.Markdown),
htmx.ID("body"),
htmx.Markdown(
conv.Bytes(props.Template.Body),
goldmark.WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
renderer.WithNodeRenderers(
util.Prioritized(
builders.NewMarkdownBuilder(
builders.WithTaskURL(fmt.Sprintf(utils.DesignTasksUrlFormat, props.Template.ID)),
), 1),
),
),
goldmark.WithExtensions(
extension.GFM,
emoji.Emoji,
&mermaid.Extender{},
),
),
),
cards.Actions(
cards.ActionsProps{},
buttons.Outline(
buttons.Button(
buttons.ButtonProps{},
htmx.HxGet(""),
htmx.HxSwap("outerHTML"),
htmx.HxGet(fmt.Sprintf(utils.EditTemplateBodyUrlFormat, props.Template.ID)),
htmx.Text("Edit"),
),
),
Expand Down
52 changes: 52 additions & 0 deletions internal/components/templates/templates-title-card.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package templates

import (
"fmt"

"github.com/zeiss/service-lens/internal/models"
"github.com/zeiss/service-lens/internal/utils"

htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/cards"
"github.com/zeiss/fiber-htmx/components/tailwind"
"github.com/zeiss/fiber-htmx/components/typography"
)

// TemplateTitleCardProps ...
type TemplateTitleCardProps struct {
ClassNames htmx.ClassNames
Template models.Template
Markdown string
}

// TemplateTitleCard ...
func TemplateTitleCard(props TemplateTitleCardProps) htmx.Node {
return cards.CardBordered(
cards.CardProps{
ClassNames: htmx.Merge(
htmx.ClassNames{
tailwind.M2: true,
},
),
},
htmx.HxTarget("this"),
htmx.HxSwap("outerHTML"),
htmx.ID("name"),
cards.Body(
cards.BodyProps{},
typography.H2(
typography.Props{},
htmx.Text(props.Template.Name),
),
cards.Actions(
cards.ActionsProps{},
buttons.Button(
buttons.ButtonProps{},
htmx.HxGet(fmt.Sprintf(utils.EditTemplateTitleUrlFormat, props.Template.ID)),
htmx.Text("Edit"),
),
),
),
)
}
157 changes: 157 additions & 0 deletions internal/controllers/templates/edit-body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package templates

import (
"bytes"
"context"
"fmt"

"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/util"
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/forms"
"github.com/zeiss/fiber-htmx/components/toasts"
seed "github.com/zeiss/gorm-seed"
"github.com/zeiss/pkg/conv"
"github.com/zeiss/pkg/errorx"
"github.com/zeiss/service-lens/internal/builders"
"github.com/zeiss/service-lens/internal/components/designs"
"github.com/zeiss/service-lens/internal/models"
"github.com/zeiss/service-lens/internal/ports"
"github.com/zeiss/service-lens/internal/utils"

htmx "github.com/zeiss/fiber-htmx"
)

var _ = htmx.Controller(&EditBodyControllerImpl{})

// EditBodyControllerImpl ...
type EditBodyControllerImpl struct {
// Template ...
Template models.Template
store seed.Database[ports.ReadTx, ports.ReadWriteTx]
htmx.DefaultController
}

// NewEditBodyController ...
func NewEditBodyController(store seed.Database[ports.ReadTx, ports.ReadWriteTx]) *EditBodyControllerImpl {
return &EditBodyControllerImpl{store: store}
}

// Error ...
func (l *EditBodyControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
}

// Prepare ...
func (l *EditBodyControllerImpl) Prepare() error {
err := l.BindParams(&l.Template)
if err != nil {
return err
}

return l.store.ReadTx(l.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.GetTemplate(ctx, &l.Template)
})
}

// Put ...
func (l *EditBodyControllerImpl) Put() error {
errorx.Panic(l.BindBody(&l.Template))
errorx.Panic(l.store.ReadWriteTx(l.Context(), func(ctx context.Context, tx ports.ReadWriteTx) error {
err := tx.UpdateTemplate(ctx, &l.Template)
if err != nil {
return err
}

markdown := goldmark.New(
goldmark.WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
renderer.WithNodeRenderers(
util.Prioritized(
builders.NewMarkdownBuilder(), 1),
),
),
goldmark.WithExtensions(
extension.GFM,
emoji.Emoji,
),
)

var b bytes.Buffer
err = markdown.Convert([]byte(l.Template.Body), &b)
if err != nil {
return err
}

l.Template.Body = b.String()

return err
}))

return l.Render(
htmx.Fragment(
htmx.Div(
htmx.ID("body"),
htmx.HxSwapOob(conv.String(true)),
htmx.Div(
htmx.Raw(l.Template.Body),
),
),
buttons.Button(
buttons.ButtonProps{},
htmx.HxSwap("outerHTML"),
htmx.HxGet(fmt.Sprintf(utils.EditTemplateBodyUrlFormat, l.Template.ID)),
htmx.Text("Edit"),
),
),
)
}

// Get ...
func (l *EditBodyControllerImpl) Get() error {
return l.Render(
htmx.Fragment(
htmx.Div(
htmx.ID("body"),
htmx.HxSwapOob(conv.String(true)),
htmx.FormElement(
forms.FormControl(
forms.FormControlProps{
ClassNames: htmx.ClassNames{},
},
designs.Editor(
designs.EditorProps{
Content: l.Template.Body,
},
),
forms.FormControlLabel(
forms.FormControlLabelProps{},
forms.FormControlLabelText(
forms.FormControlLabelTextProps{
ClassNames: htmx.ClassNames{
"text-neutral-500": true,
},
},
htmx.Text("Supports Markdown."),
),
),
),
),
),
buttons.Button(
buttons.ButtonProps{
Type: "submit",
},
htmx.HxSwap("outerHTML"),
htmx.HxPut(fmt.Sprintf(utils.EditTemplateBodyUrlFormat, l.Template.ID)),
htmx.HxInclude("body"),
htmx.Text("Update"),
),
),
)
}
Loading

0 comments on commit 8436f6d

Please sign in to comment.