Created
November 29, 2021 21:42
-
-
Save flip111/2fc6028c632efc04925bdbfd2b666836 to your computer and use it in GitHub Desktop.
trying to figure out type signature of render and handleAction to work with halogen store
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
module Main where | |
import Prelude | |
import Effect (Effect) | |
import Effect.Aff (launchAff_) | |
import Halogen.Aff as HA | |
import Halogen.Store.Monad (runStoreT) | |
import Halogen.VDom.Driver (runUI) | |
import Data.Maybe (Maybe(..)) | |
import Halogen as H | |
import Halogen.HTML as HH | |
import Halogen.HTML.Events as HE | |
import Halogen.Store.Connect (Connected, connect) | |
import Halogen.Store.Monad as Store | |
import Halogen.Store.Monad (runStoreT, class MonadStore, StoreT) | |
import Halogen.Store.Select (Selector, selectEq) | |
import Effect.Aff (Aff) | |
import Data.Const (Const) | |
main :: Effect Unit | |
main = launchAff_ do | |
body <- HA.awaitBody | |
root <- runStoreT initialStore reduce component | |
runUI root unit body | |
type Store = { count :: Int } | |
initialStore :: Store | |
initialStore = { count: 0 } | |
data StoreAction | |
= StoreIncrement | |
| StoreDecrement | |
reduce :: Store -> StoreAction -> Store | |
reduce store = case _ of | |
StoreIncrement -> store { count = store.count + 1 } | |
StoreDecrement -> store { count = store.count - 1 } | |
type Input = Unit | |
type Context = Int | |
type State = Int | |
data Action | |
= Increment | |
| Decrement | |
| Receive (Connected Context Input) | |
deriveState :: Connected Context Input -> State | |
deriveState { context } = context | |
selectCount :: Selector Store Context | |
selectCount = selectEq _.count | |
component :: H.Component (Const Unit) Input Void (StoreT StoreAction Store Aff) | |
component = connect selectCount $ H.mkComponent | |
{ initialState: deriveState | |
, render | |
, eval: H.mkEval $ H.defaultEval | |
{ handleAction = handleAction | |
, receive = Just <<< Receive | |
} | |
} | |
-- Trying to figure out what this type signature should be | |
render :: _ | |
render count = | |
HH.div_ | |
[ HH.button | |
[ HE.onClick \_ -> Increment ] | |
[ HH.text "Increment" ] | |
, HH.text $ " Count: " <> show count <> " " | |
, HH.button | |
[ HE.onClick \_ -> Decrement ] | |
[ HH.text "Decrement" ] | |
] | |
-- Trying to figure out what this type signature should be | |
handleAction :: _ | |
handleAction = case _ of | |
Increment -> | |
Store.updateStore StoreIncrement | |
Decrement -> | |
Store.updateStore StoreDecrement | |
Receive input -> | |
H.put $ deriveState input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment