Created
May 31, 2018 03:39
-
-
Save dalaing/163e2f4439461efe111742a3dc69d82f to your computer and use it in GitHub Desktop.
Reflex routing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
module Main where | |
import Control.Monad (void) | |
import Data.Maybe (fromMaybe) | |
import Data.Text (Text) | |
import Reflex.Dom | |
import Reflex.Dom.Routing.Nested | |
import Reflex.Dom.Routing.Writer | |
main :: IO () | |
main = mainWidget $ el "div" $ do | |
runRouteWithPathInFragment $ fmap snd $ runRouteWriterT $ | |
withRoute $ \route -> case fromMaybe "" route of | |
"" -> do | |
relButton "A" "a" | |
relButton "B" "b" | |
pure never | |
"a" -> numberLayer | |
"b" -> numberLayer | |
_ -> do | |
tellRedirectLocally [] | |
pure never | |
numberLayer :: (MonadWidget t m, HasRoute t Text m, RouteWriter t Text m) | |
=> m (Event t ()) | |
numberLayer = | |
withRoute $ \route -> case fromMaybe "" route of | |
"" -> do | |
relButton "1" "1" | |
relButton "2" "2" | |
backButton "Back" | |
"1" -> | |
backButton "Back" | |
"2" -> | |
backButton "Back" | |
_ -> | |
tellRedirectLocally [] | |
relButton :: (MonadWidget t m, HasRoute t Text m, RouteWriter t Text m) | |
=> Text | |
-> Text | |
-> m () | |
relButton label ref = do | |
e <- button label | |
tellRouteRelativeAs [ref] e | |
backButton :: (MonadWidget t m, HasRoute t Text m, RouteWriter t Text m) | |
=> Text | |
-> m () | |
backButton label = do | |
e <- button label | |
parents <- parentRouteSegments | |
tellRoute $ init <$> current parents <@ e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment