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)
23------------------------------------------------------------------------------
25import Servant.Reflex.Multi
33 divClass "example-base" run
34 divClass "example-multi" runMulti
37runMulti :: forall t m. (SupportsServantReflex t m,
39 DomBuilderSpace m ~ GhcjsDomSpace,
42 MonadHold t m) => m ()
46 let (_ :<|> _ :<|> sayHi :<|> dbl :<|> _ :<|> _ ) =
47 clientA api (Proxy :: Proxy m) (Proxy :: Proxy []) (Proxy :: Proxy Int) url
49 num :: Dynamic t (Either Text Double) <- fmap (note "No read" . readMaybe . T.unpack) . value <$> textInput def
50 num2 :: Dynamic t (Either Text Double) <- fmap (note "No read" . readMaybe . T.unpack) . value <$> textInput def
52 b <- button "Run dbl multi"
53 reqCount :: Dynamic t Int <- count b
54 r <- dbl ((\x y -> [x,y]) <$> num <*> num2) (tag (current reqCount) b)
55 dynText =<< holdDyn "Waiting" (T.pack . show . fmap reqSuccess <$> r)
57 lastInd <- holdDyn [] $ fmap reqTag <$> r
60 divClass "demo-group" $ do
61 nms <- fmap (fmap T.words . value) $ divClass "" $ do
64 grts <- fmap (fmap T.words . value) $ divClass "" $ do
67 gust <- fmap (value) $ divClass "gusto-input" $ checkbox False def
69 r' <- sayHi (fmap QParamSome <$> nms) (fmap (:[]) $ grts) (constDyn [True, False]) (1 <$ b)
71 dynText =<< holdDyn "Waiting" (T.pack . show . catMaybes . fmap reqSuccess <$> r')
75run :: forall t m. (SupportsServantReflex t m,
77 DomBuilderSpace m ~ GhcjsDomSpace,
80 MonadHold t m) => m ()
83 reqCount <- count $ leftmost
84 [() <$ unitBtn, () <$ intBtn, () <$ sayHiClicks, () <$ dblBtn, () <$ mpGo]
85 -- Allow user to choose the url target for the request
86 -- (alternatively we could just `let url = constDyn (BasePath "/")`)
89 dynText $ showBaseUrl <$> url
93 -- Name the computed API client functions
94 let (getUnit :<|> getInt :<|> sayhi :<|> dbl :<|> multi :<|> qna :<|> doRaw) =
95 client api (Proxy :: Proxy m) (Proxy :: Proxy Int) url
97 c2 = client (Proxy :: Proxy ComprehensiveAPI) (Proxy :: Proxy m) (Proxy :: Proxy ()) url -- Just make sure this compiles for now
99 (unitBtn, intBtn) <- elClass "div" "demo-group" $ do
100 unitBtn <- divClass "unit-button" $ button "Get unit"
101 intBtn <- divClass "int-button" $ button "Get int"
103 unitResponse <- getUnit $ tag (current reqCount) unitBtn
104 intResponse :: Event t (ReqResult Int Int) <- getInt $ tag (current reqCount) intBtn
106 score <- foldDyn (+) 0 (fmapMaybe reqSuccess (intResponse))
108 r <- holdDyn "Waiting" $ fmap showXhrResponse $
109 leftmost [fmapMaybe response (unitResponse)
110 ,fmapMaybe response (intResponse)
112 divClass "unit-int-response" $ el "p" $ dynText r >> el "br" (return ()) >> text "Total: " >> display score
113 return (unitBtn, intBtn)
115 sayHiClicks <- elClass "div" "demo-group" $ do
119 inp :: Dynamic t Text <- fmap value $ divClass "name-input" $ (textInput def)
120 let checkedName = fmap (\i -> bool (QParamSome i) (QParamInvalid "Need a name") (T.null i)) inp
123 text "Greetings (space-separated)"
125 greetings <- fmap (fmap T.words . value) $
126 divClass "greetings-input" $ (textInput def)
130 gusto <- fmap value $ divClass "gusto-input" $ checkbox False def
133 sayHiClicks :: Event t () <- divClass "hi-button" $ button "Say hi"
134 let triggers = leftmost [sayHiClicks, () <$ updated inp]
136 resp <- sayhi checkedName greetings gusto (tag (current reqCount) triggers)
137 divClass "greeting-response" $ dynText =<<
138 holdDyn "No hi yet" (leftmost [ fmapMaybe reqSuccess (resp)
139 , fmapMaybe reqFailure (resp)])
142 dblBtn <- elClass "div" "demo-group" $ do
143 text "A Double to double"
145 dblinp <- fmap value $ divClass "double-input" $ textInput def
146 (dblBtn) <- divClass "double-button" $ button "Double it"
147 dblResp <- dbl (fmap (note "read failure" . readMaybe . T.unpack) $
148 dblinp) (tag (current reqCount) dblBtn)
149 divClass "double-errors" $ dynText =<<
150 holdDyn "(no errors)" (fmapMaybe reqFailure (dblResp))
152 divClass "double-result" $ el "p" $ dynText =<<
153 holdDyn "No number yet" (fmap tShow $
154 fmapMaybe reqSuccess (dblResp))
157 mpGo <- elClass "div" "demo-group" $ do
158 text "Multi-part path"
159 b <- value <$> checkbox False def
160 mpGo <- button "Test"
161 multiResp <- multi b (tag (current reqCount) mpGo)
162 dynText =<< holdDyn "No res yet" (fmap tShow $
163 fmapMaybe reqSuccess $
171 elClass "div" "demo-group" $ do
172 text "JSON Unicode encoding test"
173 txt <- value <$> textInput def
174 ev <- fmap (1 <$) $ button "Question"
175 let dQ = Right . Question <$> traceDyn "will send: " txt
178 dynText =<< holdDyn "No Answer" (unAnswer <$> fmapMaybe reqSuccess rr)
180showXhrResponse :: XhrResponse -> Text
181showXhrResponse (XhrResponse stat stattxt rbmay rtmay respHeaders) =
182 T.unlines ["stat: " <> tShow stat
183 ,"stattxt: " <> tShow stattxt
184 ,"resp: " <> maybe "" showRB rbmay
185 ,"rtext: " <> tShow rtmay
186 ,"rHeaders: " <> tShow respHeaders]
188tShow :: Show a => a -> Text
191showRB :: XhrResponseBody -> Text
192showRB (XhrResponseBody_Default t) = tShow t
193showRB (XhrResponseBody_Text t) = tShow t
194showRB (XhrResponseBody_Blob t) = "<Blob>"
195showRB (XhrResponseBody_ArrayBuffer t) = tShow t
197note :: e -> Maybe a -> Either e a
198note 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