Advertisement
Guest User

reflex navigation

a guest
Apr 8th, 2021
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE DataKinds #-}
  2. {-# LANGUAGE OverloadedStrings #-}
  3. {-# LANGUAGE TypeApplications #-}
  4.  
  5. module Frontend where
  6.  
  7. import Control.Monad  
  8. import Control.Monad.Fix
  9. import qualified Data.Text as T
  10. import qualified Data.Text.Encoding as T
  11. import Language.Javascript.JSaddle (eval, liftJSM)
  12.  
  13. import Obelisk.Frontend
  14. import Obelisk.Configs
  15. import Obelisk.Route
  16. import Obelisk.Generated.Static
  17.  
  18. import Reflex.Dom.Core
  19.  
  20. import Common.Api
  21. import Common.Route
  22.  
  23. content :: (DomBuilder t m
  24.           , MonadHold t m
  25.           , MonadFix m
  26.           , PostBuild t m) =>
  27.     Event t (T.Text -> T.Text) -> m ()
  28. content navigation = do
  29.     contentText <- foldDyn ($) ("click on a button" :: T.Text) navigation
  30.     dynText contentText
  31.  
  32. navigation :: (DomBuilder t m) => Int -> m (Event t (T.Text -> T.Text))
  33. navigation nPages = do
  34.     n <- mapM mkNavBtn [1..nPages]
  35.     return $ leftmost n
  36.  
  37.     where
  38.         mkNavBtn i = do
  39.           evt <- button $ pageNLabel i
  40.           return $ addValue (pageNContentLabel i) evt
  41.         pageNLabel i = ("Page " :: T.Text) <> (T.pack $ show i)
  42.         pageNContentLabel i = T.pack $ "page " <> (show i) <> " content"
  43.  
  44. addValue :: Reflex t => a -> Event t () -> Event t (a -> a)
  45. addValue val evt = fmap (\_ _ -> val) evt
  46.  
  47. -- This runs in a monad that can be run on the client or the server.
  48. -- To run code in a pure client or pure server context, use one of the
  49. -- `prerender` functions.
  50. frontend :: Frontend (R FrontendRoute)
  51. frontend = Frontend
  52.   { _frontend_head = do
  53.       el "title" $ text "Test"
  54.       elAttr "link"
  55.         (  "href" =: static @"main.css"
  56.         <> "type" =: "text/css"
  57.         <> "rel" =: "stylesheet") blank
  58.   , _frontend_body = do
  59.  
  60.       n <- navigation 4
  61.       el "div" $ content n
  62.       return ()
  63.   }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement