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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
module Main exposing (main)
import Browser
import Html exposing (Attribute, Html, button, div, hr, input, span, table, tbody, td, text, th, thead, tr)
import Html.Attributes exposing (class, disabled, max, min, step, style, title, type_, value)
import Html.Events exposing (onClick, onInput)
import Http
import Json.Decode exposing (Decoder, field, float, int, list, map, map2, map3, map4)
import Json.Encode as Encode exposing (Value, object)
import Round
import Set exposing (Set)
-- MAIN
main =
Browser.element { init = init, update = update, view = view, subscriptions = \_ -> Sub.none }
-- MODEL
type alias Capital =
{ principal : Float
, interest : Float
}
capitalDecoder : Decoder Capital
capitalDecoder =
map2 Capital
(field "principal" float)
(field "interest" float)
type alias Quota =
{ period : Int
, payed : Capital
, pending_principal : Float
}
quotaDecoder : Decoder Quota
quotaDecoder =
map3 Quota
(field "period" int)
(field "payed" capitalDecoder)
(field "pending_principal" float)
type alias Simulation =
{ history : List Quota
, topay : Capital
, payed : Capital
, payed_amortized : Float
}
simDecoder : Decoder Simulation
simDecoder =
map4 Simulation
(field "history" (list quotaDecoder))
(field "topay" capitalDecoder)
(field "payed" capitalDecoder)
(field "payed_amortized" float)
type alias SimSpecsTxt =
{ principalTxt : String
, i1Txt : String
, yearsTxt : String
}
simSpecsParse : SimSpecsTxt -> Maybe SimSpecs
simSpecsParse { principalTxt, i1Txt, yearsTxt } =
case ( String.toFloat principalTxt, String.toFloat i1Txt, String.toInt yearsTxt ) of
( Just principal, Just i1, Just years ) ->
Just { principal = principal, i1 = i1, years = years }
_ ->
Nothing
type alias SimSpecs =
{ principal : Float
, i1 : Float
, years : Int
}
type alias Model =
{ error : String
, simSpecsTxt : SimSpecsTxt
, expandedYears : Set Int
, simulation : Maybe Simulation
}
qs : List ( String, String ) -> String
qs ss =
String.join "&" (List.map (\( s, t ) -> String.join "=" [ s, t ]) ss)
simSpecsToURL : SimSpecs -> String
simSpecsToURL { principal, i1, years } =
let
base =
"/api/simulate"
in
String.concat
[ base
, "?"
, qs
[ ( "principal", String.fromFloat principal )
, ( "i1"
, String.fromFloat (i1 / 100)
)
, ( "years", String.fromInt years )
]
]
runSimulation : SimSpecs -> Cmd Msg
runSimulation simSpecs =
Http.get
{ url = simSpecsToURL simSpecs
, expect = Http.expectJson GotSimulation simDecoder
}
init : () -> ( Model, Cmd Msg )
init () =
let
simSpecsTxt =
{ principalTxt = "200000", i1Txt = "1.621", yearsTxt = "30" }
in
( { error = "", simSpecsTxt = simSpecsTxt, expandedYears = Set.empty, simulation = Nothing }, Cmd.none )
-- UPDATE
type SimSpecUpdate
= Principal
| I1
| Years
type Msg
= GotSimulation (Result Http.Error Simulation)
| UpdateSimSpecs SimSpecUpdate String
| RunSimulation SimSpecs
| SetExpandedYears (Set Int)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
let
_ =
Debug.log "UPDATE!" msg
in
case msg of
GotSimulation (Ok simulation) ->
( { model | simulation = Just simulation }, Cmd.none )
GotSimulation (Err err) ->
( { model | error = errorToString err }, Cmd.none )
RunSimulation specs ->
( model, runSimulation specs )
UpdateSimSpecs u val ->
let
simSpecsTxt =
model.simSpecsTxt
newSimSpecsTxt =
case u of
Principal ->
{ simSpecsTxt | principalTxt = val }
I1 ->
{ simSpecsTxt | i1Txt = val }
Years ->
{ simSpecsTxt | yearsTxt = val }
in
( { model | simSpecsTxt = newSimSpecsTxt }, Cmd.none )
SetExpandedYears eyears ->
( { model | expandedYears = eyears }, Cmd.none )
-- VIEW (THEME)
butAttrs : List (Attribute Msg)
butAttrs =
[ class "px-3 rounded-md bg-lime-300 enabled:active:bg-lime-400 border border-lime-600 disabled:opacity-75" ]
clickableAttrs : Msg -> List (Attribute Msg)
clickableAttrs msg =
[ onClick msg, class "text-blue-800", style "cursor" "pointer" ]
-- VIEW
errorToString : Http.Error -> String
errorToString error =
case error of
Http.BadUrl url ->
"The URL " ++ url ++ " was invalid"
Http.Timeout ->
"Unable to reach the server, try again"
Http.NetworkError ->
"Unable to reach the server, check your network connection"
Http.BadStatus 500 ->
"The server had a problem, try again later"
Http.BadStatus 400 ->
"Verify your information and try again"
Http.BadStatus _ ->
"Unknown error"
Http.BadBody errorMessage ->
errorMessage
specsView : SimSpecsTxt -> Html Msg
specsView simSpecsTxt =
let
{ principalTxt, i1Txt, yearsTxt } =
simSpecsTxt
simSpecs =
simSpecsParse simSpecsTxt
simButAttrs =
case simSpecs of
Nothing ->
[ disabled True ]
Just specs ->
[ onClick (RunSimulation specs) ]
in
div []
[ div [ class "flex" ]
[ text "Principal: "
, input
[ type_ "range"
, class "accent-lime-300"
, Html.Attributes.min "0"
, Html.Attributes.max "1000000"
, step "10000"
, value principalTxt
, onInput (UpdateSimSpecs Principal)
]
[]
, text principalTxt
]
, div [ class "flex" ]
[ text "Interest rate: "
, input
[ Html.Attributes.min "0"
, Html.Attributes.max "100"
, value i1Txt
, onInput (UpdateSimSpecs I1)
]
[]
]
, div [ class "flex" ]
[ text "Years: "
, input
[ type_ "range"
, class "accent-lime-300"
, Html.Attributes.min "1"
, Html.Attributes.max "50"
, step "1"
, value yearsTxt
, onInput (UpdateSimSpecs Years)
]
[]
, text yearsTxt
]
, button (butAttrs ++ simButAttrs) [ text "Simulate" ]
]
historyView : Model -> List Quota -> Html Msg
historyView m quotas =
let
titles =
[ "Year", "Month", "Quota", "Pending" ]
head =
thead [ class "bg-lime-100" ] [ tr [] (List.map (\t -> th [ class "px-3 py-1 border border-gray-300" ] [ text t ]) titles) ]
in
div [ class "py-4" ]
[ table [ class "border border-collapse border-gray-400" ]
[ head
, tbody []
(List.map (quotaView m)
quotas
)
]
]
capitalSumView : Capital -> Html Msg
capitalSumView { principal, interest } =
let
partsTitle =
String.concat [ "Principal: ", Round.round 2 principal, "\nInterest: ", Round.round 2 interest ]
in
span [ class "underline", title partsTitle ] [ text (Round.round 2 (principal + interest)) ]
periodToYear : Int -> Int
periodToYear period =
((period - 1) // 12) + 1
quotaView : Model -> Quota -> Html Msg
quotaView m { period, payed, pending_principal } =
let
year =
periodToYear period
yearExpanded =
Set.member year m.expandedYears
in
if modBy 12 (period - 1) == 0 then
tr []
(List.map (\t -> td [ class "px-3 py-1 border border-gray-300" ] [ t ])
[ div []
[ span
(clickableAttrs
(SetExpandedYears
((if yearExpanded then
Set.remove
else
Set.insert
)
year
m.expandedYears
)
)
)
[ text
(if yearExpanded then
"▼ "
else
"▶ "
)
]
, text (String.fromInt year)
]
, text
(String.fromInt period)
, capitalSumView payed
, text (Round.round 2 pending_principal)
]
)
else if yearExpanded then
tr []
(List.map (\t -> td [ class "px-3 py-1 border border-gray-300" ] [ t ])
[ text ""
, text
(String.fromInt period)
, capitalSumView payed
, text (Round.round 2 pending_principal)
]
)
else
text ""
simView : Model -> Simulation -> Html Msg
simView m { history, topay, payed } =
div []
[ historyView m history
, div [] [ text "to pay: ", capitalSumView topay ]
, div [] [ text "payed: ", capitalSumView payed ]
]
view : Model -> Html Msg
view m =
div [ class "flex flex-row max-w-sm mx-auto" ]
[ div [ class "flex flex-col items-center" ]
[ specsView m.simSpecsTxt
, case m.simulation of
Nothing ->
text ""
Just sim ->
simView m sim
, div [] [ text m.error ]
]
]
|