Created
April 4, 2019 05:25
-
-
Save dmsnell/e2c1a80c65109d45ab712a98c4670d15 to your computer and use it in GitHub Desktop.
Using the URL fragment/query args to determine which "sub app" to render, using initial URL to inform state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (main) | |
import Browser as B | |
import Browser.Navigation as BN | |
import Html as H | |
import Html.Attributes as HA | |
import Html.Events as HE | |
import Url as U | |
type alias Model = | |
{ selectedRobot : Maybe String } | |
type Msg | |
= None | |
type alias Flags = | |
{} | |
main = | |
B.application | |
{ init = init | |
, onUrlChange = \_ -> None | |
, onUrlRequest = \_ -> None | |
, subscriptions = \_ -> Sub.none | |
, update = update | |
, view = view | |
} | |
init : Flags -> U.Url -> BN.Key -> ( Model, Cmd Msg ) | |
init flags url key = | |
( { selectedRobot = url.fragment | |
} | |
, Cmd.none | |
) | |
update : Msg -> Model -> ( Model, Cmd msg ) | |
update msg model = | |
( model, Cmd.none ) | |
type alias Robot = | |
{ name : String | |
, host : String | |
, load : ( Float, Float, Float ) | |
} | |
robots = | |
[ Robot "Hector" "iad.domain" ( 6.54, 1.8, 1.2 ) | |
, Robot "Vargus" "dfw.domain" ( 1.2, 0.7, 0.7 ) | |
, Robot "Pharnup" "fra.domain" ( 4.1, 8.4, 3.6 ) | |
] | |
view model = | |
{ title = "Dashboard" | |
, body = | |
[ case model.selectedRobot of | |
Just name -> | |
viewRobot name | |
Nothing -> | |
viewDashboard model | |
] | |
} | |
viewDashboard model = | |
H.div [] | |
[ H.node "style" | |
[] | |
[ H.text style ] | |
, H.h1 [] | |
[ H.text "Dashboard" ] | |
, H.ul | |
[] | |
(List.map | |
(\{ name } -> | |
H.li [] | |
[ H.a | |
[ HA.target "_blank", HA.href ("#" ++ name) ] | |
[ H.text name ] | |
] | |
) | |
robots | |
) | |
] | |
viewRobot name = | |
H.div | |
[] | |
(robots | |
|> List.filter (\robot -> robot.name == name) | |
|> List.map | |
(\{ host, load } -> | |
let | |
( l1, l5, l15 ) = | |
load | |
in | |
H.div | |
[] | |
[ H.p [] [ H.text <| name ++ " on " ++ host ] | |
, H.p [] [ H.text <| "( " ++ String.fromFloat l1 ++ ", " ++ String.fromFloat l5 ++ ", " ++ String.fromFloat l15 ++ " )" ] | |
] | |
) | |
) | |
style = | |
""" | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment