-
-
Save danneu/1b07761d80257df4f41b4861d248bd0f to your computer and use it in GitHub Desktop.
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 (Flags, Model, Msg(..), init, main, subscriptions, update, view) | |
import Browser | |
import Browser.Navigation as Nav exposing (Key) | |
import Html exposing (Html, a, button, div, h1, img, li, text, ul) | |
import Html.Attributes as Attr | |
import Html.Events as Events | |
import Time | |
import Url exposing (Url) | |
-- MODEL | |
type alias Model = | |
{ key : Nav.Key | |
, title : String | |
, counter : Int | |
} | |
type alias Flags = | |
{} | |
init : Flags -> Url -> Key -> ( Model, Cmd Msg ) | |
init flags url key = | |
( { key = key | |
, title = "hello" | |
, counter = 1 | |
} | |
, Cmd.none | |
) | |
-- UPDATE | |
type Msg | |
= NoOp | |
| ClickedLink Browser.UrlRequest | |
| Tick Time.Posix | |
| Increment | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
NoOp -> | |
( model, Cmd.none ) | |
Increment -> | |
( { model | counter = model.counter + 1 }, Cmd.none ) | |
Tick posix -> | |
let | |
newTitle = | |
if model.title == "hello" then | |
"bye" | |
else | |
"hello" | |
in | |
( { model | title = newTitle } | |
, Cmd.none | |
) | |
ClickedLink req -> | |
case req of | |
Browser.Internal url -> | |
( model, Nav.pushUrl model.key (Url.toString url) ) | |
Browser.External string -> | |
( model, Nav.load string ) | |
-- VIEW | |
view : Model -> Browser.Document Msg | |
view model = | |
{ title = model.title | |
, body = | |
[ ul | |
[] | |
[ li [] [ a [ Attr.href "/a" ] [ text "a" ] ] | |
, li [] [ a [ Attr.href "/b" ] [ text "b" ] ] | |
, li [] [ a [ Attr.href "/c" ] [ text "c" ] ] | |
, li [] [ a [ Attr.href "https://google.com" ] [ text "google.com" ] ] | |
] | |
, button | |
[ Events.onClick Increment ] | |
[ text ("Increment " ++ String.fromInt model.counter) ] | |
] | |
} | |
-- SUBSCRIPTIONS | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Time.every 1000 Tick | |
main : Program Flags Model Msg | |
main = | |
Browser.application | |
{ init = init | |
, update = update | |
, view = view | |
, subscriptions = subscriptions | |
, onUrlChange = | |
\url -> | |
let | |
_ = | |
Debug.log "onUrlChange" url | |
in | |
NoOp | |
, onUrlRequest = | |
\req -> | |
let | |
_ = | |
Debug.log "onUrlRequest" req | |
in | |
ClickedLink req | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment