File tree 5 files changed +91
-13
lines changed
5 files changed +91
-13
lines changed Original file line number Diff line number Diff line change @@ -6,4 +6,5 @@ This project provides examples of `swift-htmx` usage
6
6
7
7
Start the server by running ` swift run App ` in the project root
8
8
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 `
Original file line number Diff line number Diff line change @@ -51,6 +51,10 @@ func buildRouter() -> Router<AppRequestContext> {
51
51
router. get ( " / " ) { _, _ -> Response in
52
52
return . html( htmlLayout ( " <p>Select an example</p> " ) )
53
53
}
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)
55
59
return router
56
60
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -7,14 +7,10 @@ import Hummingbird
7
7
#endif
8
8
9
9
extension Route {
10
- static let swapHttp = Route ( . get, " /examples/swap-http " )
10
+ static let hxReswapHttp = Route ( . get, " /examples/hx-reswap " )
11
11
}
12
12
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 {
18
14
let timestamp = String ( Date . now. timeIntervalSince1970)
19
15
let inputId = " timestamp "
20
16
let inputSelector = " #timestamp "
@@ -36,13 +32,13 @@ func registerSwapRoutes(router: Router<AppRequestContext>) {
36
32
htmlLayout (
37
33
"""
38
34
<div>
39
- <h2>Swap via HX-Reswap and HX-Retarget headers </h2>
35
+ <h2>HX-Reswap Example </h2>
40
36
<p>This example uses HX-Reswap and HX-Retarget headers to update the input with the server's current timestamp</p>
41
37
<div>
42
38
<button
43
- hx-get= " \( Route . swapHttp . path) "
39
+ hx-get= " \( Route . hxReswapHttp . path) "
44
40
hx-swap= " none "
45
- >Swap via HX-Reswap and Hx -Retarget</button>
41
+ >Swap via HX-Reswap and HX -Retarget</button>
46
42
<br>
47
43
<br>
48
44
\( input)
Original file line number Diff line number Diff line change @@ -12,8 +12,11 @@ func htmlLayout(_ content: String) -> String {
12
12
</head>
13
13
<body>
14
14
<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>
17
20
</nav>
18
21
</header>
19
22
<main class= " container " >
You can’t perform that action at this time.
0 commit comments