module Main exposing (..) import Api exposing (Claim, Claims, Item, Loot, Player, Wealth) import Browser import Browser.Navigation as Nav import Page.Chest as Chest exposing (Msg) import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) import Json.Encode as E import Route exposing (..) import Set exposing (Set) import Svg.Attributes import Url import Utils exposing (..) import Session exposing (..) -- Main main : Program (Maybe Int) 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 -- 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 : Maybe Int -> Url.Url -> Nav.Key -> ( Model, Cmd Msg ) init flags _ 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"] ]) in { title = title , body = content } type Msg = UrlChanged Url.Url | LinkClicked Browser.UrlRequest | 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 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