11module ComponentsHalogenHooks.Main where
22
3- import Prelude hiding ( top )
3+ import Prelude
44
55import Data.Maybe (Maybe (..), maybe )
6- import Data.Symbol (SProxy (..))
76import Data.Tuple.Nested ((/\))
87import Effect (Effect )
98import Halogen as H
@@ -20,53 +19,42 @@ main =
2019 body <- HA .awaitBody
2120 void $ runUI containerComponent unit body
2221
23- _button :: SProxy " button"
24- _button = SProxy
25-
2622containerComponent
2723 :: forall unusedQuery unusedInput unusedOutput anyMonad
2824 . H.Component HH.HTML unusedQuery unusedInput unusedOutput anyMonad
2925containerComponent = Hooks .component \rec _ -> Hooks .do
26+ enabled /\ enabledIdx <- Hooks .useState false
3027 toggleCount /\ toggleCountIdx <- Hooks .useState 0
3128 buttonState /\ buttonStateIdx <- Hooks .useState (Nothing :: Maybe Boolean )
29+ let
30+ handleClick _ =
31+ Just do
32+ Hooks .modify_ toggleCountIdx (_ + 1 )
33+ Hooks .modify_ enabledIdx not
34+ label = if enabled then " On" else " Off"
3235 Hooks .pure $
3336 HH .div_
34- [ HH .slot _button unit buttonComponent unit \_ -> Just do
35- Hooks .modify_ toggleCountIdx (_ + 1 )
37+ [ renderButton {enabled, handleClick}
3638 , HH .p_
37- [ HH .text (" Button has been toggled " <> show toggleCount <> " time(s)" ) ]
39+ [ HH .text (" the Button has been toggled " <> show toggleCount <> " time(s)" ) ]
3840 , HH .p_
3941 [ HH .text
4042 $ " Last time I checked, the button was: "
4143 <> (maybe " (not checked yet)" (if _ then " on" else " off" ) buttonState)
4244 <> " . "
4345 , HH .button
4446 [ HE .onClick \_ -> Just do
45- mbBtnState <- Hooks .query rec.slotToken _button unit $ H .request IsOn
46- Hooks .put buttonStateIdx mbBtnState
47+ Hooks .put buttonStateIdx $ Just enabled
4748 ]
4849 [ HH .text " Check now" ]
4950 ]
5051 ]
5152
52- data ButtonMessage = Toggled Boolean
53- data ButtonQuery a = IsOn (Boolean -> a )
54-
55- buttonComponent
56- :: forall unusedInput anyMonad
57- . H.Component HH.HTML ButtonQuery unusedInput ButtonMessage anyMonad
58- buttonComponent = Hooks .component \rec _ -> Hooks .do
59- enabled /\ enabledIdx <- Hooks .useState false
60- Hooks .useQuery rec.queryToken case _ of
61- IsOn reply -> do
62- isEnabled <- Hooks .get enabledIdx
63- pure $ Just $ reply isEnabled
53+ renderButton {enabled, handleClick} =
6454 let label = if enabled then " On" else " Off"
65- Hooks .pure $
55+ in
6656 HH .button
6757 [ HP .title label
68- , HE .onClick \_ -> Just do
69- newState <- Hooks .modify enabledIdx not
70- Hooks .raise rec.outputToken $ Toggled newState
58+ , HE .onClick handleClick
7159 ]
7260 [ HH .text label ]
0 commit comments