works on add from list

This commit is contained in:
2019-11-14 16:25:36 +01:00
parent 0d5cc365fc
commit 1eb4fdc188
2 changed files with 147 additions and 21 deletions

View File

@@ -11,6 +11,7 @@ module Api exposing
, Update(..)
, Wealth
, blankPlayer
, checkList
, confirmAction
, fetchClaims
, fetchLoot
@@ -30,8 +31,13 @@ type alias HttpResult a =
-- Format of the server's response
type Value
= Text String
| Items Loot
type alias Response =
{ value : Maybe String
{ value : Maybe Value
, notification : Maybe String
, updates : Maybe (List Update)
, errors : Maybe String
@@ -188,6 +194,48 @@ fetchLoot url toMsg =
}
checkList : (Loot -> Maybe String -> msg) -> List String -> Cmd msg
checkList toMsg itemList =
let
parseResponse : Result Http.Error Response -> msg
parseResponse response =
case response of
Ok r ->
let
items =
case r.value of
Just (Items loot) ->
loot
_ ->
[]
errors =
case r.errors of
Nothing ->
Nothing
Just e ->
if e == "" then
Nothing
else
Just e
in
toMsg (Debug.log "CheckList, got items" items) errors
Err e ->
toMsg [] <| Just (printError e)
in
Http.post
{ url = "http://localhost:8088/api/items"
, body =
E.list (\t -> E.string t) itemList
|> Http.jsonBody
, expect = Http.expectJson parseResponse (apiResponseDecoder lootDecoder)
}
-- CLAIMS
-- API Response
@@ -217,10 +265,14 @@ updatesDecoder =
]
apiResponseDecoder : Decoder Response
apiResponseDecoder =
-- TODO: add a valueDecoder to get a proper value out of Json
apiResponseDecoder : Decoder Value -> Decoder Response
apiResponseDecoder toValue =
D.map4 Response
(D.maybe (field "value" string))
(D.maybe (field "value" toValue))
(D.maybe (field "notification" string))
(D.maybe (field "updates" (D.list updatesDecoder)))
(D.maybe (field "errors" string))
@@ -232,7 +284,7 @@ undoLastAction id =
, method = "DELETE"
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson GotActionResult apiResponseDecoder
, expect = Http.expectJson GotActionResult (apiResponseDecoder string)
, timeout = Nothing
, tracker = Nothing
}
@@ -318,7 +370,7 @@ confirmAction id data =
, headers = []
, url = endpoint
, body = Http.jsonBody <| buildPayload data
, expect = Http.expectJson GotActionResult apiResponseDecoder
, expect = Http.expectJson GotActionResult (apiResponseDecoder string)
, timeout = Nothing
, tracker = Nothing
}