now successfully decoding time
This commit is contained in:
parent
83d8196261
commit
a691de6567
|
@ -18,6 +18,8 @@ Now open `http://127.0.0.1:8080` in your browser.
|
|||
# TODO
|
||||
|
||||
- [x] Add Makefile
|
||||
- [ ] `EventHandlers.onMessage` should inject successfully decoded message into
|
||||
Msg type directly...
|
||||
- [ ] Run `uglify` twice as per [this link](https://github.com/rtfeldman/elm-spa-example/tree/master?tab=readme-ov-file#production-build)
|
||||
- [ ] Clicking in upper left should go to landing page.
|
||||
- [ ] Add GPLV3 License
|
||||
|
|
|
@ -65,7 +65,6 @@ subscriptions model = Sub.batch
|
|||
, model.page |> Body.subscriptions |> Sub.map Body
|
||||
]
|
||||
|
||||
|
||||
view : Model -> Browser.Document Msg
|
||||
view model =
|
||||
let
|
||||
|
|
|
@ -7,20 +7,57 @@ module Page.Landing exposing
|
|||
, subscriptions
|
||||
)
|
||||
import Element exposing (Element)
|
||||
import Websockets
|
||||
import Ports
|
||||
import Json.Decode as Decode
|
||||
|
||||
type alias Model = {}
|
||||
type alias Msg = {}
|
||||
type alias Model = {
|
||||
time : String
|
||||
}
|
||||
type Msg
|
||||
= SocketOpened
|
||||
| SocketClosed
|
||||
| SocketMessage String
|
||||
| NoOp
|
||||
|
||||
init : () -> Model
|
||||
init flags = {}
|
||||
init flags = {
|
||||
time = "time not yet set"
|
||||
}
|
||||
|
||||
decodeMessage : String -> String
|
||||
decodeMessage message =
|
||||
let
|
||||
decodedMessage = Decode.decodeString (Decode.field "time" Decode.string) message
|
||||
in
|
||||
case decodedMessage of
|
||||
Ok decoded -> decoded
|
||||
Err err -> "failed to decode" ++ message
|
||||
|
||||
subscriptions : Model -> Sub Msg
|
||||
subscriptions _ = Sub.none
|
||||
subscriptions model =
|
||||
Ports.socketOnEvent
|
||||
(Websockets.EventHandlers
|
||||
(\_ -> SocketOpened)
|
||||
(\_ -> SocketClosed)
|
||||
(\_ -> NoOp)
|
||||
(\message -> SocketMessage message.data)
|
||||
(\_ -> NoOp)
|
||||
)
|
||||
|
||||
update : Msg -> Model -> (Model, Cmd Msg)
|
||||
update msg model = (model, Cmd.none)
|
||||
update msg model =
|
||||
case msg of
|
||||
SocketMessage message ->
|
||||
let
|
||||
decoded = decodeMessage message
|
||||
in
|
||||
( {model | time = decoded}, Cmd.none )
|
||||
_ -> (model, Cmd.none)
|
||||
|
||||
view : Model -> Element Msg
|
||||
view model =
|
||||
Element.el []
|
||||
<| Element.text "Landing"
|
||||
Element.column []
|
||||
[ Element.text "Landing",
|
||||
Element.text <| "Time is : " ++ model.time
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue