You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.9 KiB

4 years ago
  1. package com.example.playscalajsreact.route
  2. import org.scalajs.dom
  3. import japgolly.scalajs.react._
  4. import japgolly.scalajs.react.vdom.html_<^._
  5. import japgolly.scalajs.react.extra._
  6. import japgolly.scalajs.react.extra.router._
  7. import com.example.playscalajsreact.component.HelloWorldSJSRComponent
  8. import com.example.playscalajsreact.component.MenuComponent
  9. import com.example.playscalajsreact.component.Top
  10. import com.example.playscalajsreact.component.Top2
  11. import com.example.playscalajsreact.model.MyGlobalState
  12. import com.example.playscalajsreact.component.Middle2
  13. object AppRouter {
  14. import com.example.playscalajsreact.route.Page._
  15. final case class Props(state: StateSnapshot[MyGlobalState])
  16. private def layout(c: RouterCtl[Page], r: ResolutionWithProps[Page, Props])(
  17. appState: Props
  18. ) =
  19. <.div(
  20. MenuComponent(appState.state, c),
  21. r.renderP(appState)
  22. )
  23. val x = <.ol(
  24. ^.id := "my-list",
  25. ^.lang := "en",
  26. ^.margin := 8.px,
  27. <.li("Item 1"),
  28. <.li("Item 2")
  29. // HelloWorldSJSRComponent("Hello", 18)
  30. )
  31. val routerConfig = RouterWithPropsConfigDsl[Page, Props].buildConfig { dsl =>
  32. import dsl._
  33. (emptyRule
  34. | staticRoute(root, Home) ~> render(x)
  35. | dynamicRouteCT(
  36. ("#user" / string("[a-zA-Z0-9]{1,20}") / "age" / int)
  37. .caseClass[Person]
  38. ) ~> dynRender((page: Person) => {
  39. HelloWorldSJSRComponent(page.user, page.age)
  40. })
  41. | staticRoute("#hello", Hello) ~> render(<.div("TODO"))
  42. | staticRoute("#editor", Editor) ~> render(Top.Comp())
  43. | staticRoute("#test", Test) ~> renderP(p => Middle2.Props("Aege", p.state).render)
  44. | staticRedirect("#hey") ~> redirectToPage(Hello)(
  45. SetRouteVia.HistoryReplace
  46. ))
  47. .notFound(redirectToPage(Home)(SetRouteVia.HistoryReplace))
  48. .renderWithP(layout)
  49. }
  50. val router =
  51. RouterWithProps(BaseUrl("http://localhost:8080/"), AppRouter.routerConfig)
  52. }