@@ -21,11 +21,9 @@ import Web.Hyperbole.HyperView
21
21
import Web.UI
22
22
23
23
24
- -- you can't automatically derive FromHttpApiData. I don't like it!
25
-
26
24
data Hyperbole :: Effect where
27
- RespondView :: View () () -> Hyperbole m ()
28
25
GetEvent :: (HyperView id action ) => Hyperbole m (Maybe (Event id action ))
26
+ RespondView :: View () () -> Hyperbole m ()
29
27
GetForm :: Hyperbole m Form
30
28
HyperError :: HyperError -> Hyperbole m a
31
29
@@ -46,16 +44,16 @@ runHyperbole = interpret $ \_ -> \case
46
44
send $ ResHeader " Content-Type" " text/html"
47
45
send $ ResBody ContentHtml bd
48
46
Wai. continue
47
+ GetForm -> Wai. formData
48
+ HyperError NotFound -> send $ Interrupt Wai. NotFound
49
+ HyperError (ParseError e) -> send $ Interrupt $ Wai. ParseError e
49
50
GetEvent -> do
50
51
q <- fmap queryString <$> send $ Wai. Request
51
52
pure $ do
52
53
Event ti ta <- lookupEvent q
53
54
vid <- parseParam ti
54
55
act <- parseParam ta
55
56
pure $ Event vid act
56
- GetForm -> Wai. formData
57
- HyperError NotFound -> send $ Interrupt Wai. NotFound
58
- HyperError (ParseError e) -> send $ Interrupt $ Wai. ParseError e
59
57
where
60
58
lookupParam :: ByteString -> Query -> Maybe Text
61
59
lookupParam p q =
@@ -90,18 +88,37 @@ data HyperError
90
88
| ParseError Text
91
89
92
90
93
- load :: (Hyperbole :> es ) => Eff es (View () () ) -> Eff es ()
94
- load pg = do
95
- vw <- pg
91
+ newtype Page es a = Page (Eff es a )
92
+ deriving newtype (Applicative , Monad , Functor )
93
+
94
+
95
+ -- | Load the entire page when no HyperViews match
96
+ load
97
+ :: (Hyperbole :> es )
98
+ => Eff es (View () () )
99
+ -> Page es ()
100
+ load run = Page $ do
101
+ vw <- run
96
102
send $ RespondView vw
97
103
98
104
99
- hyper :: forall id action es . (Hyperbole :> es , HyperView id action ) => (id -> action -> Eff es (View id () )) -> Eff es ()
100
- hyper run = do
105
+ -- | Handle a HyperView. If the event matches our handler, respond with the fragment
106
+ hyper
107
+ :: (Hyperbole :> es , HyperView id action )
108
+ => (id -> action -> Eff es (View id () ))
109
+ -> Page es ()
110
+ hyper run = Page $ do
101
111
-- Get an event matching our type. If it doesn't match, skip to the next handler
102
112
mev <- send GetEvent
103
113
case mev of
104
114
Just (Event vid act) -> do
105
115
vw <- run vid act
106
116
send $ RespondView $ viewId vid vw
107
117
_ -> pure ()
118
+
119
+
120
+ page
121
+ :: (Hyperbole :> es )
122
+ => Page es ()
123
+ -> Eff es ()
124
+ page (Page eff) = eff
0 commit comments