1{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE RankNTypes #-}
5{-# LANGUAGE RecursiveDo #-}
6{-# LANGUAGE ScopedTypeVariables #-}
10------------------------------------------------------------------------------
13import Control.Monad.Fix (MonadFix)
14import Data.Monoid (First(..), (<>))
15import Data.Text (Text)
16import qualified Data.Text as T
21import Text.Read (readMaybe)
22import Reflex.Dom hiding (run)
23------------------------------------------------------------------------------
25import Servant.Reflex.Multi
26import qualified TupleExample
34 el "h1" $ text "Wowzers!"
35 divClass "example-base" run
36 divClass "example-multi" runMulti
37 divClass "example-tuple" TupleExample.run
40runMulti :: forall t m. (SupportsServantReflex t m,
42 DomBuilderSpace m ~ GhcjsDomSpace,
45 MonadHold t m) => m ()
49 let (_ :<|> _ :<|> sayHi :<|> dbl :<|> _ :<|> _ ) =
50 clientA api (Proxy :: Proxy m) (Proxy :: Proxy []) (Proxy :: Proxy Int) url
52 num :: Dynamic t (Either Text Double) <- fmap (note "No read" . readMaybe . T.unpack) . value <$> textInput def
53 num2 :: Dynamic t (Either Text Double) <- fmap (note "No read" . readMaybe . T.unpack) . value <$> textInput def
55 b <- button "Run dbl multi"
56 reqCount :: Dynamic t Int <- count b
57 r <- dbl ((\x y -> [x,y]) <$> num <*> num2) (tag (current reqCount) b)
58 dynText =<< holdDyn "Waiting" (T.pack . show . fmap reqSuccess <$> r)
60 lastInd <- holdDyn [] $ fmap reqTag <$> r
63 divClass "demo-group" $ do
64 nms <- fmap (fmap T.words . value) $ divClass "" $ do
67 grts <- fmap (fmap T.words . value) $ divClass "" $ do
70 gust <- fmap value $ divClass "gusto-input" $ checkbox False def
72 r' <- sayHi (fmap QParamSome <$> nms) (fmap (:[]) grts) (constDyn [True, False]) (1 <$ b)
74 dynText =<< holdDyn "Waiting" (T.pack . show . catMaybes . fmap reqSuccess <$> r')
78run :: forall t m. (SupportsServantReflex t m,
80 DomBuilderSpace m ~ GhcjsDomSpace,
83 MonadHold t m) => m ()
86 reqCount <- count $ leftmost
87 [() <$ unitBtn, () <$ intBtn, () <$ sayHiClicks, () <$ dblBtn, () <$ mpGo]
88 -- Allow user to choose the url target for the request
89 -- (alternatively we could just `let url = constDyn (BasePath "/")`)
92 dynText $ showBaseUrl <$> url
96 -- Name the computed API client functions
97 let (getUnit :<|> getInt :<|> sayhi :<|> dbl :<|> multi :<|> qna :<|> doRaw) =
98 client api (Proxy :: Proxy m) (Proxy :: Proxy Int) url
100 c2 = client (Proxy :: Proxy ComprehensiveAPI) (Proxy :: Proxy m) (Proxy :: Proxy ()) url -- Just make sure this compiles for now
102 (unitBtn, intBtn) <- elClass "div" "demo-group" $ do
103 unitBtn <- divClass "unit-button" $ button "Get unit"
104 intBtn <- divClass "int-button" $ button "Get int"
106 unitResponse <- getUnit $ tag (current reqCount) unitBtn
107 intResponse :: Event t (ReqResult Int Int) <- getInt $ tag (current reqCount) intBtn
109 score <- foldDyn (+) 0 (fmapMaybe reqSuccess intResponse)
111 r <- holdDyn "Waiting" $ showXhrResponse <$>
112 leftmost [fmapMaybe response unitResponse
113 ,fmapMaybe response intResponse
115 divClass "unit-int-response" $ el "p" $ dynText r >> el "br" (return ()) >> text "Total: " >> display score
116 return (unitBtn, intBtn)
118 sayHiClicks <- elClass "div" "demo-group" $ do
122 inp :: Dynamic t Text <- fmap value $ divClass "name-input" $ textInput def
123 let checkedName = fmap (\i -> bool (QParamSome i) (QParamInvalid "Need a name") (T.null i)) inp
126 text "Greetings (space-separated)"
128 greetings <- fmap (fmap T.words . value) $
129 divClass "greetings-input" $ textInput def
133 gusto <- fmap value $ divClass "gusto-input" $ checkbox False def
136 sayHiClicks :: Event t () <- divClass "hi-button" $ button "Say hi"
137 let triggers = leftmost [sayHiClicks, () <$ updated inp]
139 resp <- sayhi checkedName greetings gusto (tag (current reqCount) triggers)
140 divClass "greeting-response" $ dynText =<<
141 holdDyn "No hi yet" (leftmost [ fmapMaybe reqSuccess resp
142 , fmapMaybe reqFailure resp])
145 dblBtn <- elClass "div" "demo-group" $ do
146 text "A Double to double"
148 dblinp <- fmap value $ divClass "double-input" $ textInput def
149 dblBtn <- divClass "double-button" $ button "Double it"
150 dblResp <- dbl ((note "read failure" . readMaybe . T.unpack) <$>
151 dblinp) (tag (current reqCount) dblBtn)
152 divClass "double-errors" $ dynText =<<
153 holdDyn "(no errors)" (fmapMaybe reqFailure dblResp)
155 divClass "double-result" $ el "p" $ dynText =<<
156 holdDyn "No number yet" (tShow <$>
157 fmapMaybe reqSuccess dblResp)
160 mpGo <- elClass "div" "demo-group" $ do
161 text "Multi-part path"
162 b <- value <$> checkbox False def
163 mpGo <- button "Test"
164 multiResp <- multi b (tag (current reqCount) mpGo)
165 dynText =<< holdDyn "No res yet" (tShow <$>
174 elClass "div" "demo-group" $ do
175 text "JSON Unicode encoding test"
176 txt <- value <$> textInput def
177 ev <- (1 <$) <$> button "Question"
178 let dQ = Right . Question <$> traceDyn "will send: " txt
181 dynText =<< holdDyn "No Answer" (unAnswer <$> fmapMaybe reqSuccess rr)
183showXhrResponse :: XhrResponse -> Text
184showXhrResponse (XhrResponse stat stattxt rbmay rtmay respHeaders) =
185 T.unlines ["stat: " <> tShow stat
186 ,"stattxt: " <> tShow stattxt
187 ,"resp: " <> maybe "" showRB rbmay
188 ,"rtext: " <> tShow rtmay
189 ,"rHeaders: " <> tShow respHeaders]
191tShow :: Show a => a -> Text
194showRB :: XhrResponseBody -> Text
195showRB (XhrResponseBody_Default t) = tShow t
196showRB (XhrResponseBody_Text t) = tShow t
197showRB (XhrResponseBody_Blob t) = "<Blob>"
198showRB (XhrResponseBody_ArrayBuffer t) = tShow t
200note :: e -> Maybe a -> Either e a
201note e = maybe (Left e) Right
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list