Elm: How to define a function that executes a query with a message as a parameter

In Elm 0.18, how can I define a function that executes a query that takes a message as a parameter for the update function when receiving a response?

The following code:

mypost : String -> String -> msg -> Cmd msg
mypost url csrf mymsg =
    Http.request
        { method = "POST"
        , headers = [ Http.header "X-CSRFToken" csrf ]
        , url = url
        , body = Http.emptyBody
        , expect = Http.expectJson mydecoder
        , timeout = Nothing
        , withCredentials = False
        }
        |> RemoteData.sendRequest
        |> Cmd.map mymsg

not compiling:

The function argument mapcauses a mismatch.

124 | Cmd.map m ^ The function mapexpects the argument to be:

a -> msg

But this:

msg

Hint: It looks like the function needs another argument.

What if msg accepts a parameter? eg:

|> Cmd.map (mymsg myParamTomymsg)

This led me to a lot of code duplication, there must be a better way ...

+4
source share
1 answer

, Http. Elm, .

TL; DR

mymsg a -> msg, a - , , mydecoder

mypost : String -> String -> (a -> msg) -> Cmd msg

a .

, , , Http.send, , .

Union, Msg - , NoOp Update - , Msg.

type Msg
    = NoOp
    | Update String

Update "Hello" , Msg, "Hello" Update Http.send, , ,

Update String -> Msg

Http.send:

send : (Result Error a -> msg) -> Request a -> Cmd msg

GET:

get : Int -> Difficulty -> (Result Error TriviaResult -> msg) -> Cmd msg
get amount difficulty msg =
    Http.get (triviaRequestUrl amount difficulty) decoder
        |> Http.send msg

remotedata

:

myget1 : (WebData HttpBin -> msg) -> Cmd msg

, .

+2

Source: https://habr.com/ru/post/1684532/


All Articles