makes selection work

This commit is contained in:
2019-11-02 21:55:55 +01:00
parent 68c8761c3d
commit eb2ee54add
2 changed files with 200 additions and 141 deletions

View File

@@ -10,6 +10,7 @@ import Html.Events exposing (..)
import Http
import Json.Decode exposing (Decoder, field, list, string, int)
import Url.Parser as P exposing (Parser, (</>), oneOf, s)
import Set exposing (Set)
-- Main
main : Program () Model Msg
@@ -25,12 +26,16 @@ main =
-- Model
type alias Selection = Set Int
emptySelection = []
type alias State =
{ navKey : Nav.Key
, route : Route
, error : String
, menuOpen : Bool
, selectedItems : List Bool
, selectedItems : Maybe Selection
}
type alias Model =
@@ -48,7 +53,7 @@ init flags url key =
Just r -> r
Nothing -> PlayerChest
in
( Model (State key route "" False []) blankPlayer Nothing Nothing Nothing, fetchInitialData 0)
( Model (State key route "" False Nothing) blankPlayer Nothing Nothing Nothing, fetchInitialData 0)
fetchInitialData : Int -> Cmd Msg
@@ -175,18 +180,11 @@ update msg model =
in
case route of
Just page ->
( let state = model.state in
{ 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 -> []
}
( let
state = model.state
in
{ model | state =
{ state | route = page , selectedItems = Just Set.empty}
}
, case page of
GroupLoot -> Cmd.none
@@ -224,8 +222,13 @@ update msg model =
, Cmd.none
)
LootViewItemSwitched idx ->
( switchSelectionState idx model, Cmd.none )
LootViewItemSwitched id ->
let
state = model.state
in
( { model | state =
{ state | selectedItems = Debug.log "new selection" (switchSelectionState id state.selectedItems) }}
, Cmd.none )
-- ERRORS
@@ -246,20 +249,14 @@ printError error =
-- 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 } }
switchSelectionState : Int -> Maybe Selection -> Maybe Selection
switchSelectionState id selection =
case selection of
Just s ->
Just (case Set.member id s of
True -> Set.remove id s
False -> Set.insert id s)
Nothing -> Debug.log "ignore switchSelectionState" Nothing
-- SUBSCRIPTIONS
--
@@ -283,17 +280,17 @@ view model =
PlayerChest ->
[ p [class "heading"] [text "Mon Coffre"]
, viewSearchBar
, viewLoot (Maybe.withDefault [] model.loot)
, viewLoot (Maybe.withDefault [] model.loot) model.state.selectedItems
]
GroupLoot ->
[ p [] [text "Coffre de groupe"]
, viewLoot (Maybe.withDefault [] model.groupLoot)
, viewLoot (Maybe.withDefault [] model.groupLoot) model.state.selectedItems
]
Merchant ->
[ p [] [text "Acheter des objets"]
, viewLoot (Maybe.withDefault [] model.merchantItems)
, viewLoot (Maybe.withDefault [] model.merchantItems) model.state.selectedItems
]
NewLoot ->
@@ -306,26 +303,37 @@ view model =
-- LOOT Views
viewLoot : Loot -> Html Msg
viewLoot items =
isSelected id selection =
Set.member id selection
viewLoot : Loot -> Maybe Selection -> Html Msg
viewLoot items selection =
table [ class "table is-fullwidth is-striped"]
([ thead [class "table-header"]
[ th [] [text "Nom"] ]
]
++ List.indexedMap (False |> viewItemTableRow) items
++ List.map (viewItemTableRow selection) items
)
viewItemTableRow selected idx item =
viewItemTableRow selection item =
let
(canSelect, selected) =
case selection of
Just s ->
(True, isSelected item.id s)
Nothing ->
(False, False)
in
tr [ classList [ ("is-selected", selected) ] ]
[ td []
[ label [ class "level checkbox", onClick (LootViewItemSwitched idx) ]
[ label [ class "level checkbox" ]
[ 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"] []
, input [class "checkbox level-item", type_ "checkbox", onCheck (\v -> LootViewItemSwitched item.id)] []
]
]
]