1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE RankNTypes #-}
6{-# LANGUAGE RecursiveDo #-}
7{-# LANGUAGE ScopedTypeVariables #-}
11------------------------------------------------------------------------------
14import Control.Monad.Fix (MonadFix)
15import Data.Monoid (First(..), (<>))
16import Data.Text (Text)
17import qualified Data.Text as T
18import qualified Data.Text.Encoding as T
23import Text.Read (readMaybe)
24import Reflex.Dom.Core hiding (run, mainWidget)
25import Reflex.Dom (mainWidget)
26------------------------------------------------------------------------------
28import Servant.Reflex.Multi
36 divClass "example-base" run
37 divClass "example-multi" runMulti
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)
73 (constDyn [True, False]) (1 <$ b)
75 dynText =<< holdDyn "Waiting" (T.pack . show . catMaybes .
76 fmap reqSuccess <$> r')
81run :: forall t m. (SupportsServantReflex t m,
83 DomBuilderSpace m ~ GhcjsDomSpace,
86 MonadHold t m) => m ()
89 reqCount <- count $ leftmost
90 [() <$ unitBtn, () <$ intBtn, () <$ sayHiClicks, () <$ dblBtn, () <$ mpGo]
91 -- Allow user to choose the url target for the request
92 -- (alternatively we could just `let url = constDyn (BasePath "/")`)
95 dynText $ showBaseUrl <$> url
99 -- Name the computed API client functions
100 let tweakRequest = ClientOptions $ \r -> do
101 return $ r & withCredentials .~ True
102 let (getUnit :<|> getInt :<|> sayhi :<|> dbl
103 :<|> multi :<|> qna :<|> secret :<|> doRaw) =
104 clientWithOpts api (Proxy :: Proxy m) (Proxy :: Proxy Int) url tweakRequest
106 c2 = client (Proxy :: Proxy ComprehensiveAPI) (Proxy :: Proxy m) (Proxy :: Proxy ()) url -- Just make sure this compiles for now
108 (unitBtn, intBtn) <- elClass "div" "demo-group" $ do
109 unitBtn <- divClass "unit-button" $ button "Get unit"
110 intBtn <- divClass "int-button" $ button "Get int"
112 unitResponse <- getUnit $ tag (current reqCount) unitBtn
113 intResponse :: Event t (ReqResult Int Int) <- getInt $ tag (current reqCount) intBtn
115 score <- foldDyn (+) 0 (fmapMaybe reqSuccess (intResponse))
117 r <- holdDyn "Waiting" $ fmap showXhrResponse $
118 leftmost [fmapMaybe response (unitResponse)
119 ,fmapMaybe response (intResponse)
121 divClass "unit-int-response" $ el "p" $ dynText r >> el "br" (return ()) >> text "Total: " >> display score
122 return (unitBtn, intBtn)
124 sayHiClicks <- elClass "div" "demo-group" $ do
128 inp :: Dynamic t Text <- fmap value $ divClass "name-input" $ (textInput def)
129 let checkedName = fmap (\i -> bool (QParamSome i) (QParamInvalid "Need a name") (T.null i)) inp
132 text "Greetings (space-separated)"
134 greetings <- fmap (fmap T.words . value) $
135 divClass "greetings-input" $ (textInput def)
139 gusto <- fmap value $ divClass "gusto-input" $ checkbox False def
142 sayHiClicks :: Event t () <- divClass "hi-button" $ button "Say hi"
143 let triggers = leftmost [sayHiClicks, () <$ updated inp]
145 resp <- sayhi checkedName greetings gusto (tag (current reqCount) triggers)
146 divClass "greeting-response" $ dynText =<<
147 holdDyn "No hi yet" (leftmost [ fmapMaybe reqSuccess (resp)
148 , fmapMaybe reqFailure (resp)])
151 dblBtn <- elClass "div" "demo-group" $ do
152 text "A Double to double"
154 dblinp <- fmap value $ divClass "double-input" $ textInput def
155 (dblBtn) <- divClass "double-button" $ button "Double it"
156 dblResp <- dbl (fmap (note "read failure" . readMaybe . T.unpack) $
157 dblinp) (tag (current reqCount) dblBtn)
158 divClass "double-errors" $ dynText =<<
159 holdDyn "(no errors)" (fmapMaybe reqFailure (dblResp))
161 divClass "double-result" $ el "p" $ dynText =<<
162 holdDyn "No number yet" (fmap tShow $
163 fmapMaybe reqSuccess (dblResp))
166 mpGo <- elClass "div" "demo-group" $ do
167 text "Multi-part path"
168 b <- value <$> checkbox False def
169 mpGo <- button "Test"
170 multiResp <- multi b (tag (current reqCount) mpGo)
171 dynText =<< holdDyn "No res yet" (fmap tShow $
172 fmapMaybe reqSuccess $
180 elClass "div" "demo-group" $ do
181 text "JSON Unicode encoding test"
182 txt <- value <$> textInput def
183 ev <- fmap (1 <$) $ button "Question"
184 let dQ = Right . Question <$> traceDyn "will send: " txt
187 dynText =<< holdDyn "No Answer" (unAnswer <$> fmapMaybe reqSuccess rr)
189 divClass "demo-group" $ do
190 un <- fmap value $ text "Username" >> textArea def
191 pw <- fmap value $ text "Unhidden PW" >> textArea def
192 let ba :: Dynamic t (BasicAuthData ) = BasicAuthData
193 <$> fmap T.encodeUtf8 un
194 <*> fmap T.encodeUtf8 pw
195 b <- button "Get secret"
196 r <- secret (Just <$> ba) (0 <$ b)
197 res <- holdDyn Nothing (reqSuccess <$> r)
201showXhrResponse :: XhrResponse -> Text
202showXhrResponse (XhrResponse stat stattxt rbmay rtmay respHeaders) =
203 T.unlines ["stat: " <> tShow stat
204 ,"stattxt: " <> tShow stattxt
205 ,"resp: " <> maybe "" showRB rbmay
206 ,"rtext: " <> tShow rtmay
207 ,"rHeaders: " <> tShow respHeaders]
209tShow :: Show a => a -> Text
212showRB :: XhrResponseBody -> Text
213showRB (XhrResponseBody_Default t) = tShow t
214showRB (XhrResponseBody_Text t) = tShow t
215showRB (XhrResponseBody_Blob t) = "<Blob>"
216showRB (XhrResponseBody_ArrayBuffer t) = tShow t
218note :: e -> Maybe a -> Either e a
219note 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