1
- module Demo exposing (init , update , view )
1
+ module Demo exposing (Msg (..) , init , update , view )
2
2
3
- import Browser exposing (Document )
3
+ import Browser exposing (Document , UrlRequest (..) )
4
+ import Browser.Navigation exposing (Key )
4
5
import Html exposing (Html , div , span , text )
5
6
import Html.Attributes exposing (class , classList , style )
6
7
import Html.Events exposing (onClick )
@@ -9,7 +10,9 @@ import Json.Form
9
10
import Json.Form.Config exposing (TextFieldStyle (..) )
10
11
import Json.Schema.Definitions exposing (Schema )
11
12
import Json.Value exposing (JsonValue (..) )
12
- import Snippets exposing (Example , Snippet (..) , getSnippet , getSnippetTitle )
13
+ import Route exposing (Route (..) )
14
+ import Showcase exposing (Example , Showcase (..) , getShowcase , getShowcaseTitle )
15
+ import Url exposing (Url )
13
16
14
17
15
18
type alias ExampleDemo =
@@ -19,32 +22,56 @@ type alias ExampleDemo =
19
22
20
23
21
24
type alias Model =
22
- { showcase : Snippet
25
+ { showcase : Showcase
23
26
, examples : List ExampleDemo
27
+ , key : Key
24
28
}
25
29
26
30
27
- initialShowcase : Snippet
31
+ initialShowcase : Showcase
28
32
initialShowcase =
29
33
Validation
30
34
31
35
32
- init : Value -> ( Model , Cmd Msg )
33
- init _ =
36
+ init : Value -> Url -> Key -> ( Model , Cmd Msg )
37
+ init _ url key =
34
38
{ showcase = initialShowcase
35
39
, examples = []
40
+ , key = key
36
41
}
37
- |> update ( SetShowcase initialShowcase )
42
+ |> update ( url |> Route . fromLocation |> SetRoute )
38
43
39
44
40
45
type Msg
41
- = SetShowcase Snippet
46
+ = SetShowcase Showcase
42
47
| JsonFormMsg Int Json . Form . Msg
48
+ | SetRoute ( Maybe Route )
49
+ | UrlRequested UrlRequest
43
50
44
51
45
52
update : Msg -> Model -> ( Model , Cmd Msg )
46
53
update message model =
47
54
case message of
55
+ UrlRequested urlRequest ->
56
+ case urlRequest of
57
+ Internal url ->
58
+ ( model
59
+ , Browser . Navigation . pushUrl model. key ( Url . toString url)
60
+ )
61
+
62
+ External url ->
63
+ ( model
64
+ , Browser . Navigation . load url
65
+ )
66
+
67
+ SetRoute route ->
68
+ case route of
69
+ Just ( ShowcasePage sc) ->
70
+ model |> update ( SetShowcase sc)
71
+
72
+ _ ->
73
+ model |> update ( SetShowcase initialShowcase)
74
+
48
75
JsonFormMsg index msg ->
49
76
let
50
77
( examples, cmds ) =
@@ -71,7 +98,7 @@ update message model =
71
98
SetShowcase s ->
72
99
let
73
100
( examples, cmds ) =
74
- getSnippet s
101
+ getShowcase s
75
102
|> List . indexedMap
76
103
( \ index example ->
77
104
let
@@ -107,23 +134,24 @@ view model =
107
134
108
135
topbar : Model -> Html Msg
109
136
topbar model =
110
- Snippets . index
137
+ Showcase . index
111
138
|> List . map ( snippetTab model. showcase)
112
139
|> div [ class " app-topbar" ]
113
140
114
141
115
- snippetTab : Snippet -> Snippet -> Html Msg
116
- snippetTab activeSnippet snippet =
142
+ snippetTab : Showcase -> Showcase -> Html Msg
143
+ snippetTab activeShowcase showcase =
117
144
div
118
145
[ classList
119
146
[ ( " tab" , True )
120
- , ( " tab--active" , snippet == activeSnippet )
147
+ , ( " tab--active" , showcase == activeShowcase )
121
148
]
122
- , onClick <| SetShowcase snippet
123
149
]
124
- [ snippet
125
- |> getSnippetTitle
126
- |> text
150
+ [ Html . a [ Route . href <| ShowcasePage showcase ]
151
+ [ showcase
152
+ |> getShowcaseTitle
153
+ |> text
154
+ ]
127
155
]
128
156
129
157
0 commit comments