From fcea3f87fe8764d15b78c148c11a86f7a1538af4 Mon Sep 17 00:00:00 2001 From: towards-a-new-leftypol Date: Mon, 25 Dec 2023 08:21:56 -0500 Subject: [PATCH] Figure out how to use the router (initial action) --- chandlr.cabal | 3 ++- src/Action.hs | 13 ++++++++++++ src/Main.hs | 59 +++++++++++++++++++++++++++++++++++++++------------ src/Routes.hs | 27 +++++++++++++++++++++++ 4 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 src/Action.hs create mode 100644 src/Routes.hs diff --git a/chandlr.cabal b/chandlr.cabal index c82332d..369dbc4 100644 --- a/chandlr.cabal +++ b/chandlr.cabal @@ -71,7 +71,8 @@ executable chandlr build-depends: aeson, base < 5, ghcjs-base, - miso + miso, + servant -- Directories containing source files. hs-source-dirs: src diff --git a/src/Action.hs b/src/Action.hs new file mode 100644 index 0000000..087c8ac --- /dev/null +++ b/src/Action.hs @@ -0,0 +1,13 @@ +module Action where + +import Component.CatalogGrid as Grid + +data Action + = GridAction Grid.Action + | GetLatest + | GetThread + { website :: String + , board :: String + , board_thread_id :: Int + } + | NoAction diff --git a/src/Main.hs b/src/Main.hs index 146ae15..1947c46 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,4 +1,6 @@ {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE TypeOperators #-} module Main where @@ -14,17 +16,35 @@ import Miso , noEff , defaultEvents , LogLevel (Off) + , URI + , runRoute + , getCurrentURI ) +import Data.Proxy +import Servant.API + +import Action +import Routes + import qualified Component.CatalogGrid as Grid data Model = Model { gridModel :: Grid.Model } deriving Eq -data Action - = GridAction Grid.Action - | NoAction +initialActionFromRoute :: Model -> URI -> Action +initialActionFromRoute model uri = either (const NoAction) id routing_result + where + routing_result = runRoute (Proxy :: Proxy Route) handlers (const uri) model + + handlers = h_latest :<|> h_thread + + h_latest :: Model -> Action + h_latest = const GetLatest + + h_thread :: String -> String -> BoardThreadId -> Model -> Action + h_thread board website board_thread_id _ = GetThread {..} initialModel :: Model initialModel = Model @@ -32,16 +52,19 @@ initialModel = Model } main :: IO () -main = startApp App - { model = initialModel - , update = mainUpdate - , view = mainView - , subs = [] - , events = defaultEvents - , initialAction = NoAction - , mountPoint = Nothing - , logLevel = Off - } +main = do + uri <- getCurrentURI + + startApp App + { model = initialModel + , update = mainUpdate + , view = mainView + , subs = [] + , events = defaultEvents + , initialAction = initialActionFromRoute initialModel uri + , mountPoint = Nothing + , logLevel = Off + } mainView :: Model -> View Action mainView model = @@ -64,4 +87,14 @@ iGrid = Grid.Interface - - Create Hello World page render ✓ - - Create CatalogGrid component (static at first) ✓ - - Get postgrest url from page header and perform an initial xhr request + - + - - do I need to move out everything into another project called chandlr-common? + - - if I want to use the isomorphic feature of miso, then yes + - + - - add a router first + - - go to the next page and do xhr for the content + - - before we do xhr we need the postgrest url, + - - how do we tackle a config? + - - make it isomorphic + - - move everything before or during this part into common lib -} diff --git a/src/Routes.hs b/src/Routes.hs new file mode 100644 index 0000000..041f028 --- /dev/null +++ b/src/Routes.hs @@ -0,0 +1,27 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE TypeOperators #-} + +module Routes where + +import Miso (View) + +import Servant.API + +import Action + +type Route + = R_Latest + :<|> R_Thread + +type R_Latest = View Action + +-- Show selected thread +-- - //res/ +type R_Thread + = Capture "website" String + :> Capture "board" String + :> "res" + :> Capture "board_thread_id" BoardThreadId + :> View Action + +type BoardThreadId = Int