Skip to content

Commit 4e5ee03

Browse files
committed
update example to 0.19.1
1 parent da04718 commit 4e5ee03

File tree

2 files changed

+47
-77
lines changed

2 files changed

+47
-77
lines changed

examples/Example.elm

Lines changed: 45 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
module Main exposing (Model, Msg(..), Route(..), init, main, onUrlChange, onUrlRequest, route, routeToString, subscriptions, update, view, viewLink, viewRoute)
1+
module Example exposing (main)
22

3-
import Browser exposing (Document, UrlRequest(..))
4-
import Browser.Navigation exposing (Key, pushUrl)
5-
import Html exposing (Html, a, button, code, div, h1, h3, li, text, ul)
3+
4+
import Browser
5+
import Browser.Navigation as Nav
6+
import Html exposing (Html, a, code, div, h1, h3, li, text, ul)
67
import Html.Attributes exposing (href)
7-
import Html.Events exposing (onClick)
88
import Url exposing (Url)
9-
import Url.Parser exposing ((</>), (<?>), Parser, s, top)
10-
import Url.Parser.Query
9+
import Url.Parser as P exposing (Parser, (</>), (<?>), s, top)
10+
import Url.Parser.Query as Q
11+
12+
13+
14+
-- MAIN
1115

1216

1317
main : Program () Model Msg
1418
main =
1519
Browser.application
16-
{ init = \_ -> init
20+
{ init = init
1721
, view = view
1822
, update = update
1923
, subscriptions = subscriptions
20-
, onUrlRequest = onUrlRequest
21-
, onUrlChange = onUrlChange
24+
, onUrlRequest = UrlRequest
25+
, onUrlChange = UrlChange
2226
}
2327

2428

@@ -28,13 +32,13 @@ main =
2832

2933
type alias Model =
3034
{ history : List (Maybe Route)
31-
, key : Key
35+
, key : Nav.Key
3236
}
3337

3438

35-
init : Url -> Key -> ( Model, Cmd Msg )
36-
init url key =
37-
( Model [ Url.Parser.parse route url ] key
39+
init : () -> Url -> Nav.Key -> ( Model, Cmd Msg )
40+
init _ url key =
41+
( Model [ P.parse routeParser url ] key
3842
, Cmd.none
3943
)
4044

@@ -49,12 +53,12 @@ type Route
4953
| BlogPost Int
5054

5155

52-
route : Parser (Route -> a) a
53-
route =
54-
Url.Parser.oneOf
55-
[ Url.Parser.map Home top
56-
, Url.Parser.map BlogList (s "blog" <?> Url.Parser.Query.string "search")
57-
, Url.Parser.map BlogPost (s "blog" </> Url.Parser.int)
56+
routeParser : Parser (Route -> a) a
57+
routeParser =
58+
P.oneOf
59+
[ P.map Home top
60+
, P.map BlogList (s "blog" <?> Q.string "search")
61+
, P.map BlogPost (s "blog" </> P.int)
5862
]
5963

6064

@@ -63,26 +67,29 @@ route =
6367

6468

6569
type Msg
66-
= NewUrl String
67-
| UrlChange Url
68-
| Updates
70+
= UrlChange Url
71+
| UrlRequest Browser.UrlRequest
6972

7073

7174
update : Msg -> Model -> ( Model, Cmd Msg )
7275
update msg model =
7376
case msg of
74-
NewUrl url ->
75-
( model
76-
, pushUrl model.key url
77-
)
78-
79-
UrlChange location ->
80-
( { model | history = Url.Parser.parse route location :: model.history }
77+
UrlChange url ->
78+
( { model | history = P.parse routeParser url :: model.history }
8179
, Cmd.none
8280
)
8381

84-
Updates ->
85-
( model, Cmd.none )
82+
UrlRequest request ->
83+
case request of
84+
Browser.Internal url ->
85+
( model
86+
, Nav.pushUrl model.key (Url.toString url)
87+
)
88+
89+
Browser.External url ->
90+
( model
91+
, Nav.load url
92+
)
8693

8794

8895

@@ -95,68 +102,31 @@ subscriptions model =
95102

96103

97104

98-
-- UPDATES
99-
100-
101-
onUrlRequest : UrlRequest -> Msg
102-
onUrlRequest req =
103-
case req of
104-
Internal url_ ->
105-
Debug.log ("URL requested for internal at " ++ Url.toString url_) Updates
106-
107-
External str ->
108-
Debug.log ("Some updates related to" ++ str) Updates
109-
110-
111-
onUrlChange : Url -> Msg
112-
onUrlChange url =
113-
UrlChange url
114-
115-
116-
117105
-- VIEW
118106

119107

120-
view : Model -> Document Msg
108+
view : Model -> Browser.Document Msg
121109
view model =
122-
Document "Example Page for elm/url"
110+
Browser.Document "Example Page for elm/url"
123111
[ div []
124112
[ h1 [] [ text "Links" ]
125113
, ul [] (List.map viewLink [ "/", "/blog/", "/blog/42", "/blog/37", "/blog/?search=cats" ])
126114
, h1 [] [ text "History" ]
127115
, ul [] (List.map viewRoute model.history)
128-
, h3 [] [ a [ href "http://example.com" ] [ text "External Link" ] ]
129-
, h3 [] [ a [ href "/something" ] [ text "Internal Link" ] ]
130116
]
131117
]
132118

133119

134120
viewLink : String -> Html Msg
135121
viewLink url =
136-
li [] [ button [ onClick (NewUrl url) ] [ text url ] ]
122+
li [] [ a [ href url ] [ text url ] ]
137123

138124

139125
viewRoute : Maybe Route -> Html msg
140126
viewRoute maybeRoute =
141127
case maybeRoute of
142128
Nothing ->
143-
li [] [ text "Invalid URL" ]
144-
145-
Just route_ ->
146-
li [] [ code [] [ text (routeToString route_) ] ]
147-
148-
149-
routeToString : Route -> String
150-
routeToString route_ =
151-
case route_ of
152-
Home ->
153-
"home"
154-
155-
BlogList Nothing ->
156-
"list all blog posts"
157-
158-
BlogList (Just search) ->
159-
"search for " ++ search
129+
li [] [ code [] [ text "Uknown URL" ] ]
160130

161-
BlogPost id ->
162-
"show blog " ++ String.fromInt id
131+
Just route ->
132+
li [] [ code [] [ text (Debug.toString route) ] ]

examples/elm.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"source-directories": [
44
"."
55
],
6-
"elm-version": "0.19.0",
6+
"elm-version": "0.19.1",
77
"dependencies": {
88
"direct": {
99
"elm/browser": "1.0.0",
@@ -22,4 +22,4 @@
2222
"direct": {},
2323
"indirect": {}
2424
}
25-
}
25+
}

0 commit comments

Comments
 (0)