package myapp
import org.scalajs.dom
import com.raquo.laminar.api.L.{*, given}
import com.raquo.waypoint.*
import io.bullet.borer.*
import io.bullet.borer.derivation.MapBasedCodecs.*
import java.nio.charset.StandardCharsets
import io.bullet.borer.Json.DecodingConfig
import myapp.utils.JsonUtils.*
object pages {
sealed trait Page(val title: String)
case object HomePage extends Page("Home")
case object AboutPage extends Page("About")
case object NotFoundPage extends Page("Page Not Found")
given pageCodec: Codec[Page] = deriveAllCodecs
}
val routes = List(
Route.static(pages.HomePage, root / endOfSegments),
Route.static(pages.AboutPage, root / "about" / endOfSegments)
)
object router
extends Router[pages.Page](
routes = routes,
getPageTitle = _.title,
serializePage = page => Json.encode(page).toUtf8String,
deserializePage = pageStr =>
Json.decodeString(pageStr).to[pages.Page].value,
routeFallback = _ => pages.NotFoundPage
)(
popStateEvents = windowEvents(_.onPopState),
owner = unsafeWindowOwner
) {}
def renderPage(page: pages.Page): Div = {
page match {
case pages.HomePage => div("home")
case pages.AboutPage => div("about")
case _ => div("Page not found")
}
}
val app = div(child <-- router.currentPageSignal.map(renderPage))
@main
def MyApp(): Unit =
lazy val root = dom.document.querySelector("#app")
renderOnDomContentLoaded(root, app)