Added redirect support with query params.

This commit is contained in:
mikestefanello 2024-06-16 10:53:43 -04:00
parent 71f7de8771
commit 6730b6a319
2 changed files with 39 additions and 5 deletions

View file

@ -3,6 +3,7 @@ package handlers
import (
"fmt"
"net/http"
"net/url"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/htmx"
@ -30,10 +31,19 @@ func GetHandlers() []Handler {
return handlers
}
// redirect redirects to a given route name with optional route parameters
func redirect(ctx echo.Context, route string, routeParams ...any) error {
url := ctx.Echo().Reverse(route, routeParams...)
// redirect redirects to a given route by name with optional route parameters
func redirect(ctx echo.Context, routeName string, routeParams ...any) error {
return doRedirect(ctx, ctx.Echo().Reverse(routeName, routeParams...))
}
// redirectWithQuery redirects to a given route by name with query parameters and optional route parameters
func redirectWithQuery(ctx echo.Context, query url.Values, routeName string, routeParams ...any) error {
dest := fmt.Sprintf("%s?%s", ctx.Echo().Reverse(routeName, routeParams...), query.Encode())
return doRedirect(ctx, dest)
}
// doRedirect performs a redirect to a given URL
func doRedirect(ctx echo.Context, url string) error {
if htmx.GetRequest(ctx).Boosted {
htmx.Response{
Redirect: url,