Closed
Description
Context: https://gitter.im/japgolly/scalajs-react?at=5524ad269f817500153c209b
To produce a minimized example that shows a problem with interceptRender
and redirect, I tried the following code (the original project uses Flux and all that, so a bit complex to explain). But redirect isn't working for me. Is my use of redirect correct or does this show an issue with scalajs-react?
I expected that when I click on the Login button, it should show the private page content and the url to be /private
. But I don't see any change.
package example
import japgolly.scalajs.react.extra.router.Redirect.Replace
import japgolly.scalajs.react.extra.router.{BaseUrl, Redirect, RoutingRules}
import scala.scalajs.js
import js.annotation.JSExport
import org.scalajs.dom
import scala.scalajs.js
import japgolly.scalajs.react._
import vdom.all._
@JSExport("Main")
object ScalaJSReactExample extends js.JSApp {
def main(): Unit = {
val baseUrl = BaseUrl.fromWindowOrigin + "/"
val router = MainRouter.router(baseUrl)
React.render(router(), dom.document.body)
}
}
object MainRouter extends RoutingRules {
val root = register(rootLocation(PublicPage.Component(())))
val pvt = register(location("private", PrivatePage.Component(())))
override protected val notFound = redirect(root, Redirect.Replace)
register(removeTrailingSlashes)
override protected def interceptRender(ic: InterceptionR): ReactElement = {
println(s"Intercepting")
if (AuthenticationStore.authenticated) {
div(
div(
button(onClick --> {println("Logging out"); AuthenticationStore.authenticated = false; redirect(root, Replace)})("Logout")
),
ic.element
)
} else {
div(
div(
button(onClick --> {println("Logging in"); AuthenticationStore.authenticated = true; redirect(pvt, Replace)})("Login")
),
ic.element
)
}
}
}
object AuthenticationStore {
var authenticated = false
}
object PublicPage {
val Component = ReactComponentB[Unit]("Public").render(_ => div("Public")).build
}
object PrivatePage {
val Component = ReactComponentB[Unit]("Private").render(_ => div("Private")).build
}
Metadata
Metadata
Assignees
Labels
No labels