-
-
Save Fresheyeball/aab8af5d6f243acf9ee1d661059232c0 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 GeneralizedNewtypeDeriving #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Data.Text | |
import Shpadoinkle as S | |
import Shpadoinkle.Backend.ParDiff | |
import Shpadoinkle.Html as H | |
newtype Count = Count { unCount :: Int } deriving (Eq, Ord, Num, Show) | |
newtype InputText = InputText { unInputText :: Text } deriving (Eq, Show) | |
data Model = Model | |
{ currentCount :: Count | |
, inputText :: InputText | |
} | |
deriving (Eq, Show) | |
initialModel :: Model | |
initialModel = Model (Count 0) (InputText "Hello There") | |
counter :: Count -> Html Count | |
counter model = | |
H.div | |
[] | |
[ H.button | |
[ onClick $ model - 1 | |
] | |
[ S.text "--" | |
] | |
, H.button | |
[ onClick $ model + 1 | |
] | |
[ S.text "++" | |
] | |
, H.h3 | |
[] | |
[ S.text $ pack . show . unCount $ model | |
] | |
] | |
view :: Model -> Html Model | |
view model = | |
H.div | |
[] | |
[ H.input | |
[ type' "text" | |
, H.onInput $ (\t -> model { inputText = InputText t }) | |
] | |
[] | |
, H.h3 [] [ S.text $ unInputText . inputText $ model ] | |
, (\c -> model { currentCount = c } ) | |
<$> counter (currentCount model) | |
, if currentCount model > 10 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