Commit e2362f6a authored by julien dehos's avatar julien dehos

xhr

parent e3b0a54b
......@@ -8,28 +8,31 @@ module Common where
import Data.Aeson (FromJSON, ToJSON)
import GHC.Generics (Generic)
import Miso
import qualified Network.URI as Network
import qualified Miso.String as MS
-- import qualified Network.URI as Network
import Servant.API
import Servant.Links
-- import Servant.Links
data Hero = Hero
{ heroName :: String
, heroImage :: String
} deriving (Eq, Generic, Show)
{ heroName :: MS.MisoString
, heroImage :: MS.MisoString
} deriving (Eq, Generic)
instance FromJSON Hero
instance ToJSON Hero
newtype Model = Model
{ heroes_ :: [Hero]
} deriving (Eq, Show)
} deriving (Eq)
initialModel :: Model
initialModel = Model []
data Action
= NoOp
deriving (Eq, Show)
| SetHeroes [Hero]
| FetchHeroes
deriving (Eq)
type HeroesApi = "heroes" :> Get '[JSON] [Hero]
......@@ -38,9 +41,15 @@ type ClientRoutes = HomeRoute
type HomeRoute = View Action
homeRoute :: Model -> View Action
homeRoute _ = div_
homeRoute m = div_
[]
[ h1_ [] [ text "miso-xhr" ]
, ul_ [] (map fmtHero $ heroes_ m)
, p_ [] [ a_ [href_ "heroes" ] [ text "JSON data" ] ]
]
where fmtHero h = li_ []
[ text $ heroName h
, br_ []
, img_ [ src_ $ MS.concat ["/static/", heroImage h] ]
]
{-# LANGUAGE OverloadedStrings #-}
import Common
import JavaScript.Web.XMLHttpRequest
import Miso
import Miso.String
import Common
import Data.Aeson (decodeStrict)
import Data.Maybe (fromJust, fromMaybe)
import JavaScript.Web.XMLHttpRequest
import Miso
main :: IO ()
main = miso $ const App
{ initialAction = NoOp
{ initialAction = FetchHeroes
, model = initialModel
, update = updateModel
, view = homeRoute
......@@ -18,4 +19,10 @@ main = miso $ const App
updateModel :: Action -> Model -> Effect Action Model
updateModel NoOp m = noEff m
updateModel (SetHeroes heroes) m = noEff m { heroes_ = heroes }
updateModel FetchHeroes m = m <# do SetHeroes <$> xhrHeroes
xhrHeroes :: IO [Hero]
xhrHeroes = fromMaybe [] . decodeStrict . fromJust . contents <$> xhrByteString req
where req = Request GET "heroes" Nothing [] False NoData
static/scoobydoo.png

48.2 KB | W: | H:

static/scoobydoo.png

24 KB | W: | H:

static/scoobydoo.png
static/scoobydoo.png
static/scoobydoo.png
static/scoobydoo.png
  • 2-up
  • Swipe
  • Onion skin
static/spongebob.png

33.4 KB | W: | H:

static/spongebob.png

22.3 KB | W: | H:

static/spongebob.png
static/spongebob.png
static/spongebob.png
static/spongebob.png
  • 2-up
  • Swipe
  • Onion skin
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment