Learn HalogenGHC loads in the background

6. Routing

Routing gives each view its own URL. haskell-halogen-router uses three parts: a route type, a codec that converts between routes and URLs, and a router that tracks navigation.

Routes and their codec

Route has one constructor per view and derives Generic. Its codec, route, both parses URLs into Route values and prints routes as URLs.

  • RD.sum takes a row-types record with a codec for each constructor: (#Home .== RD.noArgs) .+ (#About .== RD.path "about" RD.noArgs). From Data.Row, .== creates a field and .+ combines records. Labels such as #Home require OverloadedLabels.
  • RD.noArgs handles constructors without arguments. RD.path "about" adds the /about segment.
  • RD.root requires a leading /.

Both Halogen.Router.Duplex and Halogen.Router.Duplex.Generic are imported as RD. They can share an alias because the names used here do not clash.

The router

The component receives a Router.Router Route as input and keeps it in state. initialState reads the current route with Router.current. On initialization, watchRoutes subscribes to Router.changes, producing an Arrived action for each new route.

Router.link builds an anchor with the route's URL and converts a plain click into Go. Router.follow updates the address without loading another page. The resulting Router.changes event updates the component.

The lesson wrapper creates the router with Router.newHashRouter Home route. It uses the fragment, such as #/about, because the tutorial already uses the path /06-router/ for its own routing. Cleanup calls Router.stop before the next run.

Exercise

Add numbered posts at /post/1, /post/2 and so on:

  1. Add Post Int to Route.
  2. Add .+ (#Post .== RD.path "post" (RD.int RD.segment)) to the codec record. RD.segment reads a segment and RD.int converts it to Int. GHC reports constructors missing from the codec.
  3. Add links with link (Post 1) "Post 1" and link (Post 2) "Post 2".

Compile and follow the links. The address ends in #/post/1 or #/post/2. Back returns to the previous lesson route without leaving this tutorial page.

Lesson.hs
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}

module Lesson (Route (..), route, component) where

import Control.Monad.IO.Class (MonadIO)
import Control.Monad.State (gets, modify)
import Data.Row ((.+), (.==))
import Data.Text qualified as T
import GHC.Generics (Generic)
import Halogen qualified as H
import Halogen.HTML qualified as HH
import Halogen.Router qualified as Router
import Halogen.Router.Duplex qualified as RD
import Halogen.Router.Duplex.Generic qualified as RD

data Route = Home | About
  deriving (Eq, Show, Generic)

route :: RD.RouteDuplex' Route
route =
  RD.root
    $ RD.sum
    $ (#Home .== RD.noArgs)
      .+ (#About .== RD.path "about" RD.noArgs)

data State = State {router :: Router.Router Route, current :: Route}

data Action = Arrived Route | Go (Router.Follow Route)

-- The program around this module makes the router, with
-- Router.newHashRouter Home route, hands it to the component as its input,
-- and calls Router.stop on it before the next compile runs.
component :: forall q o m. (MonadIO m) => H.Component q (Router.Router Route) o m
component =
  H.component initialState render [H.onInitialize watchRoutes, H.onAction handleAction]
  where
    initialState router = State router <$> Router.current router
    render st =
      HH.div_
        [ HH.nav_
            [ link Home "Home"
            , HH.text " | "
            , link About "About"
            ]
        , HH.p_ [HH.text ("You are at " <> T.pack (show st.current))]
        ]
      where
        link r label = Router.link st.router Go r [] [HH.text label]
    watchRoutes = do
      router <- gets (.router)
      _ <- H.subscribe (Arrived <$> Router.changes router)
      pure ()
    handleAction action = case action of
      Arrived r -> modify (\st -> st {current = r})
      Go follow -> do
        router <- gets (.router)
        Router.follow router follow

The compiler loads once the page has started.

Your component will appear here after compilation.