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(..) , Update(..)
, Wealth , Wealth
, blankPlayer , blankPlayer
, checkList
, confirmAction , confirmAction
, fetchClaims , fetchClaims
, fetchLoot , fetchLoot
@@ -30,8 +31,13 @@ type alias HttpResult a =
-- Format of the server's response -- Format of the server's response
type Value
= Text String
| Items Loot
type alias Response = type alias Response =
{ value : Maybe String { value : Maybe Value
, notification : Maybe String , notification : Maybe String
, updates : Maybe (List Update) , updates : Maybe (List Update)
, errors : Maybe String , 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 -- CLAIMS
-- API Response -- 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.map4 Response
(D.maybe (field "value" string)) (D.maybe (field "value" toValue))
(D.maybe (field "notification" string)) (D.maybe (field "notification" string))
(D.maybe (field "updates" (D.list updatesDecoder))) (D.maybe (field "updates" (D.list updatesDecoder)))
(D.maybe (field "errors" string)) (D.maybe (field "errors" string))
@@ -232,7 +284,7 @@ undoLastAction id =
, method = "DELETE" , method = "DELETE"
, headers = [] , headers = []
, body = Http.emptyBody , body = Http.emptyBody
, expect = Http.expectJson GotActionResult apiResponseDecoder , expect = Http.expectJson GotActionResult (apiResponseDecoder string)
, timeout = Nothing , timeout = Nothing
, tracker = Nothing , tracker = Nothing
} }
@@ -318,7 +370,7 @@ confirmAction id data =
, headers = [] , headers = []
, url = endpoint , url = endpoint
, body = Http.jsonBody <| buildPayload data , body = Http.jsonBody <| buildPayload data
, expect = Http.expectJson GotActionResult apiResponseDecoder , expect = Http.expectJson GotActionResult (apiResponseDecoder string)
, timeout = Nothing , timeout = Nothing
, tracker = Nothing , tracker = Nothing
} }

View File

@@ -36,6 +36,7 @@ type alias State =
, autoComplete : Loot , autoComplete : Loot
, newItem : Maybe Item , newItem : Maybe Item
, sourceName : Maybe String , sourceName : Maybe String
, itemList : Maybe (List String)
-- Fetched on init -- Fetched on init
, player : Api.Player , player : Api.Player
@@ -72,6 +73,7 @@ init (Player navKey playerId) =
[] []
Nothing Nothing
Nothing Nothing
Nothing
Api.blankPlayer Api.blankPlayer
[] []
[] []
@@ -92,17 +94,28 @@ init (Player navKey playerId) =
) )
viewNotification : Maybe String -> Html Msg viewNotification : Model -> Html Msg
viewNotification notification = viewNotification model =
case notification of div []
Just t -> [ case model.state.notification of
div [ class "notification is-success is-marginless" ] Just t ->
[ button [ class "delete", onClick ClearNotification ] [] div [ class "notification is-success" ]
, text t [ button [ class "delete", onClick ClearNotification ] []
] , text t
]
Nothing -> Nothing ->
text "" 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 , viewPlayerBar model.state.player renderControls
, main_ , main_
[ class "container" ] [ class "container" ]
[ viewNotification model.state.notification [ viewNotification model
, article , article
[ class "section" ] [ class "section" ]
(case model.state.mode of (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", classList [ ( "is-active", isActive ) ] ]
[ div [ class "modal-background" ] [] [ div [ class "modal-background" ] []
, div [ class "modal-card" ] , div [ class "modal-card" ]
[ header [ class "modal-card-head" ] [ p [ class "modal-card-title" ] [ text "Liste d'objets" ] ] [ header [ class "modal-card-head" ] [ p [ class "modal-card-title" ] [ text "Liste d'objets" ] ]
, div [ class "modal-card-body" ] , div [ class "modal-card-body" ]
[ textarea [ class "textarea" ] [] [ textarea [ class "textarea", value (String.join "\n" itemList), onInput FromListChanged ] []
] ]
, div [ class "modal-card-foot" ] , div [ class "modal-card-foot" ]
[ button [ class "button" ] [ text "Ok" ] [ button [ class "button", onClick FromListConfirmed ] [ text "Ok" ]
, button [ class "button" ] [ text "Annuler" ] , button [ class "button" ] [ text "Annuler" ]
] ]
] ]
@@ -532,6 +545,14 @@ viewAddLoot model =
Nothing -> Nothing ->
Item 0 "" 0 Item 0 "" 0
itemList =
case model.state.itemList of
Just items ->
items
Nothing ->
[]
sourceName = sourceName =
case model.state.sourceName of case model.state.sourceName of
Just name -> Just name ->
@@ -557,7 +578,7 @@ viewAddLoot model =
newItem.id == 0 newItem.id == 0
in in
div [ class "box is-primary" ] div [ class "box is-primary" ]
[ fromListModal showModal [ fromListModal showModal itemList
, div [ class "field is-horizontal" ] , div [ class "field is-horizontal" ]
[ div [ class "field-label is-medium" ] [ label [ class "label" ] [ text "Source du loot" ] ] [ div [ class "field-label is-medium" ] [ label [ class "label" ] [ text "Source du loot" ] ]
, div [ class "field-body" ] , div [ class "field-body" ]
@@ -729,6 +750,9 @@ type Msg
| SourceNameChanged String | SourceNameChanged String
| SetNewItem Item | SetNewItem Item
| OpenModal | OpenModal
| FromListChanged String
| FromListConfirmed
| NewItemsFromList Loot (Maybe String)
insensitiveContains : String -> String -> Bool insensitiveContains : String -> String -> Bool
@@ -739,6 +763,56 @@ insensitiveContains substring string =
update : Msg -> Model -> ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg )
update msg model = update msg model =
case msg of 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 -> OpenModal ->
let let
state = state =