Files
lootalot-client/src/Main.elm
2019-11-14 12:33:07 +01:00

183 lines
4.1 KiB
Elm

module Main exposing (..)
import Api exposing (Claim, Claims, Item, Loot, Player, Wealth)
import Browser
import Browser.Navigation as Nav
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Json.Encode as E
import Page.Chest as Chest exposing (Msg)
import Route exposing (..)
import Session exposing (..)
import Set exposing (Set)
import Svg.Attributes
import Url
import Utils exposing (..)
-- Main
main : Program () Model Msg
main =
Browser.application
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
, onUrlChange = UrlChanged
, onUrlRequest = LinkClicked
}
-- Model
type Model
= Chest Chest.Model
-- | Admin Admin.Model
| About
| Loading Nav.Key
-- This is not what we really want.
-- The flags will be a Maybe Int (id of logged in player), so
-- in case there is no player logged in, we need to display
-- a "Home" page
-- This mean Chest cannot be initiated right away, and many model
-- fields are useless.
--
-- A User can :
-- - not be logged in -> See About page
-- - just loggend in -> See Loading page then Chest
-- - coming back being still logged in -> See Chest (or same as above)
init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init _ _ key =
( Loading key, Session.init SessionLoaded key )
{-
case flags of
Just id ->
let
session =
Session.playerSession key id
( chest, cmd ) =
Chest.init session
in
( Chest chest, Cmd.map GotChestMsg cmd )
Nothing ->
( About, Cmd.none )
-}
---
-- VIEWS
---
view : Model -> Browser.Document Msg
view model =
let
( title, content ) =
case model of
Chest chest ->
( "Loot-a-lot", List.map (Html.map GotChestMsg) (Chest.view chest) )
-- Admin admin ->
-- ("Administration", Admin.view admin)
About ->
( "A propos", [ p [] [ text "A propos" ] ] )
Loading _ ->
( "Chargement...", [ p [] [ text "Chargement" ] ] )
in
{ title = title
, body = content
}
type Msg
= UrlChanged Url.Url
| LinkClicked Browser.UrlRequest
| SessionLoaded (Maybe Session)
| GotChestMsg Chest.Msg
-- | GotAdminMsg Admin.Msg
update msg model =
let
updateChest chestMsg =
case model of
Chest chest ->
let
( newChest, cmd ) =
Chest.update chestMsg chest
in
( Chest newChest, Cmd.map GotChestMsg cmd )
_ ->
( About, Cmd.none )
in
case msg of
SessionLoaded session ->
case session of
Just logged ->
let
( chest, cmd ) =
Chest.init logged
in
( Chest chest, Cmd.map GotChestMsg cmd )
Nothing ->
( About, Cmd.none )
LinkClicked urlRequest ->
case model of
Chest chestModel ->
case urlRequest of
Browser.Internal url ->
( model, Nav.pushUrl chestModel.navKey (Url.toString url) )
Browser.External href ->
( model, Cmd.none )
_ ->
( model, Cmd.none )
UrlChanged url ->
let
route =
Route.fromUrl url
in
case route of
Just (Route.Home content) ->
updateChest (Chest.SetContent content)
_ ->
( About, Cmd.none )
GotChestMsg chestMsg ->
updateChest chestMsg
-- STATE Utils
-- SUBSCRIPTIONS
--
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.none