{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE OverloadedStrings #-} -- | App update logic module Biz.Ibb.Move ( module Core , move -- * Server interactions , fetchPeople ) where import Alpha import Data.Aeson import Biz.Ibb.Core as Core import JavaScript.Web.XMLHttpRequest (Request(..), Method(GET), RequestData(NoData), contents, xhrByteString) import Miso import Miso.String import Network.RemoteData move :: Action -> Model -> Effect Action Model move Nop m = noEff m move (HandleRoute u) m = m { uri = u } <# pure Nop move (ChangeRoute u) m = m <# do pushURI u >> pure Nop move FetchPeople m = m <# do SetPeople /@ fetchPeople move (SetPeople ps) m = noEff m { people = ps } fetchPeople :: IO (WebData [Core.Person]) fetchPeople = do mjson <- contents /@ xhrByteString req case mjson of Nothing -> pure $ Failure "could not read from server" Just a -> pure $ either (Failure . ms) Network.RemoteData.Success $ eitherDecodeStrict a where req = Request { reqMethod = GET , reqURI = "/api/people" -- FIXME: can replace this hardcoding with a function? , reqLogin = Nothing , reqHeaders = [] , reqWithCredentials = False , reqData = NoData }