-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDevfriendly.elm
315 lines (226 loc) · 6.47 KB
/
Devfriendly.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
port module Devfriendly exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Json.Decode as Decode exposing (Decoder, field, list)
import Json.Decode.Pipeline as Pipe exposing (decode, required, optional, optionalAt)
import Http
import Navigation
-- PORTS
port moveMap : Town -> Cmd msg
port addPlaces : List Place -> Cmd msg
-- MODEL
type alias Model =
{ towns : List Town
, places : List Place
, selectedTown : TownSlug
}
type alias Place =
{ name : String
, latitude : Float
, longitude : Float
, address : String
, url : String
, openHours : String
, comment : String
, wifi : Bool
, power : Bool
}
-- UPDATE
type Msg
= TownOnChange String
| GetTowns (Result Http.Error (List Town))
| GetPlaces (Result Http.Error (List Place))
| UrlChange Navigation.Location
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
UrlChange location ->
let
townSlug =
urlToTownSlug location
town =
findTown townSlug model.towns
in
case town of
Just town ->
( { model | selectedTown = townSlug }
, moveMap town
)
Nothing ->
let
_ =
Debug.log "Not a town" townSlug
in
( model, Cmd.none )
TownOnChange townName ->
let
hash =
"#" ++ slugifyTownName townName
in
( model, Navigation.newUrl hash )
GetTowns (Ok towns) ->
let
hash =
"#" ++ model.selectedTown
in
( { model | towns = towns }, Navigation.newUrl hash )
GetTowns (Err error) ->
let
_ =
Debug.log "Get Towns Failed" error
in
( { model | towns = [] }, Cmd.none )
GetPlaces (Ok places) ->
( { model
| places = places ++ model.places
}
, addPlaces places
)
GetPlaces (Err error) ->
let
_ =
Debug.log "Get Towns Failed" error
in
( { model | places = [] }, Cmd.none )
-- VIEW
onChange : (String -> msg) -> Html.Attribute msg
onChange tagger =
on "change" (Decode.map tagger Html.Events.targetValue)
viewMenu : Model -> Html Msg
viewMenu model =
let
townsOption =
List.map
(\town ->
option
[ selected (model.selectedTown == (slugifyTownName town.name)) ]
[ text town.name ]
)
(List.sortBy .name model.towns)
in
select [ id "towns", onChange TownOnChange ] townsOption
view : Model -> Html Msg
view model =
viewMenu model
-- Commands
loadTowns : String -> Cmd Msg
loadTowns url =
Decode.list townDecoder
|> Http.get url
|> Http.send GetTowns
loadPlaces : String -> Cmd Msg
loadPlaces url =
Decode.list placeDecoder
|> Http.get url
|> Http.send GetPlaces
cmdsDisplayTown : Town -> List (Cmd Msg)
cmdsDisplayTown town =
[ moveMap town ]
-- Decoder
townDecoder : Decoder Town
townDecoder =
Pipe.decode Town
|> Pipe.required "name" Decode.string
|> Pipe.required "lat" Decode.float
|> Pipe.required "lon" Decode.float
|> Pipe.required "defaultZoom" Decode.int
placeDecoder : Decoder Place
placeDecoder =
Pipe.decode Place
|> Pipe.required "name" Decode.string
|> Pipe.required "lat" Decode.float
|> Pipe.required "lon" Decode.float
|> Pipe.optional "address" Decode.string ("")
|> Pipe.optional "url" Decode.string ("")
|> Pipe.optional "openHours" Decode.string ("")
|> Pipe.optional "comment" Decode.string ("")
|> Pipe.optionalAt [ "wifi", "available" ] Decode.bool False
|> Pipe.optionalAt [ "power", "available" ] Decode.bool False
placesDecode : String -> List Place
placesDecode jsonPlaces =
let
result =
Decode.decodeString (Decode.list placeDecoder) jsonPlaces
in
case result of
Ok places ->
places
Err error ->
[]
-- Towns
type alias TownSlug =
String
type alias Town =
{ name : String
, latitude : Float
, longitude : Float
, defaultZoom : Int
}
slugifyTownName : String -> TownSlug
slugifyTownName town =
town
|> String.toLower
|> String.map
(\c ->
case c of
' ' ->
'-'
'é' ->
'e'
'è' ->
'e'
'à' ->
'a'
_ ->
c
)
urlToTownSlug : Navigation.Location -> TownSlug
urlToTownSlug location =
case location.hash of
"" ->
defaultTown
hash ->
String.dropLeft 1 hash
findTown : TownSlug -> List Town -> Maybe Town
findTown townSlug towns =
towns
|> List.filter (\t -> (slugifyTownName t.name) == townSlug)
|> List.head
-- MAIN
baseUrl : String
baseUrl =
"https://raw.githubusercontent.com/devfriendlyplaces/data/master/locations/"
defaultTown : TownSlug
defaultTown =
"toulouse"
townsUrl : String
townsUrl =
baseUrl ++ "locations.json"
placesUrl : String
placesUrl =
"/" ++ "all_places.json"
placesUrlFor : TownSlug -> String
placesUrlFor townSlug =
baseUrl ++ townSlug ++ ".json"
main : Program Never Model Msg
main =
let
initialModel location =
let
townSlug =
urlToTownSlug location
in
( { towns = []
, places = []
, selectedTown = townSlug
}
, Cmd.batch [ loadTowns townsUrl, loadPlaces placesUrl ]
)
in
Navigation.program UrlChange
{ init = initialModel
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}