-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHViewInstance.hs
46 lines (38 loc) · 1.73 KB
/
HViewInstance.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{-# LANGUAGE DeriveGeneric, OverloadedStrings #-}
module HViewInstance (
Counter(..),
updateCount,
handleMessage,
) where
import qualified Network.WebSockets as WS
import HViewWebSockets (Renderer(..), SendMessage(..), sendJsonMessage, Body(..), Message(..))
import Text.Mustache
import Text.Mustache.Compile
import GHC.Generics (Generic)
import Data.Aeson (ToJSON, toJSON, (.=), object, encode)
import Data.Text.Lazy as TL
data Counter = Counter {
count :: Int
} deriving (Show, Generic)
instance ToJSON Counter
instance Renderer Counter where
render hId (Counter count) = do
let templateStr = "<div h-id={{id}}><h1>Counter: <div h-value>{{count}}</div></h1><button h-click=\"increment\">Increment</button><button h-click=\"decrement\">Decrement</button></div>"
let compiledTemplate = compileMustacheText "page" templateStr
case compiledTemplate of
Left bundle -> "Error compiling template"
Right template -> renderMustache template $ object
[ "count" .= (count :: Int)
, "id" .= (hId :: String) ]
updateCount :: Counter -> Int -> Counter
updateCount (Counter oldCount) newCount = Counter newCount
handleMessage :: WS.Connection -> Message -> IO ()
handleMessage conn message = do
putStrLn $ "Handling message with ID: " ++ hID message
case body message of
Body dispatch payload -> do
let html = case dispatch of
"increment" -> render (hID message) (Counter (payload + 1))
"decrement" -> render (hID message) (Counter (payload - 2))
_ -> render (hID message) (Counter payload)
sendJsonMessage conn (SendMessage { hId = hID message, html = TL.unpack html })