Skip to content

Commit 2f5e395

Browse files
committed
add hx-location example
1 parent 3bbdf2b commit 2f5e395

File tree

5 files changed

+91
-13
lines changed

5 files changed

+91
-13
lines changed

Examples/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ This project provides examples of `swift-htmx` usage
66

77
Start the server by running `swift run App` in the project root
88

9-
- (swap-http)[Sources/App/HtmxExamples/SwapHandlers.swift] - Swap an element via `HX-Retarget` and `HX-Reswap` headers (using "`HTTPTypesHtmx` for `apple/swift-http-types`)
9+
- (HX-Location)[Sources/App/HtmxExamples/HXLocationHandlers.swift] - Do a client side redirect via `HX-Location`
10+
- (HX-Reswap)[Sources/App/HtmxExamples/HXReswapHandlers.swift] - Swap an element via `HX-Reswap`

Examples/Sources/App/Application+build.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func buildRouter() -> Router<AppRequestContext> {
5151
router.get("/") { _, _ -> Response in
5252
return .html(htmlLayout("<p>Select an example</p>"))
5353
}
54-
registerSwapRoutes(router: router)
54+
router.on(route: .hxLocation, use: hxLocationHandler)
55+
router.on(route: .hxLocationSubmit, use: hxLocationSubmitHandler)
56+
router.on(route: .hxLocationB, use: hxLocationBHandler)
57+
router.on(route: .hxLocationBSubmit, use: hxLocationBSubmitHandler)
58+
router.on(route: .hxReswapHttp, use: hxReswapHandler)
5559
return router
5660
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import Foundation
2+
import HTTPTypesHtmx
3+
import Hummingbird
4+
5+
#if canImport(FoundationNetworking)
6+
import FoundationNetworking
7+
#endif
8+
9+
extension Route {
10+
static let hxLocation = Route(.get, "/examples/hx-location")
11+
static let hxLocationSubmit = Route(.post, "/examples/hx-location")
12+
static let hxLocationB = Route(.get, "/examples/hx-location-b")
13+
static let hxLocationBSubmit = Route(.post, "/examples/hx-location-b")
14+
}
15+
16+
@Sendable func hxLocationHandler(_ req: Request) -> Response {
17+
.html(
18+
htmlLayout(
19+
"""
20+
<div>
21+
<h2>HX-Location Example</h2>
22+
<p>Pressing the button will do client-side redirection via the HX-Location header</p>
23+
<div>
24+
<button
25+
hx-post="\(Route.hxLocationSubmit.path)"
26+
>Submit</button>
27+
</div>
28+
</div>
29+
"""
30+
)
31+
)
32+
}
33+
34+
@Sendable func hxLocationSubmitHandler(_ req: Request) -> Response {
35+
if req.isHtmxRequest {
36+
return Response(
37+
status: .ok,
38+
headers: HTTPFields([
39+
.hxLocation(Route.hxLocationB.path)
40+
])
41+
)
42+
}
43+
return Response(status: .badRequest)
44+
}
45+
46+
@Sendable func hxLocationBHandler(_ req: Request) -> Response {
47+
.html(
48+
htmlLayout(
49+
"""
50+
<div>
51+
<h2>Redirected!</h2>
52+
<p>You got redirected here via HX-Location header, click the button to be redirected back.</p>
53+
<div>
54+
<button
55+
hx-post="\(Route.hxLocationBSubmit.path)"
56+
>Submit</button>
57+
</div>
58+
</div>
59+
"""
60+
)
61+
)
62+
}
63+
64+
@Sendable func hxLocationBSubmitHandler(_ req: Request) -> Response {
65+
if req.isHtmxRequest {
66+
return Response(
67+
status: .ok,
68+
headers: HTTPFields([
69+
.hxLocation(Route.hxLocation.path)
70+
])
71+
)
72+
}
73+
return Response(status: .badRequest)
74+
}

Examples/Sources/App/HtmxExamples/SwapHandlers.swift Examples/Sources/App/HtmxExamples/HXReswapHandlers.swift

+5-9
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ import Hummingbird
77
#endif
88

99
extension Route {
10-
static let swapHttp = Route(.get, "/examples/swap-http")
10+
static let hxReswapHttp = Route(.get, "/examples/hx-reswap")
1111
}
1212

13-
func registerSwapRoutes(router: Router<AppRequestContext>) {
14-
router.on(route: .swapHttp, use: swapHttpHandler)
15-
}
16-
17-
@Sendable func swapHttpHandler(_ req: Request) -> Response {
13+
@Sendable func hxReswapHandler(_ req: Request) -> Response {
1814
let timestamp = String(Date.now.timeIntervalSince1970)
1915
let inputId = "timestamp"
2016
let inputSelector = "#timestamp"
@@ -36,13 +32,13 @@ func registerSwapRoutes(router: Router<AppRequestContext>) {
3632
htmlLayout(
3733
"""
3834
<div>
39-
<h2>Swap via HX-Reswap and HX-Retarget headers</h2>
35+
<h2>HX-Reswap Example</h2>
4036
<p>This example uses HX-Reswap and HX-Retarget headers to update the input with the server's current timestamp</p>
4137
<div>
4238
<button
43-
hx-get="\(Route.swapHttp.path)"
39+
hx-get="\(Route.hxReswapHttp.path)"
4440
hx-swap="none"
45-
>Swap via HX-Reswap and Hx-Retarget</button>
41+
>Swap via HX-Reswap and HX-Retarget</button>
4642
<br>
4743
<br>
4844
\(input)

Examples/Sources/App/HtmxExamples/Support/HtmlLayout.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ func htmlLayout(_ content: String) -> String {
1212
</head>
1313
<body>
1414
<header class="container">
15-
<nav style="display: flex;align-items: center;height: 64px;gap: 16px;">
16-
<a href="\(Route.swapHttp.path)">swap via http headers</a>
15+
<nav>
16+
<ul>
17+
<li><a href="\(Route.hxLocation.path)">HX-Location</a></li>
18+
<li><a href="\(Route.hxReswapHttp.path)">HX-Reswap</a></li>
19+
</ul>
1720
</nav>
1821
</header>
1922
<main class="container">

0 commit comments

Comments
 (0)