example-spa-elm-app/frontend/src/Page/Landing.elm

80 lines
1.9 KiB
Elm

module Page.Landing exposing
( Model
, Msg
, view
, init
, update
, subscriptions
)
import Element exposing (Element)
import Websockets
import Ports
import Json.Decode as Decode
import Html.Attributes exposing (placeholder)
import Element.Input
type alias Model = {
time : String
}
type alias Landing = {
time : String
}
type Msg
= ToFrontend Landing
| DecodeError Decode.Error
| GreetWidgetText String
| NoOp
init : () -> Model
init flags = {
time = "time not yet set"
}
decodeToFrontend message =
let
decodeTime = Decode.field "time" Decode.string |> Decode.map Landing
decodeLanding = Decode.field "Landing" decodeTime |> Decode.map ToFrontend
decodedMessage = Decode.decodeString decodeLanding message
in
case decodedMessage of
Ok decoded -> decoded
Err err -> DecodeError err
subscriptions : Model -> Sub Msg
subscriptions model =
Ports.socketOnEvent
(Websockets.EventHandlers
(\_ -> NoOp)
(\_ -> NoOp)
(\_ -> NoOp)
(\message -> (message.data |> decodeToFrontend))
(\_ -> NoOp)
)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
ToFrontend landing -> ( {model | time = landing.time}, Cmd.none )
_ -> (model, Cmd.none)
greetWidget =
let
textInput =
Element.Input.text []
{ onChange = GreetWidgetText
, text = "text"
, placeholder = Nothing
, label = Element.Input.labelHidden "Greet"
}
in
Element.row []
[ textInput ]
view : Model -> Element Msg
view model =
Element.column []
[ Element.text "Landing"
, Element.text <| "Current time is : " ++ model.time
, greetWidget
]