Commit 52e0cd68 authored by julien dehos's avatar julien dehos

v2 (links) wip

parent 47fb80ce
......@@ -15,6 +15,8 @@ executable server
build-depends: aeson,
base,
binary,
http-types,
http-media,
lucid,
miso,
network-uri,
......@@ -36,6 +38,8 @@ executable client
build-depends: aeson,
base,
ghcjs-base,
http-types,
http-media,
miso,
network-uri,
servant,
......
......@@ -11,10 +11,11 @@ import Data.Proxy (Proxy(..))
import GHC.Generics (Generic)
import Miso
import qualified Miso.String as MS
-- import qualified Network.URI as Network
import Network.HTTP.Types.Method (Method)
import qualified Network.URI as Network
import Servant.API
import Servant.Client
-- import Servant.Links
import Servant.Links
data Hero = Hero
{ heroName :: MS.MisoString
......@@ -46,16 +47,25 @@ homeRoute m = div_
[]
[ h1_ [] [ text "miso-xhr" ]
, ul_ [] (map fmtHero $ heroes_ m)
, p_ [] [ a_ [href_ "heroes" ] [ text "JSON data" ] ]
, p_ [] [ a_ [href_ $ MS.ms $ show linkHeroes] [ text "JSON data" ] ]
]
where fmtHero h = li_ []
[ text $ heroName h
, br_ []
, img_ [ src_ $ MS.concat ["/static/", heroImage h] ]
, img_ [ src_ $ MS.concat [MS.ms $ show linkStatic, heroImage h] ]
]
type StaticApi = "static" :> Raw
type HeroesApi = "heroes" :> Get '[JSON] [Hero]
type PublicApi = StaticApi :<|> HeroesApi
fetchStatic :: Method -> ClientM Response
fetchHeroes :: ClientM [Hero]
fetchHeroes = client (Proxy @HeroesApi)
fetchStatic :<|> fetchHeroes = client (Proxy @PublicApi)
linkStatic :: Network.URI
linkStatic = linkURI $ safeLink (Proxy @PublicApi) (Proxy @StaticApi)
linkHeroes :: Network.URI
linkHeroes = linkURI $ safeLink (Proxy @PublicApi) (Proxy @HeroesApi)
......@@ -26,4 +26,5 @@ xhrHeroes :: IO [Hero]
xhrHeroes =
fromMaybe [] . decodeStrict . fromJust . contents <$> xhrByteString req
where req = Request GET "heroes" Nothing [] False NoData
-- TODO link
......@@ -16,7 +16,7 @@ main :: IO ()
main = run 3000 $ logStdout $ serve (Proxy @ServerApi) server
type ServerApi
= "static" :> Raw
= StaticApi
:<|> HeroesApi
:<|> ToServerRoutes ClientRoutes HtmlPage Action
......
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