raquo / Waypoint

Efficient router for Laminar UI Library
MIT License
92 stars 11 forks source link

Immediate page redirection doesn't seem to work #13

Closed yzia2000 closed 1 year ago

yzia2000 commented 1 year ago

Hi. It would be nice if I could get some insight on this use case. I have a route that is pretty much a callback which I want to immediately redirect to another page after parsing some details from the callback.

However, even after router.replaceState takes effect and url location changes to the EmailPage, the EmailPage is not rendered and I still see "Redirecting".

def renderPages(page: Page) = page match {
  case EmailPage => renderEmailPage
  case LoginPage => div("Login Page")
  case LoginCallbackPage =>
    // some sequential processing
    router.replaceState(EmailPage)
    div("Redirecting")
}

May I know what would be the right way to go about this use case.

yzia2000 commented 1 year ago

If I change the above code to something like this:

def renderPages(page: Page) = page match {
  case EmailPage => renderEmailPage
  case LoginPage => div("Login Page")
  case LoginCallbackPage =>
    // some sequential processing
    div("Redirecting", onClick --> {_ => router.replaceState(EmailPage) } )
}

And click on the "Redirecting" div, the redirection works fine and the EmailPage is rendered. But of course, I cannot expect the user to click here.

yzia2000 commented 1 year ago
def renderPages(page: Page) = page match {
  case EmailPage => EventStream.fromValue(renderEmailPage)
  case LoginPage => EventStream.fromValue(div("Login Page"))
  case LoginCallbackPage =>
    EventStream
      .unit(true)
      .delay(500)
      .flatMap(_ =>
        router.replaceState(EmailPage)
        EventStream.empty
      )
}

val app: Div = div(
  child <-- router.currentPageSignal.flatMap(renderPages)
)

Apologies for the ugly code but this one works. Basically by adding a delay until the current page renders, and only after using history api, we can make the email page render. But I don't think this is a good idea

yzia2000 commented 1 year ago

Ok, I just noticed the renderOnDomContentLoaded function. When I use this function, it works just fine. I guess this solves the issue for me.