aboutsummaryrefslogtreecommitdiff
path: root/front/src
diff options
context:
space:
mode:
authorGuillermo Ramos2025-02-21 16:29:34 +0100
committerGuillermo Ramos2025-02-21 17:10:07 +0100
commite82162fbaf16703d9e9b589e7eb2ff8c62713838 (patch)
tree9a5b6d42b4898ae95ebd97b5f009782bbe4b8348 /front/src
parentdcfeabc173b6a98b438549b3cccc12c381c04564 (diff)
downloadhiccup-e82162fbaf16703d9e9b589e7eb2ff8c62713838.tar.gz
Show only years, usability fixes
Diffstat (limited to 'front/src')
-rw-r--r--front/src/Main.elm103
1 files changed, 55 insertions, 48 deletions
diff --git a/front/src/Main.elm b/front/src/Main.elm
index 4c8fc94..9139158 100644
--- a/front/src/Main.elm
+++ b/front/src/Main.elm
@@ -2,8 +2,8 @@ module Main exposing (..)
import Round
import Browser
-import Html exposing (Html, button, div, input, text)
-import Html.Attributes exposing (max, min, step, type_, value)
+import Html exposing (Html, button, div, input, text, hr)
+import Html.Attributes exposing (max, min, step, type_, value, disabled)
import Html.Events exposing (onClick, onInput)
import Http
import Json.Decode exposing (Decoder, field, float, int, list, map, map2, map3, map4)
@@ -77,6 +77,19 @@ simDecoder =
(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
@@ -86,7 +99,7 @@ type alias SimSpecs =
type alias Model =
{ error : String
- , simSpecs : SimSpecs
+ , simSpecsTxt : SimSpecsTxt
, simulation : Maybe Simulation
}
@@ -126,13 +139,10 @@ runSimulation simSpecs =
init : () -> ( Model, Cmd Msg )
init () =
let
- simSpecs =
- { principal = 200000.0, i1 = 1.621, years = 30 }
-
- req =
- runSimulation simSpecs
+ simSpecsTxt =
+ { principalTxt = "200000", i1Txt = "1.621", yearsTxt = "30" }
in
- ( { error = "", simSpecs = simSpecs, simulation = Nothing }, req )
+ ( { error = "", simSpecsTxt = simSpecsTxt, simulation = Nothing }, Cmd.none )
@@ -148,7 +158,7 @@ type SimSpecUpdate
type Msg
= GotSimulation (Result Http.Error Simulation)
| UpdateSimSpecs SimSpecUpdate String
- | RunSimulation
+ | RunSimulation SimSpecs
update : Msg -> Model -> ( Model, Cmd Msg )
@@ -164,41 +174,26 @@ update msg model =
GotSimulation (Err err) ->
( { model | error = errorToString err }, Cmd.none )
- RunSimulation ->
- ( model, runSimulation model.simSpecs )
+ RunSimulation specs ->
+ (model, runSimulation specs)
UpdateSimSpecs u val ->
let
- simSpecs =
- model.simSpecs
+ simSpecsTxt =
+ model.simSpecsTxt
- m =
+ newSimSpecsTxt =
case u of
Principal ->
- case String.toFloat val of
- Just p ->
- { model | simSpecs = { simSpecs | principal = p } }
-
- Nothing ->
- { model | error = "Error parsing principal" }
+ { simSpecsTxt | principalTxt = val }
I1 ->
- case String.toFloat val of
- Just i ->
- { model | simSpecs = { simSpecs | i1 = i } }
-
- Nothing ->
- { model | error = "Error parsing interest" }
+ { simSpecsTxt | i1Txt = val }
Years ->
- case String.toInt val of
- Just i ->
- { model | simSpecs = { simSpecs | years = i } }
-
- Nothing ->
- { model | error = "Error parsing years" }
+ { simSpecsTxt | yearsTxt = val }
in
- ( m, Cmd.none )
+ ( { model | simSpecsTxt = newSimSpecsTxt }, Cmd.none )
@@ -230,8 +225,16 @@ errorToString error =
errorMessage
-specsView : SimSpecs -> Html Msg
-specsView { principal, i1, years } =
+specsView : SimSpecsTxt -> Html Msg
+specsView simSpecsTxt =
+ let
+ { principalTxt, i1Txt, yearsTxt } = simSpecsTxt
+ simSpecs = simSpecsParse simSpecsTxt
+ simButtonAttrs = case simSpecs of
+ Nothing -> [ disabled True ]
+ Just specs -> [ onClick (RunSimulation specs) ]
+
+ in
div []
[ div []
[ text "Principal: "
@@ -240,18 +243,18 @@ specsView { principal, i1, years } =
, Html.Attributes.min "0"
, Html.Attributes.max "1000000"
, step "10000"
- , value (String.fromFloat principal)
+ , value principalTxt
, onInput (UpdateSimSpecs Principal)
]
[]
- , text (Round.round 2 principal)
+ , text principalTxt
]
, div []
[ text "Interest rate: "
, input
[ Html.Attributes.min "0"
, Html.Attributes.max "100"
- , value (Round.round 2 i1)
+ , value i1Txt
, onInput (UpdateSimSpecs I1)
]
[]
@@ -263,20 +266,22 @@ specsView { principal, i1, years } =
, Html.Attributes.min "1"
, Html.Attributes.max "50"
, step "1"
- , value (String.fromInt years)
+ , value yearsTxt
, onInput (UpdateSimSpecs Years)
]
[]
- , text (String.fromInt years)
+ , text yearsTxt
]
- , button [ onClick RunSimulation ] [ text "Simulate" ]
+ , button simButtonAttrs [ text "Simulate" ]
]
quotaView : Quota -> Html Msg
quotaView { period, payed, pending_principal } =
- div [] [ text (String.join "\t" [ String.fromInt period, capitalSumStr payed, capitalStr payed, Round.round 2 pending_principal ]) ]
-
+ if modBy 12 period == 0 then
+ div [] [ text (String.join "\t" [ String.fromInt period, capitalSumStr payed, capitalStr payed, Round.round 2 pending_principal ]) ]
+ else
+ text ""
historyView : List Quota -> Html Msg
historyView quotas =
@@ -286,16 +291,18 @@ historyView quotas =
simView : Simulation -> Html Msg
simView { history, topay, payed } =
div []
- [ historyView history
- , div [] [ text (String.concat [ "to pay: ", capitalStr topay ]) ]
- , div [] [ text (String.concat [ "payed: ", capitalStr payed ]) ]
+ [ hr [] []
+ , historyView history
+ , hr [] []
+ , div [] [ text (String.concat [ "to pay: ", capitalSumStr topay, " (", capitalStr topay, ")" ]) ]
+ , div [] [ text (String.concat [ "payed: ", capitalSumStr payed, " (", capitalStr payed, ")" ]) ]
]
view : Model -> Html Msg
view model =
div []
- [ specsView model.simSpecs
+ [ specsView model.simSpecsTxt
, case model.simulation of
Nothing ->
text ""