aboutsummaryrefslogtreecommitdiff
path: root/utils/shortcuts/redirect.go
blob: 8b2bb1de9afe5000ed9363c2b2aa8320fa51328d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package shortcuts

import (
	"dove/utils/urls"

	"github.com/gofiber/fiber/v2"
)

func Redirect(context *fiber.Ctx, routeName string) error {
	fullPath, exists := urls.GetFullPath(routeName)
	if !exists {
		return fiber.ErrNotFound
	}

	if isHtmxRequest(context) {
		context.Set("HX-Redirect", fullPath)
		return context.SendStatus(fiber.StatusNoContent)
	}

	return context.Redirect(fullPath)
}

func RedirectToPath(context *fiber.Ctx, path string) error {
	if isHtmxRequest(context) {
		context.Set("HX-Redirect", path)
		return context.SendStatus(fiber.StatusNoContent)
	}

	return context.Redirect(path)
}

func RedirectWithStatus(context *fiber.Ctx, routeName string, statusCode int) error {
	fullPath, exists := urls.GetFullPath(routeName)
	if !exists {
		return fiber.ErrNotFound
	}

	if isHtmxRequest(context) {
		context.Set("HX-Redirect", fullPath)
		return context.SendStatus(fiber.StatusNoContent)
	}

	return context.Redirect(fullPath, statusCode)
}