-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCommon.hs
117 lines (100 loc) · 2.98 KB
/
Common.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
107
108
109
110
111
112
113
114
115
116
117
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE CPP #-}
module Common where
import Control.Lens
import Data.Proxy ( Proxy(..) )
import qualified Servant.API as Servant
import Servant.API ( (:<|>)(..), (:>) )
#if MIN_VERSION_servant(0,10,0)
import qualified Servant.Links as Servant
#endif
import qualified Miso
import Miso ( View )
import Miso.Html
import qualified Miso.String as Miso
import qualified Network.URI as Network
data Model
= Model
{ _uri :: !Network.URI
, _counterValue :: !Int
}
deriving (Eq, Show)
initialModel :: Network.URI -> Model
initialModel uri =
Model
{ _uri = uri
, _counterValue = 0
}
data Action
= NoOp
| AddOne
| SubtractOne
| ChangeURI !Network.URI
| HandleURIChange !Network.URI
deriving (Show, Eq)
-- Holds a servant route tree of `View action`
type ViewRoutes = Home :<|> Flipped
-- Home route, contains two buttons and a field
type Home = View Action
-- Flipped route, same as Home, but with the buttons flipped
type Flipped = "flipped" :> View Action
makeLenses ''Model
-- Checks which URI is open and shows the appropriate view
viewModel :: Model -> View Action
viewModel m =
case Miso.runRoute (Proxy @ViewRoutes) viewTree _uri m of
Left _routingError -> page404View
Right v -> v
-- Servant tree of view functions
-- Should follow the structure of ViewRoutes
viewTree
:: (Model -> View Action)
:<|> (Model -> View Action)
viewTree = homeView :<|> flippedView
-- View function of the Home route
homeView :: Model -> View Action
homeView m =
div_ []
[ div_
[]
[ button_ [ onClick SubtractOne ] [ text "-" ]
, text $ Miso.ms $ show $ _counterValue m
, button_ [ onClick AddOne ] [ text "+" ]
]
, button_ [ onClick $ ChangeURI flippedLink ] [ text "Go to /flipped" ]
]
-- View function of the Home route
flippedView :: Model -> View Action
flippedView m =
div_ []
[ div_
[]
[ button_ [ onClick AddOne ] [ text "+" ]
, text $ Miso.ms $ show $ _counterValue m
, button_ [ onClick SubtractOne ] [ text "-" ]
]
, button_ [ onClick $ ChangeURI homeLink ] [ text "Go to /" ]
]
page404View :: View Action
page404View =
text "Yo, 404, page unknown. Go to / or /flipped. Shoo!"
-- Network.URI that points to the home route
homeLink :: Network.URI
homeLink =
#if MIN_VERSION_servant(0,10,0)
Servant.linkURI $ Servant.safeLink (Proxy @ViewRoutes) (Proxy @Home)
#else
safeLink (Proxy @ViewRoutes) (Proxy @Home)
#endif
-- Network.URI that points to the flipped route
flippedLink :: Network.URI
flippedLink =
#if MIN_VERSION_servant(0,10,0)
Servant.linkURI $ Servant.safeLink (Proxy @ViewRoutes) (Proxy @Flipped)
#else
safeLink (Proxy @ViewRoutes) (Proxy @Flipped)
#endif