-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMain.gren
More file actions
238 lines (190 loc) · 5.04 KB
/
Main.gren
File metadata and controls
238 lines (190 loc) · 5.04 KB
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
module Main exposing (main)
import Browser
import Html exposing (Html, text, div, input, button, select, option)
import Html.Attributes exposing (id, style, value, disabled)
import Html.Events as Event exposing (onInput)
import Task
main : Program {} Model Msg
main =
Browser.sandbox
{ init = init
, update = update
, view = view
}
type FlightType
= OneWay
| ReturnFlight
flightTypeToString : FlightType -> String
flightTypeToString flightType =
when flightType is
OneWay ->
"One-way flight"
ReturnFlight ->
"Return flight"
flightTypeFromString : String -> FlightType
flightTypeFromString str =
when str is
"Return flight" ->
ReturnFlight
_ ->
OneWay
type alias Model =
{ flightType : FlightType
, input1: String
, input2: String
, date1 : Maybe Date
, date2 : Maybe Date
}
init : Model
init =
let
date =
{ year = 2022
, month = 6
, day = 14
}
in
{ flightType = OneWay
, input1 = formatDate date
, input2 = formatDate date
, date1 = Just date
, date2 = Just date
}
type Msg
= FlightTypeChanged String
| Input1Changed String
| Input2Changed String
update : Msg -> Model -> Model
update msg model =
when msg is
FlightTypeChanged asString ->
{ model | flightType = flightTypeFromString asString }
Input1Changed str ->
{ model | input1 = str
, date1 = parseDate str
}
Input2Changed str ->
{ model | input2 = str
, date2 = parseDate str
}
-- VIEW
view : Model -> Html Msg
view model =
div
[ style "display" "flex"
, style "flex-direction" "column"
, style "width" "100px"
, style "padding" "40px"
]
[ flightTypeSelector
, dateInput
{ id = "departure"
, value = model.input1
, date = model.date1
, changeMsg = Input1Changed
, isDisabled = False
}
, dateInput
{ id = "arrival"
, value = model.input2
, date = model.date2
, changeMsg = Input2Changed
, isDisabled = model.flightType == OneWay
}
, bookButton
{ isDisabled = checkIfDisabled model.flightType model.date1 model.date2 }
]
flightTypeSelector : Html Msg
flightTypeSelector =
select
[ id "flight-type"
, onInput FlightTypeChanged
]
[ option [] [ text ( flightTypeToString OneWay ) ]
, option [] [ text ( flightTypeToString ReturnFlight ) ]
]
type alias DateInputConfig =
{ id: String
, value: String
, date: Maybe Date
, changeMsg: String -> Msg
, isDisabled: Bool
}
dateInput: DateInputConfig -> Html Msg
dateInput { id = id_, value = value_, date, changeMsg, isDisabled } =
input
[ id id_
, value value_
, onInput changeMsg
, disabled isDisabled
, style "background-color" ( if date == Nothing then "red" else "white" )
]
[]
bookButton : { isDisabled : Bool } -> Html Msg
bookButton { isDisabled } =
button
[ id "book"
, disabled isDisabled
]
[ text "Book" ]
-- Helper functions
type alias Date =
{ day : Int
, month : Int
, year : Int
}
parseDate : String -> Maybe Date
parseDate dateString =
if String.count dateString /= 10 then
Nothing
else
let
parts =
dateString
|> String.split "."
|> Array.mapAndKeepJust String.toInt
in
when parts is
[ day, month, year ] ->
Just
{ day = day
, month = month
, year = year
}
_ ->
Nothing
formatDate : Date -> String
formatDate date =
String.fromInt date.day
++ "."
++ String.fromInt date.month
++ "."
++ String.fromInt date.year
compareDate : Date -> Date -> Order
compareDate date1 date2 =
when compare date1.year date2.year is
EQ ->
when compare date1.month date2.month is
EQ ->
compare date1.day date2.day
monthComparison ->
monthComparison
yearComparison ->
yearComparison
checkIfDisabled : FlightType -> Maybe Date -> Maybe Date -> Bool
checkIfDisabled flightType maybeDate1 maybeDate2 =
when flightType is
OneWay ->
maybeDate1 == Nothing
ReturnFlight ->
let
compare =
Maybe.map2 compareDate
maybeDate1
maybeDate2
in
when compare is
Just GT ->
True
_ ->
False