Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
96 lines (58 sloc) 1.83 KB
port module Main exposing (Model, Msg(..), init, main, update, view)
import Browser
import Html exposing (Html, button, div, h1, h2, img, text)
import Html.Attributes exposing (src)
import Html.Events exposing (onClick)
import Json.Decode
import Json.Encode
port sendStuff : Json.Encode.Value -> Cmd msg
port receiveStuff : (Json.Encode.Value -> msg) -> Sub msg
---- MODEL ----
type alias Model =
{ counter : Int, error : String }
init : Flags -> ( Model, Cmd Msg )
init flags =
( { counter = flags.value, error = "No error" }, Cmd.none )
---- UPDATE ----
type alias Flags =
{ value : Int
}
type Msg
= SendData
| Received (Result Json.Decode.Error Int)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SendData ->
( model, sendStuff <| Json.Encode.string "test" )
Received result ->
case result of
Ok value ->
( { model | counter = value }, Cmd.none )
Err error ->
( { model | error = Json.Decode.errorToString error }, Cmd.none )
valueDecoder : Json.Decode.Decoder Int
valueDecoder =
Json.Decode.field "value" Json.Decode.int
---- VIEW ----
view : Model -> Html Msg
view model =
div []
[ img [ src "/logo.svg" ] []
, h1 [] [ text "Your Elm App is working!" ]
, button [ onClick SendData ] [ text "Send some data" ]
, h2 [] [ text <| String.fromInt model.counter ]
, h2 [] [ text model.error ]
]
---- PROGRAM ----
subscriptions : Model -> Sub Msg
subscriptions model =
receiveStuff (Json.Decode.decodeValue valueDecoder >> Received)
main : Program Flags Model Msg
main =
Browser.element
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
You can’t perform that action at this time.