Getting the "Loading" status from the Elm RemoteData package
Here is the simplest Elm program I can think of that uses the RemoteData package to better simulate server responses:
module App exposing (..)
import Http exposing (..)
import Html exposing (..)
import Html.Events exposing (..)
import RemoteData exposing (..)
import Debug exposing (..)
type alias Model = WebData String
init : (Model, Cmd Msg)
init = (RemoteData.NotAsked, Cmd.none)
type Msg = Ask | OnUpdate (WebData String)
view : Model -> Html Msg
view model =
div []
[ button [ onClick Ask ] [ text "ask" ]
, text (toString model)
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
OnUpdate response -> (log (toString response) response, Cmd.none)
Ask -> (model,
Http.getString "https://api.ipify.org"
|> RemoteData.sendRequest
|> Cmd.map OnUpdate)
subscriptions : Model -> Sub Msg
subscriptions model = Sub.none
main : Program Never Model Msg
main =
program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
Despite the fact that the package has a status NotAsked
, Loading
, Success
and Failure
I never see the status Loading
. When does this package ever send this status and how do I use it?
+3
source to share
1 answer
Actually, it is your task to put the loading status into the model when sending the command:
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
OnUpdate response -> (log (toString response) response, Cmd.none)
Ask -> (RemoteData.Loading,
Http.getString "https://api.ipify.org"
|> RemoteData.sendRequest
|> Cmd.map OnUpdate)
See also an example here: https://github.com/krisajenkins/remotedata/blob/4.3.0/src/RemoteData.elm#L72
+4
source to share