1{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE RankNTypes #-}
5{-# LANGUAGE ScopedTypeVariables #-}
11import Control.Monad.Fix (MonadFix)
12import Data.Monoid ((<>))
13import Data.Text (Text)
14import qualified Data.Text as T
19import Text.Read (readMaybe)
29run :: forall t m. (SupportsServantReflex t m,
31 DomBuilderSpace m ~ GhcjsDomSpace,
34 MonadHold t m) => m ()
37 -- Allow user to choose the url target for the request
38 -- (alternatively we could just `let url = constDyn (BasePath "/")`)
41 dynText $ showBaseUrl <$> url
44 -- Name the computed API client functions
45 let (getUnit :<|> getInt :<|> sayhi :<|> dbl :<|> multi :<|> doRaw) =
46 client api (Proxy :: Proxy m) url
48 elClass "div" "demo-group" $ do
49 unitBtn <- button "Get unit"
50 intBtn <- button "Get int"
52 unitResponse <- getUnit unitBtn
53 intResponse :: Event t (ReqResult Int) <- getInt intBtn
55 score <- foldDyn (+) 0 (fmapMaybe reqSuccess intResponse)
57 r <- holdDyn "Waiting" $ fmap showXhrResponse $
58 leftmost [fmapMaybe response unitResponse
59 ,fmapMaybe response intResponse
61 dynText r >> el "br" (return ()) >> text "Total: " >> display score
63 elClass "div" "demo-group" $ do
67 inp :: Dynamic t Text <- fmap value (textInput def)
68 let checkedName = fmap (\i -> bool (QParamSome i) (QParamInvalid "Need a name") (T.null i)) inp
71 text "Greetings (space-separated)"
73 greetings <- fmap (fmap T.words . value) (textInput def)
77 gusto <- value <$> checkbox False def
80 sayhiClicks :: Event t () <- button "Say hi"
81 let triggers = leftmost [sayhiClicks, () <$ updated inp]
83 resp <- sayhi checkedName greetings gusto triggers
84 dynText =<< holdDyn "No hi yet" (leftmost [fmapMaybe reqSuccess resp, fmapMaybe reqFailure resp])
86 elClass "div" "demo-group" $ do
87 text "A Double to double"
89 dblinp <- value <$> textInput def
90 dblBtn <- button "Double it"
91 dblResp <- dbl (fmap (note "read failure" . readMaybe . T.unpack) $ dblinp) dblBtn
92 dynText =<< holdDyn "(no errors)" (fmapMaybe reqFailure dblResp)
94 display =<< holdDyn "No number yet" (fmap tShow $ fmapMaybe reqSuccess dblResp)
96 elClass "div" "demo-group" $ do
97 text "Multi-part path"
98 b <- value <$> checkbox False def
100 multiResp <- multi b mpGo
101 dynText =<< holdDyn "No res yet" (fmap tShow $ fmapMaybe reqSuccess $ multiResp)
103showXhrResponse :: XhrResponse -> Text
104showXhrResponse (XhrResponse stat stattxt rbmay rtmay respHeaders) =
105 T.unlines ["stat: " <> tShow stat
106 ,"stattxt: " <> tShow stattxt
107 ,"resp: " <> maybe "" showRB rbmay
108 ,"rtext: " <> tShow rtmay
109 ,"rHeaders: " <> tShow respHeaders]
111tShow :: Show a => a -> Text
114showRB :: XhrResponseBody -> Text
115showRB (XhrResponseBody_Default t) = tShow t
116showRB (XhrResponseBody_Text t) = tShow t
117showRB (XhrResponseBody_Blob t) = "<Blob>"
118showRB (XhrResponseBody_ArrayBuffer t) = tShow t
120note :: e -> Maybe a -> Either e a
121note 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