Skip to content

A mirror of the LoadingState Gitlab repository.

License

Notifications You must be signed in to change notification settings

sli/loadingstate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LoadingState

A simple convenience type for tracking the loading state of an external resource.

Usage Example

type alias Model =
    { username : String
    , password : String
    , loggingIn : LoadingState Http.Error ()
    }


type Msg
    = Username String
    | Password String
    | SendLogin
    | RecievedLogin (Result Http.Error Token)


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        -- Snipped form handling code

        SendLogin ->
            let
                body =
                    encodeLogin model
                        |> Http.jsonBody
            in
            ( model
              -- Or however you send a request.
            , Api.login
                model.username
                model.password
                RecievedLogin
            )

        RecievedLogin result ->
            case result of
                Ok token ->
                    ( { model | loggingIn = Loaded () }, Api.storeToken token )

                Err err ->
                    ( { model | loggingIn = Failed err }, Cmd.none )

And then later in your view somewhere:

case model.loggingIn of
    Loading ->
        div [] [ text "Logging in..." ]

    Loaded _ ->
        div [] [ text "Logged in successfully." ]

    Failed err ->
        div [] [ text "Login failed." ]

    NotLoading ->
        div [] []

About

A mirror of the LoadingState Gitlab repository.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages