Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Main where
- import Prelude
- import Data.Lens
- import Data.List as L
- import Data.Maybe
- import Data.Tuple
- import Data.Either
- import Control.Monad.Aff (Aff)
- import Control.Monad.Trans.Class (lift)
- import React as R
- import React.DOM as R
- import React.DOM.Props as RP
- import Thermite hiding (defaultMain) as T
- import Thermite.Try as T
- ---- COUNTER COMPONENT ----
- -- STATE --
- type CounterState = Int
- counterInitialState :: CounterState
- counterInitialState = 42
- -- ACTIONS --
- data CounterAction
- = Increment
- | Decrement
- -- STATE MACHINE --
- counterUpdate :: T.PerformAction _ CounterState _ CounterAction
- counterUpdate Increment _ _ = void do
- T.modifyState \state -> state + 1
- counterUpdate Decrement _ _ = void do
- T.modifyState \state -> state - 1
- -- SPEC --
- counterSpec :: T.Spec _ CounterState _ CounterAction
- counterSpec = T.simpleSpec counterUpdate render
- where
- -- VIEW --
- render :: T.Render CounterState _ CounterAction
- render dispatch _ state _ =
- [ R.p
- [ RP.className "input-group" ]
- [ R.span
- [ RP.className "input-group-btn" ]
- [ R.button
- [ RP.className "btn btn-default"
- , RP.onClick \_ -> dispatch Decrement
- ]
- [ R.text "-" ]
- ]
- , R.input
- [ RP.className "form-control"
- , RP._type "text"
- , RP.disabled true
- , RP.value (show state)
- ]
- []
- , R.span
- [ RP.className "input-group-btn" ]
- [ R.button
- [ RP.className "btn btn-default"
- , RP.onClick \_ -> dispatch Increment
- ]
- [ R.text "+" ]
- ]
- ]
- ]
- ---- HEADER COMPONENT ----
- -- STATE --
- type HeaderState = L.List CounterState
- headerInitialState :: HeaderState
- headerInitialState = L.Nil
- -- ACTIONS --
- type CounterIndex = Int
- data HeaderAction
- = AddCounter
- | ItemAction CounterIndex CounterAction
- _ItemAction :: Prism' HeaderAction (Tuple CounterIndex CounterAction)
- _ItemAction = prism' (uncurry ItemAction) unwrap
- where
- unwrap (ItemAction i a) = Just (Tuple i a)
- unwrap _ = Nothing
- -- STATE MACHINE --
- headerUpdate :: T.PerformAction _ HeaderState _ HeaderAction
- headerUpdate AddCounter _ _ = void $ T.cotransform $ flip L.snoc counterInitialState
- headerUpdate _ _ _ = pure unit
- -- SPEC --
- headerSpec :: T.Spec _ _ _ _
- headerSpec = T.simpleSpec headerUpdate render
- where
- -- VIEW --
- render :: T.Render HeaderState _ HeaderAction
- render dispatch _ _ _ =
- [ R.h1'
- [ R.text "Lesson 5 - Lists" ]
- , R.p'
- [ R.button
- [ RP.className "btn btn-success"
- , RP.onClick \_ -> dispatch AddCounter
- ]
- [ R.text "Add Counter" ]
- ]
- ]
- ---- ROOT COMPONENT ----
- -- SPEC --
- spec :: T.Spec _ HeaderState _ HeaderAction
- spec
- = headerSpec
- <> T.focus id _ItemAction (T.foreach \_ -> counterSpec)
- ---- MAIN ----
- main = T.defaultMain spec headerInitialState
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement