Skip to content

Instantly share code, notes, and snippets.

@dmsnell
Created Apr 4, 2019
Embed
What would you like to do?
Using the URL fragment/query args to determine which "sub app" to render, using initial URL to inform state
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