bin/utils/util.go

56 lines
1.3 KiB
Go

package utils
import (
"context"
"git.myrkvi.com/myrkvi/bin/views/components"
"github.com/a-h/templ"
"github.com/labstack/echo/v4"
"github.com/teris-io/shortid"
)
// func RenderComponent(c echo.Context, status int, component templ.Component) error {
// w := c.Response().Writer
// c.Response().Status = status
// return component.Render(context.Background(), w)
// }
func RenderComponents(c echo.Context, status int, comps ...templ.Component) error {
w := c.Response().Writer
c.Response().Status = status
combine := components.CombineTempls(comps...)
return combine.Render(context.Background(), w)
}
func RenderErrorToast(c echo.Context, message string) error {
SetHeader(c, "HX-Retarget", "#toast")
SetHeader(c, "HX-Reswap", "beforeend")
return RenderComponents(c, 200, components.ToastError(message))
}
func AddHeader(c echo.Context, key, value string) {
c.Response().Header().Add(key, value)
}
func SetHeader(c echo.Context, key, value string) {
c.Response().Header().Set(key, value)
}
func SetContentType(c echo.Context, value string) {
SetHeader(c, "Content-Type", value)
}
func GenerateCodes() (code string, adminCode string, err error) {
code, err = shortid.Generate()
if err != nil {
return
}
adminCode, err = shortid.Generate()
return
}