Last active
August 26, 2020 15:35
-
-
Save abradley2/50462d3f9b2e6c02bf41bcead06d576d to your computer and use it in GitHub Desktop.
Being silly with Shpadoinkle
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
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Shpadoinkle as S | |
import Shpadoinkle.Backend.ParDiff | |
import Shpadoinkle.Html as H | |
import Data.Text | |
data Model = Model | |
{ currentCount :: Int | |
, inputText :: String | |
, congrats :: Bool | |
} deriving (Eq, Show) | |
initialModel :: Model | |
initialModel = Model 0 "Hello There" False | |
handleCounterExtMsg :: Maybe ExtMsg -> Model -> Model | |
handleCounterExtMsg extMsg model = | |
case extMsg of | |
Just OverTen -> model { congrats = True } | |
Just TenOrUnder -> model { congrats = False } | |
_ -> model | |
data ExtMsg | |
= OverTen | |
| TenOrUnder | |
counter :: Int -> Html (Int, Maybe ExtMsg) | |
counter model = | |
H.div | |
[] | |
[ H.button | |
[ onClick | |
$ (\nextModel -> (nextModel, if nextModel == 10 then Just TenOrUnder else Nothing)) | |
$ model - 1 | |
] | |
[ S.text "--" | |
] | |
, H.button | |
[ onClick | |
$ (\nextModel -> (nextModel, if nextModel == 11 then Just OverTen else Nothing)) | |
$ model + 1 | |
] | |
[ S.text "++" | |
] | |
, H.h3 | |
[] | |
[ S.text $ pack . show $ model | |
] | |
] | |
view :: Model -> Html Model | |
view model = | |
H.div | |
[] | |
[ H.input | |
[ type' "text" | |
, H.onInput $ (\t -> model { inputText = show t }) | |
] | |
[] | |
, H.h3 [] [ S.text $ pack . inputText $ model ] | |
, (\(c,extMsg) -> handleCounterExtMsg extMsg $ model { currentCount = c } ) | |
<$> counter (currentCount model) | |
, if congrats model then | |
H.h3 | |
[] | |
[ S.text "You're good at counting!" ] | |
else | |
H.p | |
[] | |
[ S.text "Not there yet"] | |
] | |
main :: IO () | |
main = runJSorWarp 8080 $ | |
simple runParDiff initialModel (constly' . view) getBody |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment