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
}

View File

@@ -36,6 +36,7 @@ type alias State =
, autoComplete : Loot
, newItem : Maybe Item
, sourceName : Maybe String
, itemList : Maybe (List String)
-- Fetched on init
, player : Api.Player
@@ -72,6 +73,7 @@ init (Player navKey playerId) =
[]
Nothing
Nothing
Nothing
Api.blankPlayer
[]
[]
@@ -92,17 +94,28 @@ init (Player navKey playerId) =
)
viewNotification : Maybe String -> Html Msg
viewNotification notification =
case notification of
viewNotification : Model -> Html Msg
viewNotification model =
div []
[ case model.state.notification of
Just t ->
div [ class "notification is-success is-marginless" ]
div [ class "notification is-success" ]
[ button [ class "delete", onClick ClearNotification ] []
, text t
]
Nothing ->
text ""
, case model.state.error of
Just e ->
div [ class "notification is-danger" ]
[ button [ class "delete", onClick ClearNotification ] []
, text e
]
Nothing ->
text ""
]
@@ -313,7 +326,7 @@ view model =
, viewPlayerBar model.state.player renderControls
, main_
[ class "container" ]
[ viewNotification model.state.notification
[ viewNotification model
, article
[ class "section" ]
(case model.state.mode of
@@ -470,16 +483,16 @@ viewItemTableRow isSelected canSelect rowRenderer item =
--
fromListModal isActive =
fromListModal isActive itemList =
div [ class "modal", classList [ ( "is-active", isActive ) ] ]
[ div [ class "modal-background" ] []
, div [ class "modal-card" ]
[ header [ class "modal-card-head" ] [ p [ class "modal-card-title" ] [ text "Liste d'objets" ] ]
, div [ class "modal-card-body" ]
[ textarea [ class "textarea" ] []
[ textarea [ class "textarea", value (String.join "\n" itemList), onInput FromListChanged ] []
]
, div [ class "modal-card-foot" ]
[ button [ class "button" ] [ text "Ok" ]
[ button [ class "button", onClick FromListConfirmed ] [ text "Ok" ]
, button [ class "button" ] [ text "Annuler" ]
]
]
@@ -532,6 +545,14 @@ viewAddLoot model =
Nothing ->
Item 0 "" 0
itemList =
case model.state.itemList of
Just items ->
items
Nothing ->
[]
sourceName =
case model.state.sourceName of
Just name ->
@@ -557,7 +578,7 @@ viewAddLoot model =
newItem.id == 0
in
div [ class "box is-primary" ]
[ fromListModal showModal
[ fromListModal showModal itemList
, div [ class "field is-horizontal" ]
[ div [ class "field-label is-medium" ] [ label [ class "label" ] [ text "Source du loot" ] ]
, div [ class "field-body" ]
@@ -729,6 +750,9 @@ type Msg
| SourceNameChanged String
| SetNewItem Item
| OpenModal
| FromListChanged String
| FromListConfirmed
| NewItemsFromList Loot (Maybe String)
insensitiveContains : String -> String -> Bool
@@ -739,6 +763,56 @@ insensitiveContains substring string =
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NewItemsFromList newLoot maybeErrors ->
let
state =
model.state
error =
case maybeErrors of
Just errors ->
(String.lines errors
|> String.join ""
)
++ "n'ont pas pu être ajoutés.\n Faites le manuellement !"
|> Just
Nothing ->
Nothing
in
( { model
| state =
{ state
| itemList = Nothing
, newLoot = newLoot ++ model.state.newLoot
, error = error
}
}
, Cmd.none
)
FromListChanged newText ->
let
state =
model.state
itemList =
String.lines newText
in
( { model | state = { state | itemList = Just itemList } }
, Cmd.none
)
FromListConfirmed ->
let
state =
model.state
itemList =
Maybe.withDefault [] model.state.itemList
in
( { model | state = { state | showModal = False } }, Api.checkList NewItemsFromList itemList )
OpenModal ->
let
state =