Skip to content

Instantly share code, notes, and snippets.

@Fresheyeball
Forked from abradley2/Shpa.hs
Last active August 26, 2020 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fresheyeball/aab8af5d6f243acf9ee1d661059232c0 to your computer and use it in GitHub Desktop.
Save Fresheyeball/aab8af5d6f243acf9ee1d661059232c0 to your computer and use it in GitHub Desktop.
Being silly with Shpadoinkle
{-# 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