-
Notifications
You must be signed in to change notification settings - Fork 2
/
FlightBooker.elm
345 lines (269 loc) · 7.86 KB
/
FlightBooker.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
module Task.FlightBooker exposing (Model, Msg, init, update, view)
import Html as H
import Html.Attributes as HA
import Html.Events as HE
import Support.Lib as Lib
import Support.View.Button as Button
import Support.View.Control as Control
import Support.View.Frame as Frame
import Task.FlightBooker.Date as Date exposing (Date)
import Task.FlightBooker.Port as Port
-- MODEL
type Model
= Loading
| Loaded Booking
type alias Booking =
{ flight : Flight
, start : Field
, end : Field
}
type Flight
= OneWay
| Return
type Field
= Valid Date
| Invalid String
isBookable : Booking -> Bool
isBookable =
withBooking
{ onOneWay = always True
, onReturn = always (always True)
, default = False
}
withBooking :
{ onOneWay : Date -> a
, onReturn : Date -> Date -> a
, default : a
}
-> Booking
-> a
withBooking { onOneWay, onReturn, default } { flight, start, end } =
case ( flight, start, end ) of
( OneWay, Valid startDate, _ ) ->
onOneWay startDate
( Return, Valid startDate, Valid endDate ) ->
if endDate |> Date.isLaterThan startDate then
onReturn startDate endDate
else
default
_ ->
default
init : ( Model, Cmd Msg )
init =
( Loading
, Date.today <| LoadingMsg << GotDate
)
-- UPDATE
type Msg
= LoadingMsg LoadingMsg
| LoadedMsg LoadedMsg
type LoadingMsg
= GotDate Date
type LoadedMsg
= FocusFlight
| InputFlight Flight
| InputStart String
| InputEnd String
| Submitted
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case ( msg, model ) of
( LoadingMsg loadingMsg, Loading ) ->
updateLoading loadingMsg
( LoadedMsg loadedMsg, Loaded booking ) ->
updateLoaded loadedMsg booking
_ ->
( model
, Cmd.none
)
updateLoading : LoadingMsg -> ( Model, Cmd Msg )
updateLoading msg =
case msg of
GotDate date ->
( Loaded
{ flight = OneWay
, start = Valid date
, end = Valid date
}
, Lib.focus "oneWayOrReturn" <| LoadedMsg FocusFlight
)
updateLoaded : LoadedMsg -> Booking -> ( Model, Cmd msg )
updateLoaded msg booking =
case msg of
FocusFlight ->
( Loaded booking
, Cmd.none
)
InputFlight flight ->
( Loaded { booking | flight = flight }
, Cmd.none
)
InputStart rawInput ->
( Loaded
{ booking
| start =
case Date.fromString rawInput of
Just startDate ->
Valid startDate
Nothing ->
Invalid rawInput
}
, Cmd.none
)
InputEnd rawInput ->
( case booking.flight of
OneWay ->
Loaded booking
Return ->
Loaded
{ booking
| end =
case Date.fromString rawInput of
Just endDate ->
Valid endDate
Nothing ->
Invalid rawInput
}
, Cmd.none
)
Submitted ->
( Loaded booking
, withBooking
{ onOneWay =
\startDate ->
Port.alert <| "You have booked a one-way flight for " ++ Date.toString startDate ++ "."
, onReturn =
\startDate endDate ->
Port.alert <| "You have booked a return flight from " ++ Date.toString startDate ++ " to " ++ Date.toString endDate ++ "."
, default = Cmd.none
}
booking
)
-- VIEW
view : Model -> H.Html Msg
view model =
case model of
Loading ->
H.text "Loading..."
Loaded ({ flight, start, end } as booking) ->
H.map LoadedMsg <|
Frame.view
{ modifier = Frame.FlightBooker
, title = "Flight Booker"
, body =
H.form
[ HA.class "flight-booker"
, HE.onSubmit Submitted
]
[ viewField
{ id = "oneWayOrReturn"
, text = "One way or return:"
, toField = \id -> viewFlight id flight
}
, viewField
{ id = "departure"
, text = "Departure date (format: DD.MM.YYYY):"
, toField = \id -> viewStart id start
}
, viewField
{ id = "return"
, text = "Return date (format: DD.MM.YYYY):"
, toField = \id -> viewEnd id flight end
}
, viewSubmitButton booking
]
}
viewField :
{ id : String
, text : String
, toField : String -> H.Html msg
}
-> H.Html msg
viewField { id, text, toField } =
H.div [ HA.class "flight-booker__field" ]
[ Control.viewLabel
{ for = id
, text = text
}
, toField id
]
viewFlight : String -> Flight -> H.Html LoadedMsg
viewFlight id flight =
let
toOption current =
( flight == current
, current
)
in
Control.viewSelect
{ id = id
, fromString = flightFromString
, onInput = InputFlight
, toValue = flightToValue
, toText = flightToText
, options = List.map toOption [ OneWay, Return ]
}
flightFromString : String -> Maybe Flight
flightFromString s =
case s of
"one-way" ->
Just OneWay
"return" ->
Just Return
_ ->
Nothing
flightToValue : Flight -> String
flightToValue flight =
case flight of
OneWay ->
"one-way"
Return ->
"return"
flightToText : Flight -> String
flightToText flight =
case flight of
OneWay ->
"one-way flight"
Return ->
"return flight"
viewStart : String -> Field -> H.Html LoadedMsg
viewStart id =
viewInput id <| Just InputStart
viewEnd : String -> Flight -> Field -> H.Html LoadedMsg
viewEnd id flight =
viewInput id <|
case flight of
OneWay ->
Nothing
Return ->
Just InputEnd
viewInput : String -> Maybe (String -> msg) -> Field -> H.Html msg
viewInput id maybeOnInput field =
let
( status, value ) =
case field of
Valid date ->
( Control.Normal
, Date.toString date
)
Invalid s ->
( Control.HasError
, s
)
in
Control.viewInput
{ id = id
, status = status
, value = value
, maybeOnInput = maybeOnInput
}
viewSubmitButton : Booking -> H.Html msg
viewSubmitButton booking =
Button.view
{ type_ =
if isBookable booking then
Button.Submit
else
Button.Disabled
, text = "Book"
}