Skip to content

Commit ebe7bca

Browse files
authored
Add redirect in noMatch handler (#170)
Co-authored-by: hfhbd <[email protected]>
1 parent c1fe888 commit ebe7bca

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

integrationTest/src/jsMain/kotlin/demo.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ fun RouteBuilder.Routing() {
9797
}
9898
redirect("redirect", target = "users", hide = true)
9999
if (enableAnswer) {
100-
route("answer") {
101-
Br()
102-
Text("The Answer to the Ultimate Question of Life, the Universe, and Everything is 42.")
103-
}
100+
answer()
104101
}
105102
noMatch {
106103
P {
@@ -145,6 +142,20 @@ fun RouteBuilder.Routing() {
145142
}
146143
}
147144

145+
@Routing
146+
@Composable
147+
fun RouteBuilder.answer() {
148+
route("answer") {
149+
int {
150+
Br()
151+
Text("The Answer to the Ultimate Question of Life, the Universe, and Everything is $it.")
152+
}
153+
noMatch {
154+
redirect("42")
155+
}
156+
}
157+
}
158+
148159
@Composable
149160
fun RedirectButton() {
150161
Text("You could also hide navigations and redirect path to other routes.")

src/commonMain/kotlin/app/softwork/routingcompose/RouteBuilder.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,17 @@ public class RouteBuilder internal constructor(private val basePath: String, pri
150150
}
151151
}
152152

153-
public class NoMatch(public val remainingPath: String, public val parameters: Parameters?)
153+
@Routing
154+
public class NoMatch(public val remainingPath: String, public val parameters: Parameters?) {
155+
@Routing
156+
@Composable
157+
public fun redirect(target: String, hide: Boolean = false) {
158+
val router = Router.current
159+
LaunchedEffect(Unit) {
160+
router.navigate(target, hide)
161+
}
162+
}
163+
}
154164
}
155165

156166
private class DelegatingRouter(val basePath: String, val router: Router) : Router by router {
@@ -159,13 +169,16 @@ private class DelegatingRouter(val basePath: String, val router: Router) : Route
159169
to.startsWith("/") -> {
160170
router.navigate(to, hide)
161171
}
172+
162173
basePath == "/" -> {
163174
router.navigate("/$to", hide)
164175
}
176+
165177
to.startsWith(".") -> {
166178
val newPath = router.currentPath.relative(to)
167179
router.navigate(newPath.path)
168180
}
181+
169182
else -> {
170183
router.navigate("$basePath/$to", hide)
171184
}

src/jsTest/kotlin/app/softwork/routingcompose/RedirectTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,28 @@ class RedirectTest {
3333
waitForChanges()
3434
assertEquals("foo", root.innerHTML, "Second")
3535
}
36+
37+
@Test
38+
fun redirectingNoMatchTest() = runTest {
39+
val router = MockRouter()
40+
composition {
41+
router("/bar") {
42+
route("foo") {
43+
Text("foo")
44+
}
45+
route("bar") {
46+
Text("bar")
47+
}
48+
noMatch {
49+
redirect("foo", hide = true)
50+
}
51+
}
52+
}
53+
assertEquals("bar", root.innerHTML)
54+
55+
router.navigate("/asdf")
56+
waitForChanges()
57+
waitForChanges()
58+
assertEquals("foo", root.innerHTML)
59+
}
3660
}

0 commit comments

Comments
 (0)