|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE DeriveGeneric #-} |
| 3 | +{-# LANGUAGE RecordWildCards #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +{-# LANGUAGE TypeFamilies #-} |
| 6 | +{-# LANGUAGE TypeOperators #-} |
| 7 | + |
| 8 | +module XHR (start) where |
| 9 | + |
| 10 | +-- slightly adapted from https://github.com/dmjio/miso/blob/master/examples/xhr/Main.hs |
| 11 | + |
| 12 | +import Control.Monad.IO.Class |
| 13 | +import Data.Aeson |
| 14 | +import qualified Data.Map as M |
| 15 | +import Data.Maybe |
| 16 | +import qualified Data.Text as T |
| 17 | +import GHC.Generics |
| 18 | +import GHC.Wasm.Prim |
| 19 | + |
| 20 | +import Miso hiding (defaultOptions) |
| 21 | +import Miso.String |
| 22 | + |
| 23 | +-- | Model |
| 24 | +data Model |
| 25 | + = Model |
| 26 | + { info :: Maybe APIInfo |
| 27 | + } deriving (Eq, Show) |
| 28 | + |
| 29 | +-- | Action |
| 30 | +data Action |
| 31 | + = FetchGitHub |
| 32 | + | SetGitHub APIInfo |
| 33 | + | NoOp |
| 34 | + deriving (Show, Eq) |
| 35 | + |
| 36 | +-- | Main entry point |
| 37 | +start :: JSM () |
| 38 | +start = do |
| 39 | + startApp App { model = Model Nothing |
| 40 | + , initialAction = NoOp |
| 41 | + , mountPoint = Nothing |
| 42 | + , .. |
| 43 | + } |
| 44 | + where |
| 45 | + update = updateModel |
| 46 | + events = defaultEvents |
| 47 | + subs = [] |
| 48 | + view = viewModel |
| 49 | + logLevel = Off |
| 50 | + |
| 51 | +-- | Update your model |
| 52 | +updateModel :: Action -> Model -> Effect Action Model |
| 53 | +updateModel FetchGitHub m = m <# do |
| 54 | + SetGitHub <$> getGitHubAPIInfo |
| 55 | +updateModel (SetGitHub apiInfo) m = |
| 56 | + noEff m { info = Just apiInfo } |
| 57 | +updateModel NoOp m = noEff m |
| 58 | + |
| 59 | +-- | View function, with routing |
| 60 | +viewModel :: Model -> View Action |
| 61 | +viewModel Model {..} = view |
| 62 | + where |
| 63 | + view = div_ [ style_ $ M.fromList [ |
| 64 | + (pack "text-align", pack "center") |
| 65 | + , (pack "margin", pack "200px") |
| 66 | + ] |
| 67 | + ] [ |
| 68 | + h1_ [class_ $ pack "title" ] [ text $ pack "Miso XHR Example" ] |
| 69 | + , button_ attrs [ |
| 70 | + text $ pack "Fetch JSON from https://api.github.com via XHR" |
| 71 | + ] |
| 72 | + , case info of |
| 73 | + Nothing -> div_ [] [ text $ pack "No data" ] |
| 74 | + Just APIInfo{..} -> |
| 75 | + table_ [ class_ $ pack "table is-striped" ] [ |
| 76 | + thead_ [] [ |
| 77 | + tr_ [] [ |
| 78 | + th_ [] [ text $ pack "URLs"] |
| 79 | + ] |
| 80 | + ] |
| 81 | + , tbody_ [] [ |
| 82 | + tr_ [] [ td_ [] [ text current_user_url ] ] |
| 83 | + , tr_ [] [ td_ [] [ text emojis_url ] ] |
| 84 | + , tr_ [] [ td_ [] [ text emails_url ] ] |
| 85 | + , tr_ [] [ td_ [] [ text events_url ] ] |
| 86 | + , tr_ [] [ td_ [] [ text gists_url ] ] |
| 87 | + , tr_ [] [ td_ [] [ text feeds_url ] ] |
| 88 | + , tr_ [] [ td_ [] [ text followers_url ] ] |
| 89 | + , tr_ [] [ td_ [] [ text following_url ] ] |
| 90 | + ] |
| 91 | + ] |
| 92 | + ] |
| 93 | + where |
| 94 | + attrs = [ onClick FetchGitHub |
| 95 | + , class_ $ pack "button is-large" |
| 96 | + ] ++ [ disabled_ True | isJust info ] |
| 97 | + |
| 98 | +data APIInfo |
| 99 | + = APIInfo |
| 100 | + { current_user_url :: MisoString |
| 101 | + , current_user_authorizations_html_url :: MisoString |
| 102 | + , authorizations_url :: MisoString |
| 103 | + , code_search_url :: MisoString |
| 104 | + , commit_search_url :: MisoString |
| 105 | + , emails_url :: MisoString |
| 106 | + , emojis_url :: MisoString |
| 107 | + , events_url :: MisoString |
| 108 | + , feeds_url :: MisoString |
| 109 | + , followers_url :: MisoString |
| 110 | + , following_url :: MisoString |
| 111 | + , gists_url :: MisoString |
| 112 | + , hub_url :: MisoString |
| 113 | + , issue_search_url :: MisoString |
| 114 | + , issues_url :: MisoString |
| 115 | + , keys_url :: MisoString |
| 116 | + , notifications_url :: MisoString |
| 117 | + , organization_repositories_url :: MisoString |
| 118 | + , organization_url :: MisoString |
| 119 | + , public_gists_url :: MisoString |
| 120 | + , rate_limit_url :: MisoString |
| 121 | + , repository_url :: MisoString |
| 122 | + , repository_search_url :: MisoString |
| 123 | + , current_user_repositories_url :: MisoString |
| 124 | + , starred_url :: MisoString |
| 125 | + , starred_gists_url :: MisoString |
| 126 | + , user_url :: MisoString |
| 127 | + , user_organizations_url :: MisoString |
| 128 | + , user_repositories_url :: MisoString |
| 129 | + , user_search_url :: MisoString |
| 130 | + } deriving (Show, Eq, Generic) |
| 131 | + |
| 132 | +instance FromJSON APIInfo where |
| 133 | + parseJSON = genericParseJSON defaultOptions { fieldLabelModifier = camelTo2 '_' } |
| 134 | + |
| 135 | +getGitHubAPIInfo :: JSM APIInfo |
| 136 | +getGitHubAPIInfo = do |
| 137 | + resp <- liftIO $ |
| 138 | + T.pack . fromJSString <$> js_fetch (toJSString "https://api.github.com") |
| 139 | + case eitherDecodeStrictText resp :: Either String APIInfo of |
| 140 | + Left s -> error s |
| 141 | + Right j -> pure j |
| 142 | + |
| 143 | +-- We use the WASM JS FFI here to access the more modern fetch API. If you want |
| 144 | +-- your code to eg also work when compiling with non-cross GHC and using |
| 145 | +-- jsaddle-warp, you can use fetch or XMLHttpRequest via JSaddle, for example |
| 146 | +-- via ghcjs-dom, servant-jsaddle or servant-client-js. |
| 147 | +foreign import javascript safe "const r = await fetch($1); return r.text();" |
| 148 | + js_fetch :: JSString -> IO JSString |
0 commit comments