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, build-depends: aeson,
base < 5, base < 5,
ghcjs-base, ghcjs-base,
miso miso,
servant
-- Directories containing source files. -- Directories containing source files.
hs-source-dirs: src 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 OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeOperators #-}
module Main where module Main where
@ -14,17 +16,35 @@ import Miso
, noEff , noEff
, defaultEvents , defaultEvents
, LogLevel (Off) , LogLevel (Off)
, URI
, runRoute
, getCurrentURI
) )
import Data.Proxy
import Servant.API
import Action
import Routes
import qualified Component.CatalogGrid as Grid import qualified Component.CatalogGrid as Grid
data Model = Model data Model = Model
{ gridModel :: Grid.Model { gridModel :: Grid.Model
} deriving Eq } deriving Eq
data Action initialActionFromRoute :: Model -> URI -> Action
= GridAction Grid.Action initialActionFromRoute model uri = either (const NoAction) id routing_result
| NoAction 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
initialModel = Model initialModel = Model
@ -32,13 +52,16 @@ initialModel = Model
} }
main :: IO () main :: IO ()
main = startApp App main = do
uri <- getCurrentURI
startApp App
{ model = initialModel { model = initialModel
, update = mainUpdate , update = mainUpdate
, view = mainView , view = mainView
, subs = [] , subs = []
, events = defaultEvents , events = defaultEvents
, initialAction = NoAction , initialAction = initialActionFromRoute initialModel uri
, mountPoint = Nothing , mountPoint = Nothing
, logLevel = Off , logLevel = Off
} }
@ -64,4 +87,14 @@ iGrid = Grid.Interface
- - Create Hello World page render - - Create Hello World page render
- - Create CatalogGrid component (static at first) - - Create CatalogGrid component (static at first)
- - Get postgrest url from page header and perform an initial xhr request - - 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