puts group chest almost back up
This commit is contained in:
@@ -1,31 +1,48 @@
|
||||
module Page.GroupChest exposing (..)
|
||||
module Page.GroupChest exposing (Model, Msg(..), init, update, view)
|
||||
|
||||
import Api exposing (HttpResult, Loot)
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
import Html.Events exposing (..)
|
||||
import Page.Chest as Chest exposing (Chest)
|
||||
import Session exposing (Session)
|
||||
import Table
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ session : Session
|
||||
, state : State
|
||||
, loot : State
|
||||
, mode : Mode
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
| View Loot
|
||||
| Loaded Loot
|
||||
|
||||
|
||||
init session =
|
||||
( Model session Loading, Api.fetchLoot GotLoot Api.OfGroup )
|
||||
( Model session Loading (View Chest.init), Cmd.map Internal <| Api.fetchLoot GotLoot Api.OfGroup )
|
||||
|
||||
|
||||
view : Model -> ( Html Msg, List (Html Msg) )
|
||||
view model =
|
||||
case model.state of
|
||||
case model.loot of
|
||||
Loading ->
|
||||
( text ""
|
||||
, [ p [ class "title" ] [ text "loading..." ] ]
|
||||
@@ -36,29 +53,101 @@ view model =
|
||||
, [ p [ class "has-text-danger" ] [ text <| "Error : " ++ error ] ]
|
||||
)
|
||||
|
||||
View loot ->
|
||||
( case Session.user model.session of
|
||||
Session.Admin ->
|
||||
text ""
|
||||
Loaded loot ->
|
||||
( Html.map Internal <|
|
||||
case model.mode of
|
||||
View _ ->
|
||||
case Session.user model.session of
|
||||
Session.Admin ->
|
||||
text ""
|
||||
|
||||
Session.Player p _ _ ->
|
||||
if p.id == 0 then
|
||||
button [ class "button" ] [ text "Vendre" ]
|
||||
Session.Player p _ _ ->
|
||||
if p.id == 0 then
|
||||
text ""
|
||||
|
||||
else
|
||||
button [ class "button" ] [ text "Demander" ]
|
||||
, [ Table.view Table.name loot ]
|
||||
else
|
||||
button [ class "button", onClick IntoGrab ] [ text "Demander" ]
|
||||
|
||||
Grab _ ->
|
||||
case Session.user model.session of
|
||||
Session.Admin ->
|
||||
text ""
|
||||
|
||||
Session.Player p _ _ ->
|
||||
if p.id == 0 then
|
||||
text ""
|
||||
|
||||
else
|
||||
button [ class "button", onClick ConfirmGrab ] [ text "Valider" ]
|
||||
, [ mapChest (\c -> Chest.view c loot) model.mode
|
||||
|> Html.map (Internal << GotChestMsg)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
type Msg
|
||||
= Api Api.Msg
|
||||
| Internal InnerMsg
|
||||
|
||||
|
||||
type InnerMsg
|
||||
= GotLoot Api.ToChest (HttpResult Loot)
|
||||
| GotChestMsg Chest.Msg
|
||||
| IntoGrab
|
||||
| IntoView
|
||||
| ConfirmGrab
|
||||
|
||||
|
||||
update : Msg -> Model -> ( Model, Cmd Msg )
|
||||
update msg model =
|
||||
case msg of
|
||||
GotLoot _ (Ok loot) ->
|
||||
( { model | state = View loot }, Cmd.none )
|
||||
Api apiMsg ->
|
||||
( model, Cmd.none )
|
||||
|
||||
GotLoot _ (Err _) ->
|
||||
( { model | state = LoadError "Le chargement a échoué" }, Cmd.none )
|
||||
Internal ConfirmGrab ->
|
||||
case ( Session.user model.session, model.loot, model.mode ) of
|
||||
( Session.Player player _ _, Loaded loot, Grab chest ) ->
|
||||
( { model | mode = View Chest.init }
|
||||
, Chest.confirmGrab
|
||||
player.id
|
||||
loot
|
||||
chest
|
||||
|> Cmd.map Api
|
||||
)
|
||||
|
||||
_ ->
|
||||
( model, Cmd.none )
|
||||
|
||||
Internal innerMsg ->
|
||||
(case innerMsg of
|
||||
GotLoot _ (Ok loot) ->
|
||||
( { model | loot = Loaded loot }, Cmd.none )
|
||||
|
||||
GotLoot _ (Err _) ->
|
||||
( { model | loot = LoadError "Le chargement a échoué" }, Cmd.none )
|
||||
|
||||
GotChestMsg chestMsg ->
|
||||
mapChest (Chest.update chestMsg) model.mode
|
||||
|> updateChest model
|
||||
|
||||
IntoGrab ->
|
||||
( { model | mode = Grab Chest.initSelection }, Cmd.none )
|
||||
|
||||
IntoView ->
|
||||
( { model | mode = View Chest.init }, Cmd.none )
|
||||
|
||||
_ ->
|
||||
( model, Cmd.none )
|
||||
)
|
||||
|> Tuple.mapSecond (Cmd.map Internal)
|
||||
|
||||
|
||||
updateChest model ( chestModel, chestCmd ) =
|
||||
( case model.mode of
|
||||
View _ ->
|
||||
{ model | mode = View chestModel }
|
||||
|
||||
Grab _ ->
|
||||
{ model | mode = Grab chestModel }
|
||||
, Cmd.map GotChestMsg chestCmd
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user