-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathMain.hs
107 lines (94 loc) · 2.49 KB
/
Main.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE CPP #-}
module Main where
import Data.Proxy
import Servant.API
#if MIN_VERSION_servant(0,14,1)
import Servant.Links
#elif MIN_VERSION_servant(0,10,0)
import Servant.Utils.Links
#endif
import Miso
#ifdef IOS
import Language.Javascript.JSaddle.WKWebView as JSaddle
runApp :: JSM () -> IO ()
runApp = JSaddle.run
#else
import Language.Javascript.JSaddle.Warp as JSaddle
runApp :: JSM () -> IO ()
runApp = JSaddle.run 8080
#endif
-- | Model
data Model
= Model
{ uri :: URI
-- ^ current URI of application
} deriving (Eq, Show)
-- | Action
data Action
= HandleURI URI
| ChangeURI URI
| NoOp
deriving (Show, Eq)
-- | Main entry point
main :: IO ()
main =
runApp $ do
currentURI <- getCurrentURI
startApp App { model = Model currentURI, initialAction = NoOp, ..}
where
update = updateModel
events = defaultEvents
subs = [ uriSub HandleURI ]
view = viewModel
mountPoint = Nothing
logLevel = Off
-- | Update your model
updateModel :: Action -> Model -> Effect Action Model
updateModel (HandleURI u) m = m { uri = u } <# do
pure NoOp
updateModel (ChangeURI u) m = m <# do
pushURI u
pure NoOp
updateModel _ m = noEff m
-- | View function, with routing
viewModel :: Model -> View Action
viewModel model = view
where
view =
either (const the404) id
$ runRoute (Proxy :: Proxy API) handlers uri model
handlers = about :<|> home
home (_ :: Model) = div_ [] [
div_ [] [ text "home" ]
, button_ [ onClick goAbout ] [ text "go about" ]
]
about (_ :: Model) = div_ [] [
div_ [] [ text "about" ]
, button_ [ onClick goHome ] [ text "go home" ]
]
the404 = div_ [] [
text "the 404 :("
, button_ [ onClick goHome ] [ text "go home" ]
]
-- | Type-level routes
type API = About :<|> Home
type Home = View Action
type About = "about" :> View Action
-- | Type-safe links used in `onClick` event handlers to route the application
goAbout, goHome :: Action
(goHome, goAbout) = (goto api home, goto api about)
where
#if MIN_VERSION_servant(0,10,0)
goto a b = ChangeURI (linkURI (safeLink a b))
#else
goto a b = ChangeURI (safeLink a b)
#endif
home = Proxy :: Proxy Home
about = Proxy :: Proxy About
api = Proxy :: Proxy API