Figure out how to use the router (initial action)

This commit is contained in:
towards-a-new-leftypol 2023-12-25 08:21:56 -05:00
parent d8f3637839
commit fcea3f87fe
4 changed files with 88 additions and 14 deletions

View File

@ -71,7 +71,8 @@ executable chandlr
build-depends: aeson,
base < 5,
ghcjs-base,
miso
miso,
servant
-- Directories containing source files.
hs-source-dirs: src

13
src/Action.hs Normal file
View File

@ -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

View File

@ -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
-}

27
src/Routes.hs Normal file
View File

@ -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
-- - <website>/<board>/res/<thread_id>
type R_Thread
= Capture "website" String
:> Capture "board" String
:> "res"
:> Capture "board_thread_id" BoardThreadId
:> View Action
type BoardThreadId = Int