@@ -23,6 +23,9 @@ import GHC.Generics
2323----------------------------------------------------------------------------
2424import Miso hiding (defaultOptions )
2525import Miso.String
26+ import qualified Miso.Html.Event as E
27+ import qualified Miso.Html.Element as H
28+ import qualified Miso.Html.Property as P
2629import Miso.Lens
2730import qualified Miso.CSS as CSS
2831----------------------------------------------------------------------------
@@ -46,9 +49,8 @@ info = lens _info $ \r x -> r { _info = x }
4649-- | Action
4750data Action
4851 = FetchGitHub
49- | SetGitHub GitHub
50- | ErrorHandler MisoString
51- deriving (Show , Eq )
52+ | SetGitHub (Response GitHub )
53+ | ErrorHandler (Response MisoString )
5254----------------------------------------------------------------------------
5355app :: App Model Action
5456app = (component emptyModel updateModel viewModel)
@@ -64,56 +66,58 @@ emptyModel :: Model
6466emptyModel = Model Nothing
6567----------------------------------------------------------------------------
6668updateModel :: Action -> Transition Model Action
67- updateModel FetchGitHub =
68- getJSON " https://api.github.com" [] SetGitHub ErrorHandler
69- updateModel (SetGitHub apiInfo) =
70- info ?= apiInfo
71- updateModel (ErrorHandler msg) =
72- io_ (consoleError msg)
69+ updateModel = \ case
70+ FetchGitHub ->
71+ getJSON " https://api.github.com/k" [] SetGitHub ErrorHandler
72+ SetGitHub Response {.. } ->
73+ info ?= body
74+ ErrorHandler Response {.. } ->
75+ io_ (consoleError body)
7376----------------------------------------------------------------------------
7477-- | View function, with routing
7578viewModel :: Model -> View Model Action
76- viewModel m = div_
79+ viewModel m =
80+ H. div_
7781 [ CSS. style_
7882 [ CSS. textAlign " center"
7983 , CSS. margin " 200px"
8084 ]
8185 ]
82- [ h1_
83- [ class_ $ pack " title"
86+ [ H. h1_
87+ [ P. class_ $ pack " title"
8488 ]
8589 [ " 🍜 Miso Fetch API"
8690 ]
8791 , optionalAttrs
88- button_
89- [ onClick FetchGitHub
90- , class_ (pack " button is-large is-outlined" )
92+ H. button_
93+ [ E. onClick FetchGitHub
94+ , P. class_ (pack " button is-large is-outlined" )
9195 ]
9296 (isJust (m ^. info))
93- [ disabled_
97+ [ P. disabled_
9498 ]
9599 [ " Fetch JSON from https://api.github.com"
96100 ]
97101 , case m ^. info of
98102 Nothing ->
99- div_
103+ H. div_
100104 []
101105 [ " No data"
102106 ]
103107 Just GitHub {.. } ->
104- table_
105- [ class_ " table is-striped" ]
106- [ thead_
108+ H. table_
109+ [ P. class_ " table is-striped" ]
110+ [ H. thead_
107111 []
108- [ tr_
112+ [ H. tr_
109113 []
110- [ th_
114+ [ H. th_
111115 []
112116 [ text " URLs"
113117 ]
114118 ]
115119 ]
116- , tbody_
120+ , H. tbody_
117121 []
118122 [ tr currentUserUrl
119123 , tr emojisUrl
@@ -128,7 +132,7 @@ viewModel m = div_
128132 ]
129133 where
130134 tr :: MisoString -> View Model action
131- tr x = tr_ [] [ td_ [] [ text x ] ]
135+ tr x = H. tr_ [] [ H. td_ [] [ text x ] ]
132136----------------------------------------------------------------------------
133137-- | Structure to capture the JSON returned from https://api.github.com
134138data GitHub
0 commit comments