Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
executeWith : String -> ScoresTagger msg -> Score -> Cmd msg
executeWith uri tagger score =
let
tagHighScores = highScoreTagger tagger
storeScore = storeScoreTask uri
getScores = getScoresTask uri
in
storeScore score
|> Task.andThen (\_ -> getScores)
|> Task.attempt tagHighScores
storeScoreTask : String -> Score -> Task Http.Error ()
storeScoreTask uri score =
Http.post uri (Http.jsonBody <| encode score) (Json.succeed ())
|> Http.toTask
|> Task.onError (\_ -> Task.succeed ())
getScoresTask : String -> Task Http.Error (List Score)
getScoresTask uri =
Http.get uri (Json.list scoreDecoder)
|> Http.toTask
highScoreTagger : ScoresTagger msg -> Result Http.Error (List Score) -> msg
highScoreTagger tagger result =
case result of
Ok scores ->
tagger scores
Err _ ->
tagger []
scoreDecoder : Decoder Score
decoder =
Json.field "score" Json.int
encode : Score -> Value
encode score =
Encode.object
[ ( "score", Encode.int score )
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment