This is a servant-client binding to miso.
-----------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
-----------------------------------------------------------------------------
module Main where
-----------------------------------------------------------------------------
import Miso
import Miso.JSON
import Miso.Html.Element as H
import Miso.Html.Event as H
-----------------------------------------------------------------------------
import Data.Proxy
import Servant.Miso.Client
import Servant.API
-----------------------------------------------------------------------------
main :: IO ()
main = startApp defaultEvents myComponent
{ mount = Just Start
}
-----------------------------------------------------------------------------
type MyComponent = App () Action
-----------------------------------------------------------------------------
myComponent :: MyComponent
myComponent = component () update_ $ \() ->
H.div_ []
[ button_ [ onClick Download ] [ "download" ]
] where
update_ = \case
Download -> do
io_ (consoleLog "clicked")
downloadGithub Downloaded DownloadError
DownloadError Response {..} -> io_ $ do
consoleError $ ms (show errorMessage)
Downloaded Response {..} -> io_ $ do
consoleLog $ ms $ show body
Start -> io_ $ do
consoleLog "starting..."
-----------------------------------------------------------------------------
data Action
= Downloaded (Response Value)
| DownloadError (Response MisoString)
| Download
| Start
-----------------------------------------------------------------------------
type GitHubAPI = Get '[JSON] Value
-----------------------------------------------------------------------------
downloadGithub :: (Response Value -> Action) -> (Response MisoString -> Action) -> Effect ROOT () Action
downloadGithub successsful errorful = withSink $ \sink ->
toClient "https://api.github.com" (Proxy @GitHubAPI) (sink . successsful) (sink . errorful)
-----------------------------------------------------------------------------cabal buildcabal build