Skip to content

Instantly share code, notes, and snippets.

@focusaurus focusaurus/sign-in.elm
Last active Nov 7, 2016

Embed
What would you like to do?
How to handle Http POST
module SignIn exposing (..)
import Html exposing (..)
import Html.App
import Http
import Json.Decode
import Task
type alias Model =
{ signInMessage : String
}
model : Model
model =
{ signInMessage = "Request not sent"
}
type Message
= SignInStart
| SignInSucceed String
| SignInFail Http.Error
view : Model -> Html Message
view model =
label [] [ text model.signInMessage ]
update : Message -> Model -> ( Model, Cmd Message )
update message model =
case message of
SignInStart ->
( { model | signInMessage = "Signing in..." }, Cmd.none )
SignInSucceed _ ->
( { model | signInMessage = "Sign in succeeded" }, Cmd.none )
SignInFail error ->
( { model | signInMessage = "Sign in failed" ++ toString error }, Cmd.none )
init =
( model, Task.perform SignInFail SignInSucceed (Http.post Json.Decode.succeed "sign-in" (Http.string """{"email":"foo@example.com","password":"bar"}""")) )
main : Program Never
main =
Html.App.program
{ init = init
, view = view
, update = update
, subscriptions = (\model -> Sub.none)
}
@focusaurus

This comment has been minimized.

Copy link
Owner Author

focusaurus commented Nov 7, 2016

Compiler error message is:

The 3rd argument to function `perform` is causing a mismatch.

45|              Task.perform SignInFail SignInSucceed (Http.post Json.Decode.value "sign-in" (Http.string """{"email":"foo@example.com","password":"bar"}""")
                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Function `perform` is expecting the 3rd argument to be:

    Task.Task Http.Error String

But it is:

    Task.Task Http.Error Json.Encode.Value

Hint: I always figure out the type of arguments from left to right. If an
argument is acceptable when I check it, I assume it is "correct" in subsequent
checks. So the problem may actually be in how previous arguments interact with
the 3rd.

Detected errors in 1 module.                                        
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.