2023-09-11 23:26:50 +02:00
|
|
|
package utils
|
2023-09-10 23:40:19 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-09-11 23:26:50 +02:00
|
|
|
"git.myrkvi.com/myrkvi/bin/views/components"
|
2023-09-10 23:40:19 +02:00
|
|
|
"github.com/a-h/templ"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/teris-io/shortid"
|
|
|
|
)
|
|
|
|
|
2023-09-11 23:26:50 +02:00
|
|
|
// 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 {
|
2023-09-10 23:40:19 +02:00
|
|
|
w := c.Response().Writer
|
|
|
|
c.Response().Status = status
|
|
|
|
|
2023-09-11 23:26:50 +02:00
|
|
|
combine := components.CombineTempls(comps...)
|
|
|
|
|
|
|
|
return combine.Render(context.Background(), w)
|
2023-09-10 23:40:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RenderErrorToast(c echo.Context, message string) error {
|
|
|
|
SetHeader(c, "HX-Retarget", "#toast")
|
|
|
|
SetHeader(c, "HX-Reswap", "beforeend")
|
|
|
|
|
2023-09-11 23:26:50 +02:00
|
|
|
return RenderComponents(c, 200, components.ToastError(message))
|
2023-09-10 23:40:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-09-11 23:26:50 +02:00
|
|
|
func GenerateCodes() (code string, adminCode string, err error) {
|
2023-09-10 23:40:19 +02:00
|
|
|
code, err = shortid.Generate()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
adminCode, err = shortid.Generate()
|
|
|
|
return
|
|
|
|
}
|