Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ flags: +template-haskell
source-repository-package
type: git
location: https://github.com/dmjio/miso
tag: cd98dc522027b150397a51d95055892dfa5a624e
tag: 7d48d27283283e0dbdea85bc68d2fe208ad68e3f
26 changes: 13 additions & 13 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 27 additions & 22 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import Data.Maybe
import GHC.Generics
----------------------------------------------------------------------------
import Miso hiding (defaultOptions)
import qualified Miso.Html.Event as E
import qualified Miso.Html.Element as H
import qualified Miso.Html.Property as P
import Miso.String
import Miso.Lens
import qualified Miso.CSS as CSS
Expand All @@ -46,9 +49,8 @@ info = lens _info $ \r x -> r { _info = x }
-- | Action
data Action
= FetchGitHub
| SetGitHub GitHub
| ErrorHandler MisoString
deriving (Show, Eq)
| SetGitHub (Response GitHub)
| ErrorHandler (Response (Maybe MisoString))
----------------------------------------------------------------------------
app :: App Model Action
app = (component emptyModel updateModel viewModel)
Expand All @@ -66,54 +68,57 @@ emptyModel = Model Nothing
updateModel :: Action -> Transition Model Action
updateModel FetchGitHub =
getJSON "https://api.github.com" [] SetGitHub ErrorHandler
updateModel (SetGitHub apiInfo) =
info ?= apiInfo
updateModel (ErrorHandler msg) =
io_ (consoleError msg)
updateModel (SetGitHub Response {..}) =
info ?= body
updateModel (ErrorHandler Response {..}) =
case errorMessage of
Nothing -> pure ()
Just errMsg ->
io_ (consoleError errMsg)
----------------------------------------------------------------------------
-- | View function, with routing
viewModel :: Model -> View Model Action
viewModel m = div_
viewModel m = H.div_
[ CSS.style_
[ CSS.textAlign "center"
, CSS.margin "200px"
]
]
[ h1_
[ class_ $ pack "title"
[ H.h1_
[ P.class_ $ pack "title"
]
[ "🍜 Miso Fetch API"
]
, optionalAttrs
button_
[ onClick FetchGitHub
, class_ (pack "button is-large is-outlined")
H.button_
[ E.onClick FetchGitHub
, P.class_ (pack "button is-large is-outlined")
]
(isJust (m ^. info))
[ disabled_
[ P.disabled_
]
[ "Fetch JSON from https://api.github.com"
]
, case m ^. info of
Nothing ->
div_
H.div_
[]
[ "No data"
]
Just GitHub {..} ->
table_
[ class_ "table is-striped" ]
[ thead_
H.table_
[ P.class_ "table is-striped" ]
[ H.thead_
[]
[ tr_
[ H.tr_
[]
[ th_
[ H.th_
[]
[ text "URLs"
]
]
]
, tbody_
, H.tbody_
[]
[ tr currentUserUrl
, tr emojisUrl
Expand All @@ -128,7 +133,7 @@ viewModel m = div_
]
where
tr :: MisoString -> View Model action
tr x = tr_ [] [ td_ [] [ text x ] ]
tr x = H.tr_ [] [ H.td_ [] [ text x ] ]
----------------------------------------------------------------------------
-- | Structure to capture the JSON returned from https://api.github.com
data GitHub
Expand Down