works on items selection
This commit is contained in:
112
src/Main.elm
112
src/Main.elm
@@ -30,6 +30,7 @@ type alias State =
|
||||
, route : Route
|
||||
, error : String
|
||||
, menuOpen : Bool
|
||||
, selectedItems : List Bool
|
||||
}
|
||||
|
||||
type alias Model =
|
||||
@@ -47,9 +48,15 @@ init flags url key =
|
||||
Just r -> r
|
||||
Nothing -> PlayerChest
|
||||
in
|
||||
( Model (State key route "" False) blankPlayer Nothing Nothing Nothing, initPlayer 0)
|
||||
( Model (State key route "" False []) blankPlayer Nothing Nothing Nothing, fetchInitialData 0)
|
||||
|
||||
|
||||
fetchInitialData : Int -> Cmd Msg
|
||||
fetchInitialData playerId =
|
||||
Cmd.batch [ initPlayer playerId
|
||||
, fetchShopInventory
|
||||
, fetchGroupLoot
|
||||
]
|
||||
-- PLAYER
|
||||
--
|
||||
type alias Player =
|
||||
@@ -117,7 +124,22 @@ lootDecoder =
|
||||
fetchLoot id =
|
||||
Http.get
|
||||
{ url = "http://localhost:8088/api/players/" ++ (String.fromInt id) ++ "/loot"
|
||||
, expect = Http.expectJson GotLoot (valueDecoder lootDecoder)}
|
||||
, expect = Http.expectJson (GotLoot OfPlayer) (valueDecoder lootDecoder)}
|
||||
|
||||
fetchShopInventory =
|
||||
Http.get
|
||||
{ url = "http://localhost:8088/api/items"
|
||||
, expect = Http.expectJson (GotLoot OfShop) (valueDecoder lootDecoder)}
|
||||
|
||||
fetchGroupLoot =
|
||||
Http.get
|
||||
{ url = "http://localhost:8088/api/players/0/loot"
|
||||
, expect = Http.expectJson (GotLoot OfGroup) (valueDecoder lootDecoder)}
|
||||
|
||||
type ToChest
|
||||
= OfPlayer
|
||||
| OfGroup
|
||||
| OfShop
|
||||
-- API Response
|
||||
--
|
||||
valueDecoder : Decoder a -> Decoder a
|
||||
@@ -131,7 +153,8 @@ type Msg
|
||||
| UrlChanged Url.Url
|
||||
| PlayerChanged Int
|
||||
| GotPlayer (Result Http.Error Player)
|
||||
| GotLoot (Result Http.Error Loot)
|
||||
| GotLoot ToChest (Result Http.Error Loot)
|
||||
| LootViewItemSwitched Int
|
||||
|
||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||
update msg model =
|
||||
@@ -153,7 +176,18 @@ update msg model =
|
||||
case route of
|
||||
Just page ->
|
||||
( let state = model.state in
|
||||
{ model | state = { state | route = page }}
|
||||
{ model
|
||||
| state = { state
|
||||
| route = page
|
||||
-- Reinitialize selectionList with url change
|
||||
, selectedItems =
|
||||
case page of
|
||||
GroupLoot -> List.map (\v -> False) (Maybe.withDefault [] model.groupLoot)
|
||||
PlayerChest -> List.map (\v -> False) (Maybe.withDefault [] model.loot)
|
||||
Merchant -> List.map (\v -> False) (Maybe.withDefault [] model.merchantItems)
|
||||
NewLoot -> []
|
||||
}
|
||||
}
|
||||
, case page of
|
||||
GroupLoot -> Cmd.none
|
||||
a -> Cmd.none
|
||||
@@ -176,10 +210,13 @@ update msg model =
|
||||
, Cmd.none
|
||||
)
|
||||
|
||||
GotLoot result ->
|
||||
GotLoot dest result ->
|
||||
case result of
|
||||
Ok loot ->
|
||||
( { model | loot = Just loot}
|
||||
( case dest of
|
||||
OfPlayer -> { model | loot = Just loot}
|
||||
OfGroup -> { model | groupLoot = Just loot}
|
||||
OfShop -> { model | merchantItems = Just loot}
|
||||
, Cmd.none
|
||||
)
|
||||
Err error ->
|
||||
@@ -187,6 +224,9 @@ update msg model =
|
||||
, Cmd.none
|
||||
)
|
||||
|
||||
LootViewItemSwitched idx ->
|
||||
( switchSelectionState idx model, Cmd.none )
|
||||
|
||||
-- ERRORS
|
||||
|
||||
setError : String -> Model -> Model
|
||||
@@ -204,6 +244,23 @@ printError error =
|
||||
Http.NetworkError -> "Le serveur ne répond pas"
|
||||
_ -> "Erreur inconnue"
|
||||
|
||||
-- STATE Utils
|
||||
|
||||
switchBooleanAt idx =
|
||||
(\i value ->
|
||||
if i == idx then
|
||||
not value
|
||||
else
|
||||
value
|
||||
)
|
||||
|
||||
switchSelectionState idx model =
|
||||
let
|
||||
state = model.state
|
||||
selection = model.state.selectedItems
|
||||
in
|
||||
{ model | state = { state | selectedItems = List.indexedMap (switchBooleanAt idx) selection } }
|
||||
|
||||
-- SUBSCRIPTIONS
|
||||
--
|
||||
subscriptions : Model -> Sub Msg
|
||||
@@ -224,7 +281,8 @@ view model =
|
||||
, article [class "section container"]
|
||||
(case model.state.route of
|
||||
PlayerChest ->
|
||||
[ p [] [text "Mon Coffre"]
|
||||
[ p [class "heading"] [text "Mon Coffre"]
|
||||
, viewSearchBar
|
||||
, viewLoot (Maybe.withDefault [] model.loot)
|
||||
]
|
||||
|
||||
@@ -234,7 +292,9 @@ view model =
|
||||
]
|
||||
|
||||
Merchant ->
|
||||
[ p [] [text "Acheter des objets"] ]
|
||||
[ p [] [text "Acheter des objets"]
|
||||
, viewLoot (Maybe.withDefault [] model.merchantItems)
|
||||
]
|
||||
|
||||
NewLoot ->
|
||||
[ p [] [text "Nouveau trésor :) "] ]
|
||||
@@ -248,12 +308,27 @@ view model =
|
||||
|
||||
viewLoot : Loot -> Html Msg
|
||||
viewLoot items =
|
||||
table []
|
||||
(List.map viewItemTableRow items)
|
||||
table [ class "table is-fullwidth is-striped"]
|
||||
([ thead [class "table-header"]
|
||||
[ th [] [text "Nom"] ]
|
||||
]
|
||||
++ List.indexedMap (False |> viewItemTableRow) items
|
||||
)
|
||||
|
||||
viewItemTableRow item =
|
||||
tr [class "table"]
|
||||
[ td [] [p [] [text item.name]]
|
||||
|
||||
viewItemTableRow selected idx item =
|
||||
tr [ classList [ ("is-selected", selected) ] ]
|
||||
[ td []
|
||||
[ label [ class "level checkbox", onClick (LootViewItemSwitched idx) ]
|
||||
[ div [ class "level-left" ]
|
||||
[ p [class "level-item"] [ text item.name ]
|
||||
]
|
||||
, div [ class "level-right" ]
|
||||
[ p [class "level-item"] [ text (String.fromInt item.base_price ++ "po") ]
|
||||
, input [class "checkbox level-item", type_ "checkbox"] []
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
-- DEBUG SECTION
|
||||
@@ -265,6 +340,7 @@ viewDebugSection model =
|
||||
, debugSwitchPlayers
|
||||
, p [class "panel-block has-text-danger"] [text model.state.error]
|
||||
, p [class "panel-block"] [text ("Route : " ++ Debug.toString model.state.route)]
|
||||
, p [class "panel-block"] [text ("Selection : " ++ Debug.toString model.state.selectedItems)]
|
||||
]
|
||||
|
||||
debugSwitchPlayers : Html Msg
|
||||
@@ -279,7 +355,7 @@ debugSwitchPlayers =
|
||||
|
||||
viewHeaderBar : Model -> Html Msg
|
||||
viewHeaderBar model =
|
||||
nav [ class "navbar", class "is-info" ]
|
||||
nav [ class "navbar container", class "is-info" ]
|
||||
[ div [ class "navbar-brand" ]
|
||||
[ a [ class "navbar-item", href "/"]
|
||||
[ text model.player.name ]
|
||||
@@ -310,7 +386,7 @@ viewHeaderBar model =
|
||||
|
||||
viewPlayerBar : Player -> Route -> Html Msg
|
||||
viewPlayerBar player route =
|
||||
section [ class "level is-mobile box" ]
|
||||
section [ class "level container is-mobile box" ]
|
||||
[ div [class "level-left"]
|
||||
([div [ class "level-item" ]
|
||||
[ span [ class "icon is-large" ]
|
||||
@@ -358,6 +434,12 @@ showWealthField name value =
|
||||
, p [class "heading"] [text name]
|
||||
]
|
||||
|
||||
-- Search Bar
|
||||
|
||||
viewSearchBar : Html Msg
|
||||
viewSearchBar =
|
||||
input [class "input"] []
|
||||
|
||||
---
|
||||
-- ROUTES
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user