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

@@ -5,16 +5,17 @@ import Api.Player
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Page.Dashboard as Dashboard
import Page.Dashboard as Home
import Page.GroupChest as GroupChest
import Page.Shop as Shop
import Route
import Session exposing (Session)
import Utils exposing (renderIcon)
import Wealth
type Page
= Dashboard Dashboard.Model
= Home Home.Model
| GroupChest GroupChest.Model
| Shop Shop.Model
| About
@@ -25,14 +26,16 @@ init =
Loading
mapMsg toMsg =
List.map (Html.map toMsg)
mapPageMsg toMsg ( controls, content ) =
( Html.map toMsg controls
, List.map (Html.map toMsg) content
)
maybeSession page =
case page of
Dashboard model ->
Just <| Dashboard.getSession model
Home model ->
Just <| Home.getSession model
GroupChest model ->
Just <| Session.getSession model
@@ -48,35 +51,37 @@ view page =
let
( title, ( controls, content ) ) =
case page of
Dashboard home ->
Home home ->
( "Lootalot"
, Dashboard.view home
|> Tuple.mapBoth
(Html.map GotDashboardMsg)
(mapMsg GotDashboardMsg)
, Home.view home
|> mapPageMsg GotHomeMsg
)
GroupChest chest ->
( "Lootalot"
, GroupChest.view chest
|> Tuple.mapBoth
(Html.map GotGroupChestMsg)
(mapMsg GotGroupChestMsg)
|> mapPageMsg GotGroupChestMsg
)
Shop shop ->
( "Marchand"
, Shop.view shop
|> Tuple.mapBoth
(Html.map GotShopMsg)
(mapMsg GotShopMsg)
|> mapPageMsg GotShopMsg
)
About ->
( "Loot-a-lot", ( text "", [ p [] [ text "A propos" ] ] ) )
Loading ->
( "Loot-a-lot", ( text "", [ p [] [ text "Chargement" ] ] ) )
( "Loot-a-lot"
, ( text ""
, [ div [ class "hero" ]
[ div [ class "hero-body" ]
[ p [] [ text "Chargement" ] ]
]
]
)
)
navbarTitle =
case maybeSession page of
@@ -96,20 +101,16 @@ view page =
Just session ->
case Session.user session of
Session.Player data ->
let
linkWithGem =
navLink "fas fa-gem"
in
[ navLink "fas fa-store-alt" "Marchand" "/marchand"
[ navLink "fas fa-store-alt" Route.Merchant page
, if data.player.id /= 0 then
linkWithGem "Coffre de groupe" "/coffre"
navLink "fas fa-gem" Route.GroupChest page
else
text ""
]
Session.Admin ->
[ navLink "fas fa-store-alt" "Marchand" "/marchand" ]
[ navLink "fas fa-store-alt" Route.Merchant page ]
Nothing ->
[]
@@ -118,25 +119,44 @@ view page =
, { title = navbarTitle, links = navbarLinks }
, [ div [ class "container" ] <|
viewSessionBar (maybeSession page) [ controls ]
:: (case Maybe.map Session.notification (maybeSession page) of
Just (Just notify) ->
div [ class "notification is-success" ] [ text notify ]
:: div [ class "section" ]
[ case Maybe.map Session.notification (maybeSession page) of
Just (Just t) ->
viewNotification NotifySuccess t
_ ->
text ""
)
:: (case Maybe.map Session.error (maybeSession page) of
Just (Just notify) ->
div [ class "notification is-danger" ] [ text notify ]
, case Maybe.map Session.error (maybeSession page) of
Just (Just t) ->
viewNotification NotifyError t
_ ->
text ""
)
]
:: content
]
)
type NotificationKind
= NotifySuccess
| NotifyError
viewNotification kind content =
let
className =
case kind of
NotifySuccess ->
"is-success"
NotifyError ->
"is-danger"
in
div [ class ("notification " ++ className) ]
[ text content ]
viewSessionBar session controls =
let
user =
@@ -149,8 +169,10 @@ viewSessionBar session controls =
Wealth.view data.player.wealth data.wealth
++ (if data.player.debt > 0 then
[ div [ class "level-item" ]
[ p [ class "heading is-size-4 has-text-danger" ]
[ text ("Dette : " ++ String.fromInt data.player.debt ++ "po") ]
[ p [ class "has-text-right has-text-danger" ]
[ strong [ class "heading is-marginless has-text-danger" ] [ text "Dette" ]
, span [ class <| "is-size-4" ] [ text (String.fromInt data.player.debt ++ "po") ]
]
]
]
@@ -179,10 +201,42 @@ renderLevel left right =
-- PLAYER BAR
navLink icon linkText url =
a [ class "navbar-item", href url ]
navLink icon route page =
let
( link, url ) =
case route of
Route.Merchant ->
( "Marchand", "/marchand" )
Route.GroupChest ->
( "Coffre de groupe", "/groupe" )
Route.Home ->
( "Home", "/" )
Route.About ->
( "About", "/" )
isActive =
case ( route, page ) of
( Route.Merchant, Shop _ ) ->
True
( Route.GroupChest, GroupChest _ ) ->
True
( Route.Home, Home _ ) ->
True
( Route.About, About ) ->
True
_ ->
False
in
a [ class "navbar-item", classList [ ( "is-active", isActive ) ], href url ]
[ renderIcon { icon = icon, ratio = "1x", size = "medium" }
, span [] [ text linkText ]
, span [] [ text link ]
]
@@ -194,7 +248,7 @@ navLink icon linkText url =
type PageMsg
= ApiMsg Api.Msg
| GotGroupChestMsg GroupChest.Msg
| GotDashboardMsg Dashboard.Msg
| GotHomeMsg Home.Msg
| GotShopMsg Shop.Msg
| Wealth Wealth.Msg
@@ -210,8 +264,8 @@ map func page =
Just session ->
case page of
Dashboard model ->
Dashboard <| Dashboard.updateSession model (func session)
Home model ->
Home <| Home.updateSession model (func session)
GroupChest model ->
GroupChest { model | session = func session }
@@ -229,7 +283,7 @@ map func page =
closeAction ( page, cmd ) =
case page of
Dashboard home ->
Home home ->
( page, cmd )
GroupChest chest ->
@@ -244,17 +298,17 @@ closeAction ( page, cmd ) =
update msg page =
case ( msg, page, maybeSession page ) of
-- Dashboard page
-- Home page
-- Capture API messages
( GotDashboardMsg (Dashboard.Api apiMsg), Dashboard home, _ ) ->
( GotHomeMsg (Home.Api apiMsg), Home home, _ ) ->
update (ApiMsg apiMsg) page
-- Relay others
( GotDashboardMsg subMsg, Dashboard home, _ ) ->
Dashboard.update subMsg home
|> updatePage Dashboard GotDashboardMsg
( GotHomeMsg subMsg, Home home, _ ) ->
Home.update subMsg home
|> updatePage Home GotHomeMsg
( GotDashboardMsg _, _, _ ) ->
( GotHomeMsg _, _, _ ) ->
( page, Cmd.none )
-- Group chest
@@ -417,8 +471,8 @@ applyUpdate u user =
initHome session =
Dashboard.init session
|> updatePage Dashboard GotDashboardMsg
Home.init session
|> updatePage Home GotHomeMsg
gotoHome page =
@@ -427,8 +481,8 @@ gotoHome page =
( page, Cmd.none )
Just session ->
Dashboard.init session
|> updatePage Dashboard GotDashboardMsg
Home.init session
|> updatePage Home GotHomeMsg
gotoShop page =