refactoring with one page per route (wip)

This commit is contained in:
2019-11-27 16:04:26 +01:00
parent 89b22bb07d
commit 32ff8bd2d6
7 changed files with 353 additions and 228 deletions

226
src/Page.elm Normal file
View File

@@ -0,0 +1,226 @@
module Page exposing (Page(..), PageMsg, gotoHome, gotoShop, update, view)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Page.Admin as Admin
import Page.Chest as Chest
import Page.Shop as Shop
import Session exposing (Session)
import Utils exposing (renderIcon)
type Page
= Chest Chest.Model
| Admin Admin.Model
| Shop Shop.Model
| About
| Loading
{-
type Page
= Dashboard Session
| GroupChest Session
| Shop Shop.Model
| NewLoot Session
| About
| Loading
-}
init =
Loading
mapMsg toMsg =
List.map (Html.map toMsg)
view page =
let
maybeSession =
case page of
Chest model ->
Just <| Session.getSession model
Admin model ->
Just <| Admin.getSession model
Shop model ->
Just <| Session.getSession model
_ ->
Nothing
( title, ( controls, content ) ) =
case page of
Chest chest ->
( "Lootalot", ( text "", mapMsg GotChestMsg <| Chest.view chest ) )
Admin admin ->
( "Administration", ( text "", mapMsg GotAdminMsg <| Admin.view admin ) )
Shop shop ->
( "Marchand"
, Shop.view shop
|> Tuple.mapBoth
(Html.map GotShopMsg)
(mapMsg GotShopMsg)
)
About ->
( "Loot-a-lot", ( text "", [ p [] [ text "A propos" ] ] ) )
Loading ->
( "Loot-a-lot", ( text "", [ p [] [ text "Chargement" ] ] ) )
navbarTitle =
case maybeSession of
Just session ->
case Session.user session of
Session.Player id ->
String.fromInt id
Session.Admin ->
"Administration"
Nothing ->
"Loot-a-lot"
navbarLinks =
case maybeSession of
Just session ->
case Session.user session of
Session.Player id ->
let
linkWithGem =
navLink "fas fa-gem"
in
[ navLink "fas fa-store-alt" "Marchand" "/marchand"
, if id == 0 then
linkWithGem "Nouveau loot" "/nouveau-tresor"
else
linkWithGem "Coffre de groupe" "/coffre"
]
Session.Admin ->
[ navLink "fas fa-store-alt" "Marchand" "/marchand" ]
Nothing ->
[]
in
( title
, { title = navbarTitle, links = navbarLinks }
, viewSessionBar maybeSession controls
:: content
)
viewSessionBar maybeSession controls =
controls
-- PLAYER BAR
{-
viewPlayerBar : Player -> List (Html Msg) -> Wealth.Model -> Html Msg
viewPlayerBar player actionControls wealthModel =
section [ class "hero is-dark is-bold" ]
[ div [ class "hero-body" ]
[ div [ class "level container is-mobile" ]
[ div [ class "level-left" ]
(Wealth.view player.wealth wealthModel
++ (if player.debt > 0 then
[ div [ class "level-item" ]
[ p [ class "heading is-size-4 has-text-danger" ]
[ text ("Dette : " ++ String.fromInt player.debt ++ "po") ]
]
]
else
[]
)
)
|> Html.map WealthMsg
, div [ class "level-right" ] actionControls
]
]
]
-}
navLink icon linkText url =
a [ class "navbar-item", href url ]
[ renderIcon { icon = icon, ratio = "1x", size = "medium" }
, span [] [ text linkText ]
]
-- UPDATE
--
type PageMsg
= GotChestMsg Chest.Msg
| GotAdminMsg Admin.Msg
| GotShopMsg Shop.Msg
update msg page =
case ( msg, page ) of
( GotChestMsg subMsg, Chest chest ) ->
Chest.update subMsg chest
|> updatePage Chest GotChestMsg
( GotAdminMsg subMsg, Admin admin ) ->
Admin.update subMsg admin
|> updatePage Admin GotAdminMsg
( GotShopMsg subMsg, Shop shop ) ->
Shop.update subMsg shop
|> updatePage Shop GotShopMsg
_ ->
( page, Cmd.none )
updatePage toPage toMsg ( subModel, subMsg ) =
( toPage subModel
, Cmd.map toMsg subMsg
)
-- CHANGE ROUTE
gotoHome session =
case Session.user session of
Session.Player _ ->
Chest.init session
|> updatePage Chest GotChestMsg
Session.Admin ->
Admin.init session
|> updatePage Admin GotAdminMsg
gotoShop session =
Shop.init session
|> updatePage Shop GotShopMsg
gotoGroupChest session =
()
gotoInventory session =
()