decided to internalize modes inside Chest.elm

This commit is contained in:
2019-12-04 19:16:16 +01:00
parent 976fbe6b4b
commit b97be8c321
9 changed files with 332 additions and 242 deletions

View File

@@ -8,29 +8,16 @@ import Page.Chest as Chest exposing (Chest)
import Session exposing (Session, User(..))
import Set
import Table
import Utils exposing (renderIcon)
type alias Model =
{ session : Session
, loot : State
, mode : Mode
, chest : Chest
}
type Mode
= View Chest
| Grab Chest
mapChest fn mode =
case mode of
View chest ->
fn chest
Grab chest ->
fn chest
type State
= Loading
| LoadError String
@@ -52,7 +39,13 @@ getClaimsFromSession session =
init session =
( Model session Loading (View <| showClaims (getClaimsFromSession session)), Cmd.map Internal <| Api.fetchLoot GotLoot Api.OfGroup )
( Model session
Loading
(Tuple.first <|
Chest.update (Chest.intoMode (Chest.IntoViewWithClaims <| getClaimsFromSession session)) Chest.init
)
, Cmd.map Internal <| Api.fetchLoot GotLoot Api.OfGroup
)
view : Model -> ( Html Msg, List (Html Msg) )
@@ -79,21 +72,25 @@ view model =
Session.Player data ->
( True, data.player.id == 0 )
in
case model.mode of
View _ ->
if isPlayer && not isGroup then
button [ class "button", onClick IntoGrab ] [ text "Demander" ]
case ( model.chest, isPlayer && not isGroup ) of
( Chest.View _, True ) ->
button
[ class "button"
, onClick
(GotChestMsg
(Chest.intoMode <|
Chest.IntoClaim (getClaimsFromSession model.session)
)
)
]
[ text "Demander" ]
else
text ""
( Chest.Claim _, True ) ->
button [ class "button", onClick ConfirmGrab ] [ text "Valider" ]
Grab _ ->
if isPlayer && not isGroup then
button [ class "button", onClick ConfirmGrab ] [ text "Valider" ]
else
text ""
, [ mapChest (\c -> Chest.view c loot) model.mode
( _, _ ) ->
text ""
, [ Chest.view model.chest loot
|> Html.map (Internal << GotChestMsg)
]
)
@@ -107,34 +104,11 @@ type Msg
type InnerMsg
= GotLoot Api.ToChest (HttpResult Loot)
| GotChestMsg Chest.Msg
| IntoGrab
| IntoView
| ConfirmGrab
showClaims claims =
let
itemClaimed item =
List.any (\c -> c.loot_id == item.id) claims
in
Chest.show
(\item ->
[ p []
[ text <|
(if itemClaimed item then
"C"
else
""
)
++ item.name
]
]
)
refresh model =
{ model | mode = View <| showClaims (getClaimsFromSession model.session) }
update (Internal <| GotChestMsg (Chest.intoMode (Chest.IntoViewWithClaims (getClaimsFromSession model.session)))) model
update : Msg -> Model -> ( Model, Cmd Msg )
@@ -144,13 +118,13 @@ update msg model =
( model, Cmd.none )
Internal ConfirmGrab ->
case ( Session.user model.session, model.loot, model.mode ) of
( Session.Player data, Loaded loot, Grab chest ) ->
case ( Session.user model.session, model.loot, model.chest ) of
( Session.Player data, Loaded loot, Chest.Claim _ ) ->
( model
, Chest.confirmGrab
data.player.id
loot
chest
model.chest
|> Cmd.map Api
)
@@ -166,24 +140,9 @@ update msg model =
( { model | loot = LoadError "Le chargement a échoué" }, Cmd.none )
GotChestMsg chestMsg ->
mapChest (Chest.update chestMsg) model.mode
Chest.update chestMsg model.chest
|> updateChest model
IntoGrab ->
let
claimedIds =
case Session.user model.session of
Player data ->
List.map .loot_id data.claims
Admin ->
[]
in
( { model | mode = Grab <| Chest.initSelection (Just claimedIds) }, Cmd.none )
IntoView ->
( refresh model, Cmd.none )
_ ->
( model, Cmd.none )
)
@@ -191,11 +150,6 @@ update msg model =
updateChest model ( chestModel, chestCmd ) =
( case model.mode of
View _ ->
{ model | mode = View chestModel }
Grab _ ->
{ model | mode = Grab chestModel }
( { model | chest = chestModel }
, Cmd.map GotChestMsg chestCmd
)